将 pandas 数据帧写入 xlsm 文件(Excel 启用宏)
Write pandas dataframe to xlsm file (Excel with Macros enabled)
将 pandas.DataFrame
写入 .xlsx
格式的 Excel 工作簿非常简单:
import pandas as pd
df = pd.DataFrame({'firstColumn' : [5, 2, 0, 10, 4], 'secondColumn' : [9, 8, 21, 3, 8]})
print(df)
df.to_excel('test.xlsx')
给出:
firstColumn secondColumn
0 5 9
1 2 8
2 0 21
3 10 3
4 4 8
和对应的Excel文件。
是否也可以将 DataFrame
写入 .xlsm
Excel 文件?这实际上或多或少与 .xlsx
相同,但可以在文件中存储 VBA 宏。我需要这个,因为我想在创建文件后插入 运行 一个 VBA 宏。
但是,在常规 xlsx
文件上尝试此操作时,我在弹出窗口中收到以下错误消息:
The following features cannot be saved in macro-free workbooks: VB project.
To save a file with these features, click No, and then choose a macro-enabled file type in the File Type list.
To continue saving as macro-free workbook, click Yes.
然后我可以手动选择将文件另存为 .xlsm
,这将包含我的宏。但是,我更愿意在没有额外步骤的情况下自动执行此操作。
documentation for the to_excel
method 表明这应该是可能的(参见 engine
参数)。但是,我不明白如何启用它。
当我简单地将输出文件名更改为 *.xlsm
时,会创建一个 .xlsx
文件,该文件的名称为 .xlsm
。当我尝试打开它时,我得到
Excel cannot open the file 'myFilename.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
如果我手动将扩展名更改为 .xlsx
,我可以再次打开它。
关于this part of the pandas
documentation:
openpyxl
: This includes stable support for OpenPyxl 1.6.1 up to but not including 2.0.0, and experimental support for OpenPyxl 2.0.0 and later.`
我的 Openpyxl
版本是 1.8.6。更新到2.1.4也没有解决问题。也没有将 XlsxWriter
从 0.63 更新到 0.6.6。
按照建议使用 df.to_excel('test.xlsx', engine='openpyxl')
也没有解决问题。
Pandas 要求工作簿名称以 .xls
或 .xlsx
结尾。它使用扩展来选择要使用的 Excel 引擎。
你可以传递一个临时名称,然后用这样的东西覆盖它:
import pandas as pd
df = pd.DataFrame({'First' : [5, 2, 0, 10, 4],
'Second' : [9, 8, 21, 3, 8]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
workbook.filename = 'test.xlsm'
# !! Won't load in Excel !!
writer.save()
这将创建一个 Excel 文件,扩展名为 .xlsm
。
但是,由于名为 "extension hardening" Excel 的功能,它不会打开此文件,因为它知道它不包含宏并且实际上不是 xlsm
文件。 (即您在上面报告的 Excel 错误。)
您可以通过从真实的 xlsm 文件中提取 VbaProject.bin
宏文件并将其插入到新文件中来使用最新版本的 XlsxWriter 解决此问题:
import pandas as pd
df = pd.DataFrame({'First' : [5, 2, 0, 10, 4],
'Second' : [9, 8, 21, 3, 8]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
workbook.filename = 'test.xlsm'
workbook.add_vba_project('./vbaProject.bin')
writer.save()
有关详细信息,请参阅 XlsxWriter 文档的 Working with VBA Macros 部分。
将 pandas.DataFrame
写入 .xlsx
格式的 Excel 工作簿非常简单:
import pandas as pd
df = pd.DataFrame({'firstColumn' : [5, 2, 0, 10, 4], 'secondColumn' : [9, 8, 21, 3, 8]})
print(df)
df.to_excel('test.xlsx')
给出:
firstColumn secondColumn
0 5 9
1 2 8
2 0 21
3 10 3
4 4 8
和对应的Excel文件。
是否也可以将 DataFrame
写入 .xlsm
Excel 文件?这实际上或多或少与 .xlsx
相同,但可以在文件中存储 VBA 宏。我需要这个,因为我想在创建文件后插入 运行 一个 VBA 宏。
但是,在常规 xlsx
文件上尝试此操作时,我在弹出窗口中收到以下错误消息:
The following features cannot be saved in macro-free workbooks: VB project.
To save a file with these features, click No, and then choose a macro-enabled file type in the File Type list.
To continue saving as macro-free workbook, click Yes.
然后我可以手动选择将文件另存为 .xlsm
,这将包含我的宏。但是,我更愿意在没有额外步骤的情况下自动执行此操作。
documentation for the to_excel
method 表明这应该是可能的(参见 engine
参数)。但是,我不明白如何启用它。
当我简单地将输出文件名更改为 *.xlsm
时,会创建一个 .xlsx
文件,该文件的名称为 .xlsm
。当我尝试打开它时,我得到
Excel cannot open the file 'myFilename.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
如果我手动将扩展名更改为 .xlsx
,我可以再次打开它。
关于this part of the pandas
documentation:
openpyxl
: This includes stable support for OpenPyxl 1.6.1 up to but not including 2.0.0, and experimental support for OpenPyxl 2.0.0 and later.`
我的 Openpyxl
版本是 1.8.6。更新到2.1.4也没有解决问题。也没有将 XlsxWriter
从 0.63 更新到 0.6.6。
按照建议使用 df.to_excel('test.xlsx', engine='openpyxl')
也没有解决问题。
Pandas 要求工作簿名称以 .xls
或 .xlsx
结尾。它使用扩展来选择要使用的 Excel 引擎。
你可以传递一个临时名称,然后用这样的东西覆盖它:
import pandas as pd
df = pd.DataFrame({'First' : [5, 2, 0, 10, 4],
'Second' : [9, 8, 21, 3, 8]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
workbook.filename = 'test.xlsm'
# !! Won't load in Excel !!
writer.save()
这将创建一个 Excel 文件,扩展名为 .xlsm
。
但是,由于名为 "extension hardening" Excel 的功能,它不会打开此文件,因为它知道它不包含宏并且实际上不是 xlsm
文件。 (即您在上面报告的 Excel 错误。)
您可以通过从真实的 xlsm 文件中提取 VbaProject.bin
宏文件并将其插入到新文件中来使用最新版本的 XlsxWriter 解决此问题:
import pandas as pd
df = pd.DataFrame({'First' : [5, 2, 0, 10, 4],
'Second' : [9, 8, 21, 3, 8]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
workbook.filename = 'test.xlsm'
workbook.add_vba_project('./vbaProject.bin')
writer.save()
有关详细信息,请参阅 XlsxWriter 文档的 Working with VBA Macros 部分。