简体   繁体   English

使用 openpyxl 加载/保存后合并单元格被破坏

[英]merged cells are broken after load/save using openpyxl

I am opening a .xlsm file using openpyxl abd updated some cells in it and then saved as .xlsm file.Now when I open the saved file I see that the cells which were merged in the original file are broken in new file.我正在使用 openpyxl abd 打开一个 .xlsm 文件,更新其中的一些单元格,然后另存为 .xlsm 文件。现在当我打开保存的文件时,我看到合并到原始文件中的单元格在新文件中被破坏了。

the code which i am using is-我正在使用的代码是-

from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('Excel.xlsm',read_only=False ,keep_vba=True)
ws = wb['K0 Reg Patch Util']
ws.cell(row=42,column=3).value = 25
ws.cell(row=43,column=3).value = 30      
ws.cell(row=44,column=3).value = 24
wb.save('Test.xlsm')

Even on simple opening and saving file with openpyxl merged columns borders in the original file are broken.即使在原始文件中使用 openpyxl 合并列的简单打开和保存文件也会被破坏。 I have searched many times regarding this problem but none of the solutions were satisfying.关于这个问题,我已经搜索了很多次,但没有一个解决方案令人满意。 I even came across a monkeypatch script which is to be included in the script after including the openpyxl library.The source for the script is-我什至遇到了一个monkeypatch脚本,该脚本在包含openpyxl库之后将包含在脚本中。脚本的源代码是-

https://bitbucket.org/openpyxl/openpyxl/issues/365/styling-merged-cells-isnt-working https://bitbucket.org/openpyxl/openpyxl/issues/365/styling-merged-cells-isnt-working

The monkeypatch will overwrite the definition of merged cell from that present in the library. Monkeypatch 将覆盖库中存在的合并单元的定义。

can somebody tell me how to include this patch in the script and What does "self" means in the script.有人可以告诉我如何在脚本中包含这个补丁以及“self”在脚本中是什么意思。

I ran into a similar issue.我遇到了类似的问题。 But for me this only occurs with "protected" excel files.但对我来说,这只发生在“受保护的”excel 文件中。 Removing that protection worked for me.取消这种保护对我有用。 No more "broken" merged cells.不再有“破碎”的合并单元格。 At least for files with .XLSX extension, I did not test .XLSM.至少对于扩展名为 .XLSX 的文件,我没有测试 .XLSM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM