openpyxl.worksheet.merge 源代码
# Copyright (c) 2010-2020 openpyxl
from openpyxl.descriptors.serialisable import Serialisable
from openpyxl.descriptors import (
Integer,
Sequence,
)
from openpyxl.cell.cell import MergedCell
from openpyxl.styles.borders import Border
from .cell_range import CellRange
[文档]class MergeCell(CellRange):
tagname = "mergeCell"
ref = CellRange.coord
__attrs__ = ("ref",)
def __init__(self,
ref=None,
):
super(MergeCell, self).__init__(ref)
def __copy__(self):
return self.__class__(self.ref)
[文档]class MergeCells(Serialisable):
tagname = "mergeCells"
count = Integer(allow_none=True)
mergeCell = Sequence(expected_type=MergeCell, )
__elements__ = ('mergeCell',)
__attrs__ = ('count',)
def __init__(self,
count=None,
mergeCell=(),
):
self.mergeCell = mergeCell
@property
def count(self):
return len(self.mergeCell)
[文档]class MergedCellRange(CellRange):
"""
MergedCellRange stores the border information of a merged cell in the top
left cell of the merged cell.
The remaining cells in the merged cell are stored as MergedCell objects and
get their border information from the upper left cell.
"""
def __init__(self, worksheet, coord):
self.ws = worksheet
super().__init__(range_string=coord)
self.start_cell = None
self._get_borders()
def _get_borders(self):
"""
If the upper left cell of the merged cell does not yet exist, it is
created.
The upper left cell gets the border information of the bottom and right
border from the bottom right cell of the merged cell, if available.
"""
# Top-left cell.
self.start_cell = self.ws._cells.get((self.min_row, self.min_col))
if self.start_cell is None:
self.start_cell = self.ws.cell(row=self.min_row, column=self.min_col)
# Bottom-right cell
end_cell = self.ws._cells.get((self.max_row, self.max_col))
if end_cell is not None:
self.start_cell.border += Border(right=end_cell.border.right,
bottom=end_cell.border.bottom)
def __contains__(self, coord):
return coord in CellRange(self.coord)
def __copy__(self):
return self.__class__(self.ws, self.coord)