简体   繁体   English

Python openpyxl 保存 xlsm 文件在打开时出错

[英]Python openpyxl saving xlsm file gets error when opening

I have a script that opens an xlsm file and an xlsx file.我有一个脚本可以打开一个 xlsm 文件和一个 xlsx 文件。 It modifies the xlsm with data from the xlsx, then saves the xlsm file.它使用来自 xlsx 的数据修改 xlsm,然后保存 xlsm 文件。 When i open that xlsm file after the script is run, I get an error shown in the image.当我在脚本运行后打开该 xlsm 文件时,出现图像中显示的错误。 在此处输入图片说明

The file then works fine but I get an XML error shown below:该文件然后工作正常,但我收到如下所示的 XML 错误: 在此处输入图片说明

The code I am using is:我正在使用的代码是:

import openpyxl
destwb = openpyxl.load_workbook(filename="C:\\627 Data\\winphy\\071-000-022-00 627 data.xlsm", read_only=False, keep_vba=True)

.....Code.....

destwb.save(filename="C:\\627 Data\\winphy\\071-000-022-00 627 data2.xlsm")

I ran into something similar and pieced largely recycled code from this solution by Joost in this question: How to save XLSM file with Macro, using openpyxl我遇到了类似的事情,并通过拼凑很大程度上回收代码从该解决方案的Joost这样一个问题: 如何保存XLSM文件与宏,使用openpyxl

Apparently, openpyxl doesn't read or preserve all of the magic macro parts of an xslm when opening and saving.显然,openpyxl 在打开和保存时不会读取或保留 xslm 的所有魔术宏部分。 Since the files are in a zip format, the solution:由于文件是 zip 格式,因此解决方案:

  • Saves your work as an xlsx将您的工作保存为 xlsx
  • Opens the original xlsm as a zip and extracts the key parts以zip格式打开原始xlsm并提取关键部分
  • Creates a new zip with the data from your saved xlsx and the above key parts使用您保存的 xlsx 中的数据和上述关键部分创建一个新的 zip
  • Renames that as a xlsm将其重命名为 xlsm

I took the sample code, turned it into a usable replacement for workbook.save(), fixed a missing file (likely Excel change since the original solution), added zip compression and creation of a backup file to the mix.我拿了示例代码,把它变成了 workbook.save() 的可用替代品,修复了一个丢失的文件(自原始解决方案以来可能是 Excel 更改),添加了 zip 压缩并创建了一个备份文件。 May this do what you need.愿这做你需要的。

def saveXlsm(wb, xlsmname):
'''Some crazy workaround to fix what openpyxl cannot when recreating an xlsm file. 
   Use as replacement for workbook.save()
'''
import zipfile
from shutil import copyfile
from shutil import rmtree

# Unzip original and tmp into separate dirs
PAD = os.getcwd()
wb.save('tmp.xlsx')
with zipfile.ZipFile(xlsmname, 'r') as z:
    z.extractall('./xlsm/')
with zipfile.ZipFile('tmp.xlsx', 'r') as z:
    z.extractall('./xlsx/')
# copy pertinent left out macro parts into tmp
copyfile('./xlsm/[Content_Types].xml','./xlsx/[Content_Types].xml')
copyfile('./xlsm/xl/_rels/workbook.xml.rels','./xlsx/xl/_rels/workbook.xml.rels')
copyfile('./xlsm/xl/vbaProject.bin','./xlsx/xl/vbaProject.bin')
copyfile('./xlsm/xl/sharedStrings.xml','./xlsx/xl/sharedStrings.xml')
# create a new tmp zip to rebuild the xlsm
z = zipfile.ZipFile('tmp.zip', 'w', zipfile.ZIP_DEFLATED)
# put all the parts back into the new Frankenstein
os.chdir('./xlsx')
for root, dirs, files in os.walk('./'):
        for file in files:
            z.write(os.path.join(root, file))
z.close()
os.chdir(PAD)
# humanize Frankenstein
bakname = xlsmname + '.bak'
if os.access(bakname, os.W_OK):
    os.remove(bakname)
os.rename(xlsmname, bakname)
os.rename('tmp.zip', xlsmname)
#clean
rmtree('./xlsm/')
rmtree('./xlsx/')
os.remove('./tmp.xlsx')

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

相关问题 使用 openpyxl 打开.xlsm 文件时出现问题 - Issue while opening .xlsm file using openpyxl 保存文件时出现 Python OpenPyXL 错误 - Python OpenPyXL Errors When Saving File 使用 Python (Openpyxl) 保存 Excel 文件 - Saving Excel file with Python (Openpyxl) 打开xlsx文件时,openpyxl库StopIteration错误 - openpyxl library StopIteration error when opening xlsx file 使用 Openpyxl 保存启用宏的 Excel 工作簿 (.xlsm) 时出现问题。 Excel 引发错误并开始修复工作簿 - Problem saving Macro Enabled Excel Workbook (.xlsm) using Openpyxl. Excel throws an error and starts Repairing the workbook 使用 Openpyxl 打开 xlsx 文件会给出 KeyError '$A$' - Python - Opening xlsx file with Openpyxl gives KeyError '$A$' - Python 为什么打开 .xlsm 而不是 .xls 时会出现 python xlrd 错误 - Why is python xlrd errors when opening a .xlsm instead of .xls 如何使用宏保存 XLSM 文件,使用 openpyxl - How to save XLSM file with Macro, using openpyxl 在 XLSM 文件中调用 Python 脚本时,Windows 上出现“没有名为 xlwings 的模块”错误 - “no module named xlwings” error on Windows when calling the Python script within XLSM file 当保存Excel文件(openpyxl),然后尝试使Excel在Mac上打开时,出现“权限被拒绝”错误 - When saving excel file (openpyxl) and then trying to make excel open it on mac get `Permission denied` error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM