[英]How to write in merged cell in Excel using `openpyxl` library?
I am using openpyxl
library to write in existing Excel file in separate cells.我正在使用openpyxl
库在单独的单元格中写入现有的 Excel 文件。
How do I write some text in Excel merged cell?如何在 Excel 合并单元格中写入一些文本?
ERROR AttributeError: 'MergedCell' object attribute 'value' is read-only
错误AttributeError: 'MergedCell' object attribute 'value' is read-only
when cells are merged:合并单元格时:
CODE:代码:
wb = openpyxl.load_workbook(filename=src)
for row in df_short.itertuples():
ws = wb[row.sheet]
try:
cell = 'N'+str(row.id)
ws[cell] = '=HYPERLINK("%s","#%s")' % (row.txt_path, row.txt)
Use the following code where ws is the sheet object.使用以下代码,其中 ws 是工作表对象。
ws.cell(cells).value = 'Whatever you want it to be'
replace cells with the top-left cell of the merged block.用合并块的左上角单元格替换单元格。 I usually keep this as rows and columns.我通常将其保留为行和列。 So B1 would be represented as row = 1, column = 2.所以 B1 将表示为 row = 1, column = 2。
After the value =值后 =
Put any string or integer of what you want to place inside the cell.将您想要放置在单元格内的任何字符串或整数。
ws.merged_cells
would return a list contains all the merged cells' location, such as this: ws.merged_cells
将返回一个包含所有合并单元格位置的列表,例如:
In [6]: mgs = ws.merged_cells
In [7]: mgs
Out[7]: <MultiCellRange [B1:C1 D1:J1 K1:L1 M1:N1]>
In [8]: for ms in mgs:
...: print(ms)
...:
B1:C1
D1:J1
K1:L1
M1:N1
If you want to set a new value to a merged cell, you have to unmerge it first, then change the value of the first sub-cell, finally merge them again. 如果要为合并的单元格设置新值,则必须先取消合并,然后更改第一个子单元格的值,最后再次合并它们。 A full example: 一个完整的例子:
In [1]: import openpyxl
In [2]: wb = openpyxl.load_workbook('demo.xlsx')
In [3]: ws = wb.active
In [4]: m_cells = ws.merged_cells
In [5]: m_cells
Out[5]: <MultiCellRange [B1:C1 D1:J1 K1:L1 M1:N1]>
In [6]: coo = m_cells.ranges[0].coord
In [7]: coo
Out[7]: 'B1:C1'
In [8]: ws.unmerge_cells(coo)
In [9]: ws['B1'] = 'New Value'
In [10]: ws.merge_cells(coo)
Additional info: 附加信息:
ws.merged_cells
would return a MultiCellRange
type object but not a list, you should use .ranges
attribute to get a list that contains CellRange
type object. ws.merged_cells
将返回MultiCellRange
类型的对象,但不会返回列表,您应该使用.ranges
属性来获取包含CellRange
类型的对象的列表。
The CellRange
type object could not be used in ws.unmerge_cells()
, you have to use .coord
attribute to get the str
type which ws.unmerge_cells(str)
could calls. 无法在ws.unmerge_cells()
使用CellRange
类型的对象,您必须使用.coord
属性来获取ws.unmerge_cells(str)
可以调用的str
类型。
A wrapped function 包装功能
def mod_cell(merged_cell, new_value):
"""
Set new value to a merged_cell
:param merged_cell: Merged cell to modify, ws.merged_cells.ranges[x]
:param new_value: New value
:return:
"""
# Unmerge
coo = merged_cell.coord
ws.unmerge_cells(coo)
# Set new value to the first sub-cell
first_cell = ws.cell(row=merged_cell.left[0][0], column=merged_cell.left[0][1])
first_cell.value = new_value
# Merge again
ws.merge_cells(coo)
If you are open to using another excel library, your issue is not encountered with xlwings
which lets Python control excel.如果您愿意使用另一个 excel 库,那么xlwings
不会遇到您的问题,它可以让 Python 控制 excel。 Differences between xlwings vs openpyxl Reading Excel Workbooks . xlwings 与 openpyxl 阅读 Excel 工作簿之间的差异。 Note xlwings
requires Excel to be installed on your machine while openpyxl
does not.注意xlwings
需要在您的计算机上安装 Excel,而openpyxl
不需要。
The below sets the value of already merged cell A1 with no problems.下面设置已经合并的单元格 A1 的值,没有问题。
import xlwings as xw
wb = xw.Book('myproject.xlsm')
sh = wb.sheets[0]
sh.range('A1').value = 'hello'
Result:结果:
I had this exact error and solution for conda environment within python3.5 was我在 python3.5 中对 conda 环境有这个确切的错误和解决方案是
conda install -c conda-forge openpyxl=2.5.14 -y
Similarly for pip3 environments:同样对于 pip3 环境:
pip3 install openpyxl==2.5.14 -y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.