有没有办法使用 Python(openpyxl) 复制合并的单元格?
Is there a way to copy merged cells using Python(openpyxl)?
我正在尝试使用 Python 编写代码以重复复制特定范围的 Excel 文件。
像下面的图像文件一样,通过在 sheet 上放置几列来复制相同的内容,现有内容位于相同的 sheet.
enter image description here
尽管是入门级别的能力,起初我认为它会很容易实现。我已经通过下面的代码实现了我的大部分计划,除了合并的单元格
import openpyxl
from openpyxl import Workbook
wb = openpyxl.load_workbook('DSD.xlsx')
ws = wb.worksheets[3]
from openpyxl.utils import range_boundaries
min_col, min_row, max_col, max_row = range_boundaries('I1')
for row, row_cells in enumerate(wb['2'], min_row):
for column, cell in enumerate(row_cells, min_col):
# Copy Value from Copy.Cell to given Worksheet.Cell
ws.cell(row=row, column=column).value = cell.value
ws.cell(row=row, column=column)._style = cell._style
wb.save('DSD.xlsx')
enter image description here
可是,我找了几天,想了想,还是不知道如何合并合并的单元格。除此之外,我什至不知道这在技术上是否可行。
我今天想到的方法是在sheet中拉出合并单元格的列表,然后从每个合并单元格的坐标中仅添加更多列以添加合并到相同范围的命令.
但是,由于我拉出了如下所示的合并单元格列表,所以我想不出要做什么。
有办法吗?
import openpyxl
from openpyxl import Workbook
wb=openpyxl.load_workbook('merged.xlsx')
ws=wb.active
Merged_Cells = ws.merged_cell_ranges
a = Merged_Cells[0]
print(ws.merged_cell_ranges)
print(a)
[<MergedCellRange B2:C3>, <MergedCellRange B9:C9>, <MergedCellRange B13:B14>]
B2:C3
对应a的值B2:C3似乎是一个特殊值,其中a.replace(~~)即使该值似乎是“[=36=”也不起作用]".
真的没有办法吗?
您需要了解 CellRanges 的工作原理,因为您需要计算每个范围的偏移量,然后使用原始单元格的尺寸为新单元格创建一个新的 MergedCellRange。
以下应该可以让您了解如何执行此操作。
from openpyxl.worksheet.cell_range import CellRange
area = CellRange("A1:F13") # area being copied
for mcr in ws.merged_cells:
if mcr.coord not in area:
continue
cr = CellRange(mcr.coord)
cr.shift(col_shift=10)
ws.merge_cells(cr.coord)
我正在尝试使用 Python 编写代码以重复复制特定范围的 Excel 文件。
像下面的图像文件一样,通过在 sheet 上放置几列来复制相同的内容,现有内容位于相同的 sheet.
enter image description here
尽管是入门级别的能力,起初我认为它会很容易实现。我已经通过下面的代码实现了我的大部分计划,除了合并的单元格
import openpyxl
from openpyxl import Workbook
wb = openpyxl.load_workbook('DSD.xlsx')
ws = wb.worksheets[3]
from openpyxl.utils import range_boundaries
min_col, min_row, max_col, max_row = range_boundaries('I1')
for row, row_cells in enumerate(wb['2'], min_row):
for column, cell in enumerate(row_cells, min_col):
# Copy Value from Copy.Cell to given Worksheet.Cell
ws.cell(row=row, column=column).value = cell.value
ws.cell(row=row, column=column)._style = cell._style
wb.save('DSD.xlsx')
enter image description here
可是,我找了几天,想了想,还是不知道如何合并合并的单元格。除此之外,我什至不知道这在技术上是否可行。
我今天想到的方法是在sheet中拉出合并单元格的列表,然后从每个合并单元格的坐标中仅添加更多列以添加合并到相同范围的命令.
但是,由于我拉出了如下所示的合并单元格列表,所以我想不出要做什么。 有办法吗?
import openpyxl
from openpyxl import Workbook
wb=openpyxl.load_workbook('merged.xlsx')
ws=wb.active
Merged_Cells = ws.merged_cell_ranges
a = Merged_Cells[0]
print(ws.merged_cell_ranges)
print(a)
[<MergedCellRange B2:C3>, <MergedCellRange B9:C9>, <MergedCellRange B13:B14>]
B2:C3
对应a的值B2:C3似乎是一个特殊值,其中a.replace(~~)即使该值似乎是“[=36=”也不起作用]".
真的没有办法吗?
您需要了解 CellRanges 的工作原理,因为您需要计算每个范围的偏移量,然后使用原始单元格的尺寸为新单元格创建一个新的 MergedCellRange。
以下应该可以让您了解如何执行此操作。
from openpyxl.worksheet.cell_range import CellRange
area = CellRange("A1:F13") # area being copied
for mcr in ws.merged_cells:
if mcr.coord not in area:
continue
cr = CellRange(mcr.coord)
cr.shift(col_shift=10)
ws.merge_cells(cr.coord)