当前位置: 首页>>代码示例>>Python>>正文


Python date_time.SharedDate类代码示例

本文整理汇总了Python中openpyxl.shared.date_time.SharedDate的典型用法代码示例。如果您正苦于以下问题:Python SharedDate类的具体用法?Python SharedDate怎么用?Python SharedDate使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了SharedDate类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

 def __init__(self, worksheet, column, row, value=None):
     self.column = column.upper()
     self.row = row
     # _value is the stored value, while value is the displayed value
     self._value = None
     self._hyperlink_rel = None
     self._data_type = self.TYPE_NULL
     if value:
         self.value = value
     self.parent = worksheet
     self.xf_index = 0
     self._shared_date = SharedDate(base_date=worksheet.parent.excel_base_date)
开发者ID:demula,项目名称:pintracker,代码行数:12,代码来源:cell.py

示例2: __init__

    def __init__(self, parent_workbook, title):
        Worksheet.__init__(self, parent_workbook, title)

        self._max_col = 0
        self._max_row = 0
        self._parent = parent_workbook

        self._fileobj_header_name = create_temporary_file(suffix='.header')
        self._fileobj_content_name = create_temporary_file(suffix='.content')
        self._fileobj_name = create_temporary_file()

        self._shared_date = SharedDate()
        self._string_builder = self._parent.strings_table_builder
开发者ID:Khan,项目名称:openpyxl,代码行数:13,代码来源:dump_worksheet.py

示例3: __init__

    def __init__(self, parent_workbook, title, sheet_codename, xml_source,
                 string_table, style_table):

        Worksheet.__init__(self, parent_workbook, title)
        self._sheet_codename = sheet_codename
        self._string_table = string_table
        self._style_table = style_table

        min_col, min_row, max_col, max_row = read_dimension(xml_source=self.xml_source)
        self.min_col = min_col
        self.min_row = min_row
        self.max_row = max_row
        self.max_col = max_col

        self._shared_date = SharedDate(base_date=parent_workbook.excel_base_date)
开发者ID:gabelula,项目名称:zapatistas,代码行数:15,代码来源:iter_worksheet.py

示例4: __init__

    def __init__(self, parent_workbook):

        Worksheet.__init__(self, parent_workbook)

        self._max_col = 0
        self._max_row = 0
        self._parent = parent_workbook
        self._fileobj_header = NamedTemporaryFile(mode='r+', prefix='openpyxl.', suffix='.header', delete=False)
        self._fileobj_content = NamedTemporaryFile(mode='r+', prefix='openpyxl.', suffix='.content', delete=False)
        self._fileobj = NamedTemporaryFile(mode='w', prefix='openpyxl.', delete=False)
        self.doc = XMLGenerator(self._fileobj_content, 'utf-8')
        self.header = XMLGenerator(self._fileobj_header, 'utf-8')
        self.title = 'Sheet'

        self._shared_date = SharedDate()
        self._string_builder = self._parent.strings_table_builder
开发者ID:chronossc,项目名称:openpyxl,代码行数:16,代码来源:dump_worksheet.py

示例5: IterableWorksheet

class IterableWorksheet(Worksheet):

    def __init__(self, parent_workbook, title, workbook_name,
            sheet_codename, xml_source, string_table):

        Worksheet.__init__(self, parent_workbook, title)
        self.archive = zipfile.ZipFile(workbook_name, 'r')
        self._workbook_name = workbook_name
        self._sheet_codename = sheet_codename
        self._string_table = string_table

        min_col, min_row, max_col, max_row = read_dimension(xml_source=self.xml_source)
        self.min_col = min_col
        self.min_row = min_row
        self.max_row = max_row
        self.max_col = max_col

        self._shared_date = SharedDate(base_date=parent_workbook.excel_base_date)

    @property
    def xml_source(self):
        worksheet_path = '%s/%s' % (PACKAGE_WORKSHEETS, self._sheet_codename)
        return self.archive.open(worksheet_path)

    @xml_source.setter
    def xml_source(self, value):
        """Base class is always supplied XML source, IteratableWorksheet obtains it on demand."""
        pass

    def __getitem__(self, key):
        if isinstance(key, slice):
            key = "{0}:{1}".format(key)
        return self.iter_rows(key)

    def iter_rows(self, range_string='', row_offset=0, column_offset=1):
        """ Returns a squared range based on the `range_string` parameter,
        using generators.

        :param range_string: range of cells (e.g. 'A1:C4')
        :type range_string: string

        :param row_offset: additional rows (e.g. 4)
        :type row: int

        :param column_offset: additonal columns (e.g. 3)
        :type column: int

        :rtype: generator

        """
        if range_string:
            min_col, min_row, max_col, max_row = get_range_boundaries(range_string, row_offset, column_offset)
        else:
            min_col = column_index_from_string(self.min_col)
            max_col = column_index_from_string(self.max_col) + 1
            min_row = self.min_row
            max_row = self.max_row + 6

        return self.get_squared_range(min_col, min_row, max_col, max_row)

    def get_squared_range(self, min_col, min_row, max_col, max_row):
        expected_columns = [get_column_letter(ci) for ci in xrange(min_col, max_col)]
        current_row = min_row

        style_properties = read_style_table(self.archive.read(ARC_STYLE))
        style_table = style_properties.pop('table')

        for row, cells in groupby(self.get_cells(min_row, min_col,
                                                 max_row, max_col),
                                  operator.attrgetter('row')):
            full_row = []
            if current_row < row:

                for gap_row in xrange(current_row, row):
                    dummy_cells = get_missing_cells(gap_row, expected_columns)
                    yield tuple([dummy_cells[column] for column in expected_columns])
                    current_row = row

            temp_cells = list(cells)
            retrieved_columns = dict([(c.column, c) for c in temp_cells])
            missing_columns = list(set(expected_columns) - set(retrieved_columns.keys()))
            replacement_columns = get_missing_cells(row, missing_columns)

            for column in expected_columns:
                if column in retrieved_columns:
                    cell = retrieved_columns[column]
                    if cell.style_id is not None:
                        style = style_table[int(cell.style_id)]
                        cell = cell._replace(number_format=style.number_format.format_code) #pylint: disable-msg=W0212
                    if cell.internal_value is not None:
                        if cell.data_type in Cell.TYPE_STRING:
                            cell = cell._replace(internal_value=unicode(self._string_table[int(cell.internal_value)])) #pylint: disable-msg=W0212
                        elif cell.data_type == Cell.TYPE_BOOL:
                            cell = cell._replace(internal_value=cell.internal_value == '1')
                        elif cell.is_date:
                            cell = cell._replace(internal_value=self._shared_date.from_julian(float(cell.internal_value)))
                        elif cell.data_type == Cell.TYPE_NUMERIC:
                            cell = cell._replace(internal_value=float(cell.internal_value))
                        elif cell.data_type in(Cell.TYPE_INLINE, Cell.TYPE_FORMULA_CACHE_STRING):
                            cell = cell._replace(internal_value=unicode(cell.internal_value))
#.........这里部分代码省略.........
开发者ID:fizikst,项目名称:connect_ws,代码行数:101,代码来源:iter_worksheet.py

示例6: Cell

class Cell(object):
    """Describes cell associated properties.

    Properties of interest include style, type, value, and address.

    """

    __slots__ = ("column", "row", "_value", "_data_type", "parent", "xf_index", "_hyperlink_rel", "_shared_date")

    ERROR_CODES = {"#NULL!": 0, "#DIV/0!": 1, "#VALUE!": 2, "#REF!": 3, "#NAME?": 4, "#NUM!": 5, "#N/A": 6}

    TYPE_STRING = "s"
    TYPE_FORMULA = "f"
    TYPE_NUMERIC = "n"
    TYPE_BOOL = "b"
    TYPE_NULL = "s"
    TYPE_INLINE = "inlineStr"
    TYPE_ERROR = "e"
    TYPE_FORMULA_CACHE_STRING = "str"

    VALID_TYPES = [
        TYPE_STRING,
        TYPE_FORMULA,
        TYPE_NUMERIC,
        TYPE_BOOL,
        TYPE_NULL,
        TYPE_INLINE,
        TYPE_ERROR,
        TYPE_FORMULA_CACHE_STRING,
    ]

    RE_PATTERNS = {
        "percentage": re.compile(r"^\-?[0-9]*\.?[0-9]*\s?\%$"),
        "time": re.compile(r"^(\d|[0-1]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$"),
        "numeric": re.compile(r"^-?([\d]|[\d]+\.[\d]*|\.[\d]+|[1-9][\d]+\.?[\d]*)((E|e)-?[\d]+)?$"),
    }

    def __init__(self, worksheet, column, row, value=None):
        self.column = column.upper()
        self.row = row
        # _value is the stored value, while value is the displayed value
        self._value = None
        self._hyperlink_rel = None
        self._data_type = self.TYPE_NULL
        if value:
            self.value = value
        self.parent = worksheet
        self.xf_index = 0
        self._shared_date = SharedDate(base_date=worksheet.parent.excel_base_date)

    @property
    def encoding(self):
        return self.parent.encoding

    def __repr__(self):
        return unicode("<Cell %s.%s>") % (self.parent.title, self.get_coordinate())

    def check_string(self, value):
        """Check string coding, length, and line break character"""
        # convert to unicode string
        if not isinstance(value, unicode):
            value = unicode(value, self.encoding)
        value = unicode(value)
        # string must never be longer than 32,767 characters
        # truncate if necessary
        value = value[:32767]
        # we require that newline is represented as "\n" in core,
        # not as "\r\n" or "\r"
        value = value.replace("\r\n", "\n")
        return value

    def check_numeric(self, value):
        """Cast value to int or float if necessary"""
        if not isinstance(value, NUMERIC_TYPES):
            try:
                value = int(value)
            except ValueError:
                value = float(value)
        return value

    def set_value_explicit(self, value=None, data_type=TYPE_STRING):
        """Coerce values according to their explicit type"""
        type_coercion_map = {
            self.TYPE_INLINE: self.check_string,
            self.TYPE_STRING: self.check_string,
            self.TYPE_FORMULA: self.check_string,
            self.TYPE_NUMERIC: self.check_numeric,
            self.TYPE_BOOL: bool,
        }
        try:
            self._value = type_coercion_map[data_type](value)
        except KeyError:
            if data_type not in self.VALID_TYPES:
                msg = "Invalid data type: %s" % data_type
                raise DataTypeException(msg)
        self._data_type = data_type

    def data_type_for_value(self, value):
        """Given a value, infer the correct data type"""
        if value is None:
#.........这里部分代码省略.........
开发者ID:demula,项目名称:pintracker,代码行数:101,代码来源:cell.py

示例7: DumpWorksheet

class DumpWorksheet(Worksheet):

    """
    .. warning::

        You shouldn't initialize this yourself, use :class:`openpyxl.workbook.Workbook` constructor instead, 
        with `optimized_write = True`.
    """

    def __init__(self, parent_workbook):

        Worksheet.__init__(self, parent_workbook)

        self._max_col = 0
        self._max_row = 0
        self._parent = parent_workbook
        self._fileobj_header = NamedTemporaryFile(mode='r+', prefix='openpyxl.', suffix='.header', delete=False)
        self._fileobj_content = NamedTemporaryFile(mode='r+', prefix='openpyxl.', suffix='.content', delete=False)
        self._fileobj = NamedTemporaryFile(mode='w', prefix='openpyxl.', delete=False)
        self.doc = XMLGenerator(self._fileobj_content, 'utf-8')
        self.header = XMLGenerator(self._fileobj_header, 'utf-8')
        self.title = 'Sheet'

        self._shared_date = SharedDate()
        self._string_builder = self._parent.strings_table_builder

    @property
    def filename(self):
        return self._fileobj.name

    def write_header(self):

        doc = self.header

        start_tag(doc, 'worksheet',
                {'xml:space': 'preserve',
                'xmlns': 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
                'xmlns:r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'})
        start_tag(doc, 'sheetPr')
        tag(doc, 'outlinePr',
                {'summaryBelow': '1', 
                'summaryRight': '1'})
        end_tag(doc, 'sheetPr')
        tag(doc, 'dimension', {'ref': 'A1:%s' % (self.get_dimensions())})
        start_tag(doc, 'sheetViews')
        start_tag(doc, 'sheetView', {'workbookViewId': '0'})
        tag(doc, 'selection', {'activeCell': 'A1',
                'sqref': 'A1'})
        end_tag(doc, 'sheetView')
        end_tag(doc, 'sheetViews')
        tag(doc, 'sheetFormatPr', {'defaultRowHeight': '15'})
        start_tag(doc, 'sheetData')

    def close(self):

        self._close_content()
        self._close_header()

        self._write_fileobj(self._fileobj_header)
        self._write_fileobj(self._fileobj_content)

        self._fileobj.close()

    def _write_fileobj(self, fobj):

        fobj.flush()
        fobj.seek(0)

        while True:
            chunk = fobj.read(4096)
            if not chunk:
                break
            self._fileobj.write(chunk)

        fobj.close()
        os.remove(fobj.name)

        self._fileobj.flush()

    def _close_header(self):
        
        doc = self.header
        #doc.endDocument()

    def _close_content(self):

        doc = self.doc
        end_tag(doc, 'sheetData')

        end_tag(doc, 'worksheet')
        #doc.endDocument()

    def get_dimensions(self):

        if not self._max_col or not self._max_row:
            return 'A1'
        else:
            return '%s%d' % (get_column_letter(self._max_col), (self._max_row))
            
    def append(self, row):
#.........这里部分代码省略.........
开发者ID:chronossc,项目名称:openpyxl,代码行数:101,代码来源:dump_worksheet.py

示例8: DumpWorksheet

class DumpWorksheet(Worksheet):

    """
    .. warning::

        You shouldn't initialize this yourself, use :class:`openpyxl.workbook.Workbook` constructor instead, 
        with `optimized_write = True`.
    """

    def __init__(self, parent_workbook, title):

        Worksheet.__init__(self, parent_workbook, title)

        self._max_col = 0
        self._max_row = 0
        self._parent = parent_workbook

        self._fileobj_header_name = create_temporary_file(suffix='.header')
        self._fileobj_content_name = create_temporary_file(suffix='.content')
        self._fileobj_name = create_temporary_file()

        self._shared_date = SharedDate()
        self._string_builder = self._parent.strings_table_builder

    @property
    def filename(self):
        return self._fileobj_name

    @property
    def _temp_files(self):

        return (self._fileobj_content_name,
                self._fileobj_header_name,
                self._fileobj_name)

    def _unset_temp_files(self):
        self._fileobj_header_name = None
        self._fileobj_content_name = None
        self._fileobj_name = None

    def write_header(self):

        fobj = get_temporary_file(filename=self._fileobj_header_name)
        doc = XMLGenerator(fobj, 'utf-8')

        start_tag(doc, 'worksheet',
                {'xml:space': 'preserve',
                'xmlns': 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
                'xmlns:r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'})
        start_tag(doc, 'sheetPr')
        tag(doc, 'outlinePr',
                {'summaryBelow': '1',
                'summaryRight': '1'})
        end_tag(doc, 'sheetPr')
        tag(doc, 'dimension', {'ref': 'A1:%s' % (self.get_dimensions())})
        start_tag(doc, 'sheetViews')
        start_tag(doc, 'sheetView', {'workbookViewId': '0'})
        tag(doc, 'selection', {'activeCell': 'A1',
                'sqref': 'A1'})
        end_tag(doc, 'sheetView')
        end_tag(doc, 'sheetViews')
        tag(doc, 'sheetFormatPr', {'defaultRowHeight': '15'})
        start_tag(doc, 'sheetData')

    def close(self):

        self._close_content()

        self._fileobj = get_temporary_file(filename=self._fileobj_name)

        self._write_fileobj(self._fileobj_header_name)
        self._write_fileobj(self._fileobj_content_name)

        self._fileobj.close()

    def _write_fileobj(self, fobj_name):

        fobj = get_temporary_file(filename=fobj_name)

        fobj.flush()
        fobj.seek(0)

        while True:
            chunk = fobj.read(4096)
            if not chunk:
                break
            self._fileobj.write(chunk)

        fobj.close()

        self._fileobj.flush()

    def _close_content(self):

        doc = self._get_content_generator()
        end_tag(doc, 'sheetData')

        end_tag(doc, 'worksheet')

    def get_dimensions(self):
#.........这里部分代码省略.........
开发者ID:grlownzl,项目名称:cdisc-share-template-checker,代码行数:101,代码来源:dump_worksheet.py

示例9: Cell

class Cell(object):
    """Describes cell associated properties.

    Properties of interest include style, type, value, and address.

    """
    __slots__ = ('column',
                 'row',
                 '_value',
                 '_data_type',
                 'parent',
                 'xf_index',
                 '_hyperlink_rel',
                 '_shared_date',
                 'merged')

    ERROR_CODES = {'#NULL!': 0,
                   '#DIV/0!': 1,
                   '#VALUE!': 2,
                   '#REF!': 3,
                   '#NAME?': 4,
                   '#NUM!': 5,
                   '#N/A': 6}

    TYPE_STRING = 's'
    TYPE_FORMULA = 'f'
    TYPE_NUMERIC = 'n'
    TYPE_BOOL = 'b'
    TYPE_NULL = 's'
    TYPE_INLINE = 'inlineStr'
    TYPE_ERROR = 'e'
    TYPE_FORMULA_CACHE_STRING = 'str'

    VALID_TYPES = [TYPE_STRING, TYPE_FORMULA, TYPE_NUMERIC, TYPE_BOOL,
                   TYPE_NULL, TYPE_INLINE, TYPE_ERROR, TYPE_FORMULA_CACHE_STRING]

    RE_PATTERNS = {
        'percentage': re.compile(r'^\-?[0-9]*\.?[0-9]*\s?\%$'),
        'time': re.compile(r'^(\d|[0-1]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$'),
        'numeric': re.compile(r'^-?([\d]|[\d]+\.[\d]*|\.[\d]+|[1-9][\d]+\.?[\d]*)((E|e)-?[\d]+)?$'),
        }

    def __init__(self, worksheet, column, row, value=None):
        self.column = column.upper()
        self.row = row
        # _value is the stored value, while value is the displayed value
        self._value = None
        self._hyperlink_rel = None
        self._data_type = self.TYPE_NULL
        if value:
            self.value = value
        self.parent = worksheet
        self.xf_index = 0
        self._shared_date = SharedDate(base_date=worksheet.parent.excel_base_date)
        self.merged = False

    @property
    def encoding(self):
        return self.parent.encoding

    def __repr__(self):
        return unicode("<Cell %s.%s>") % (self.parent.title, self.get_coordinate())

    def check_string(self, value):
        """Check string coding, length, and line break character"""
        # convert to unicode string
        if not isinstance(value, unicode):
            value = unicode(value, self.encoding)
        value = unicode(value)
        # string must never be longer than 32,767 characters
        # truncate if necessary
        value = value[:32767]
        # we require that newline is represented as "\n" in core,
        # not as "\r\n" or "\r"
        value = value.replace('\r\n', '\n')
        return value

    def check_numeric(self, value):
        """Cast value to int or float if necessary"""
        if not isinstance(value, NUMERIC_TYPES):
            try:
                value = int(value)
            except ValueError:
                value = float(value)
        return value

    def set_value_explicit(self, value=None, data_type=TYPE_STRING):
        """Coerce values according to their explicit type"""
        type_coercion_map = {
            self.TYPE_INLINE: self.check_string,
            self.TYPE_STRING: self.check_string,
            self.TYPE_FORMULA: self.check_string,
            self.TYPE_NUMERIC: self.check_numeric,
            self.TYPE_BOOL: bool, }
        try:
            self._value = type_coercion_map[data_type](value)
        except KeyError:
            if data_type not in self.VALID_TYPES:
                msg = 'Invalid data type: %s' % data_type
                raise DataTypeException(msg)
#.........这里部分代码省略.........
开发者ID:DHLabs,项目名称:keep_isn,代码行数:101,代码来源:cell.py

示例10: DumpWorksheet

class DumpWorksheet(Worksheet):

    """
    .. warning::

        You shouldn't initialize this yourself, use :class:`openpyxl.workbook.Workbook` constructor instead, 
        with `optimized_write = True`.
    """

    def __init__(self, parent_workbook):

        Worksheet.__init__(self, parent_workbook)

        self._max_col = 0
        self._max_row = 0
        self._parent = parent_workbook

        self._fileobj_header_name = create_temporary_file(suffix=".header")
        self._fileobj_content_name = create_temporary_file(suffix=".content")
        self._fileobj_name = create_temporary_file()

        self.title = "Sheet"

        self._shared_date = SharedDate()
        self._string_builder = self._parent.strings_table_builder

    @property
    def filename(self):
        return self._fileobj_name

    @property
    def _temp_files(self):

        return (self._fileobj_content_name, self._fileobj_header_name, self._fileobj_name)

    def _unset_temp_files(self):
        self._fileobj_header_name = None
        self._fileobj_content_name = None
        self._fileobj_name = None

    def write_header(self):

        fobj = get_temporary_file(filename=self._fileobj_header_name)
        doc = XMLGenerator(fobj, "utf-8")

        start_tag(
            doc,
            "worksheet",
            {
                "xml:space": "preserve",
                "xmlns": "http://schemas.openxmlformats.org/spreadsheetml/2006/main",
                "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
            },
        )
        start_tag(doc, "sheetPr")
        tag(doc, "outlinePr", {"summaryBelow": "1", "summaryRight": "1"})
        end_tag(doc, "sheetPr")
        tag(doc, "dimension", {"ref": "A1:%s" % (self.get_dimensions())})
        start_tag(doc, "sheetViews")
        start_tag(doc, "sheetView", {"workbookViewId": "0"})
        tag(doc, "selection", {"activeCell": "A1", "sqref": "A1"})
        end_tag(doc, "sheetView")
        end_tag(doc, "sheetViews")
        tag(doc, "sheetFormatPr", {"defaultRowHeight": "15"})
        start_tag(doc, "sheetData")

    def close(self):

        self._close_content()

        self._fileobj = get_temporary_file(filename=self._fileobj_name)

        self._write_fileobj(self._fileobj_header_name)
        self._write_fileobj(self._fileobj_content_name)

        self._fileobj.close()

    def _write_fileobj(self, fobj_name):

        fobj = get_temporary_file(filename=fobj_name)

        fobj.flush()
        fobj.seek(0)

        while True:
            chunk = fobj.read(4096)
            if not chunk:
                break
            self._fileobj.write(chunk)

        fobj.close()

        self._fileobj.flush()

    def _close_content(self):

        doc = self._get_content_generator()
        end_tag(doc, "sheetData")

        end_tag(doc, "worksheet")
#.........这里部分代码省略.........
开发者ID:OCHA-DAP,项目名称:dataproxy,代码行数:101,代码来源:dump_worksheet.py


注:本文中的openpyxl.shared.date_time.SharedDate类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。