[英]Python xlwings not working in different excel file
I started a project in xlwings called MakeReport.我在 xlwings 中启动了一个名为 MakeReport 的项目。 So I have two files: 1) MakeReport.xlsm and 2) MakeReport.py.
所以我有两个文件:1)MakeReport.xlsm 和 2)MakeReport.py。 My python functions in MakeReport.py write data to a third excel sheet called Template.xlsm.
我在 MakeReport.py 中的 python 函数将数据写入名为 Template.xlsm 的第三个 Excel 工作表。 They were all in the same directory.
他们都在同一个目录中。 I then wrote macros in Makereport.xlsm that triggered specific MakeReport.py functions.
然后我在 Makereport.xlsm 中编写了触发特定 MakeReport.py 函数的宏。 Then in Template.xlsm I made buttons and assigned them to the respective macros from MakeReport.xlsm.
然后在 Template.xlsm 中,我制作了按钮并将它们分配给 MakeReport.xlsm 中的相应宏。 Everything at this point is working fine.
此时一切正常。
I wanted the Template.xlsm file to be in a new directory so I took it out of the folder with my MakeReport files.我希望 Template.xlsm 文件位于一个新目录中,所以我将它从我的 MakeReport 文件所在的文件夹中取出。 The macros are still linked.
宏仍然链接。 When I run my macros directly in MakeReport.xlsm, everything works fine and the correct data populates to Template.xlsm.
当我直接在 MakeReport.xlsm 中运行我的宏时,一切正常并且正确的数据填充到 Template.xlsm。 However, when I try and use the buttons in Template.xlsm, I get the following error: No module named 'MakeReport'
但是,当我尝试使用 Template.xlsm 中的按钮时,出现以下错误: No module named 'MakeReport'
Why is this happening?为什么会这样?
Here is my VBA code:这是我的 VBA 代码:
Sub TestRun()
RunPython("import MakeReport;MakeReport.test_run()")
End Sub
Figured it out.弄清楚了。 Just need to create a xlwings.config sheet in the Template.xlsm workbook and then specify the PYTHONPATH.
只需要在 Template.xlsm 工作簿中创建一个 xlwings.config 表,然后指定 PYTHONPATH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.