Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python openpyxl保存xlsm文件在打开时出错_Python_Xml_Excel_Vba_Openpyxl - Fatal编程技术网

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

Python openpyxl保存xlsm文件在打开时出错,python,xml,excel,vba,openpyxl,Python,Xml,Excel,Vba,Openpyxl,我有一个打开xlsm文件和xlsx文件的脚本。它使用xlsx中的数据修改xlsm,然后保存xlsm文件。当我在脚本运行后打开xlsm文件时,我得到一个错误,如图所示 然后,该文件工作正常,但我得到一个XML错误,如下所示: 我使用的代码是: import openpyxl destwb = openpyxl.load_workbook(filename="C:\\627 Data\\winphy\\071-000-022-00 627 data.xlsm", read_only=False,

我有一个打开xlsm文件和xlsx文件的脚本。它使用xlsx中的数据修改xlsm,然后保存xlsm文件。当我在脚本运行后打开xlsm文件时,我得到一个错误,如图所示

然后,该文件工作正常,但我得到一个XML错误,如下所示:

我使用的代码是:

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")

在这个问题中,我遇到了类似的情况,并从这个解决方案中拼凑了大量可回收的代码:

显然,在打开和保存xslm时,openpyxl不会读取或保留xslm的所有神奇宏部分。由于文件采用zip格式,因此解决方案:

  • 将您的工作保存为xlsx
  • 将原始xlsm作为zip打开并提取关键部分
  • 使用保存的xlsx和上述关键部分中的数据创建一个新的zip
  • 将其重命名为xlsm
我获取了示例代码,将其转换为工作簿.save()的可用替代品,修复了丢失的文件(可能是Excel自原始解决方案以来所做的更改),在混合中添加了压缩和创建备份文件。愿这能满足你的需要

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')

在这个问题中,我遇到了类似的情况,并从这个解决方案中拼凑了大量可回收的代码:

显然,在打开和保存xslm时,openpyxl不会读取或保留xslm的所有神奇宏部分。由于文件采用zip格式,因此解决方案:

  • 将您的工作保存为xlsx
  • 将原始xlsm作为zip打开并提取关键部分
  • 使用保存的xlsx和上述关键部分中的数据创建一个新的zip
  • 将其重命名为xlsm
我获取了示例代码,将其转换为工作簿.save()的可用替代品,修复了丢失的文件(可能是Excel自原始解决方案以来所做的更改),在混合中添加了压缩和创建备份文件。愿这能满足你的需要

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')

修复后的摘要部分未关闭,“摘要”丢失Excel文件错误很不幸不是很有用,没有该文件,我们无法帮助您。我不确定如何将该文件附加到线程,但一旦打开该文件并再次保存,错误就消失了。@mickNeill您似乎将其保存为xls。我不确定
openpyxl
中的
save
是否可以这样保存xlsm文件!修复后的摘要部分未关闭,“摘要”丢失Excel文件错误很不幸不是很有用,没有该文件,我们无法帮助您。我不确定如何将该文件附加到线程,但一旦打开该文件并再次保存,错误就消失了。@mickNeill您似乎将其保存为xls。我不确定
openpyxl
中的
save
是否可以这样保存xlsm文件!