[英]Losing macros when adding data to excel spread sheet in python
I am trying to generate reports for a web project. 我正在尝试为Web项目生成报告。 The reports require I fill an excel spreadsheet with data and then run a macro to generate the details.
报告要求我用数据填充excel电子表格,然后运行宏以生成详细信息。
Using the following code 使用以下代码
from xlrd import open_workbook
from xlutils.copy import copy
import StringIO
contents=open_workbook("filename.xls")
cloneContents = copy(contents)
#code for adding data to cloneContents here
stream = StringIO()
cloneContents.save(stream)
#this is from a web app so this is stream sent to the user to download
return stream
This all works fine except the cloned file appears to lose all the macros. 除克隆的文件似乎丢失所有宏外,所有其他方法都工作正常。 I had a look around for other solutions, but the only one I could see involved actually loading the file in excel which I can't do because this is a linux server.
我环顾了其他解决方案,但我只能看到实际上涉及在excel中加载文件的方法,因为这是linux服务器,所以我做不到。 Does anyone have any ideas what could be done?
有谁知道可以做什么? Thanks
谢谢
This cannot be done with xlrd and xlutils. xlrd和xlutils无法做到这一点。
But it can be done using this branch https://bitbucket.org/amorris/openpyxl/overview of openpyxl. 但是可以使用该分支https://bitbucket.org/amorris/openpyxl/openpyxl的概述来完成。
The key line is contents = load_workbook('filename.xlsm', keep_vba = True);
关键是
contents = load_workbook('filename.xlsm', keep_vba = True);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.