Xlwings:来自 Excel 文件的 运行 python

Xlwings: run python from Excel file

首先,我应该说这段代码上次没问题(这意味着它 运行 没有问题)。

我不知道为什么,但是: 1 - 我在 OneDrive 上有一个 Excel 文件。这个文件导入了 xlwings 并且这个函数在 VBA:

Sub CallSAP()
'Run the Python Script
RunPython ("import LoadSAP; LoadSAP.ScriptSAP()")    
End Sub

2 - LoadSAP.py 被放置在与 Excel 文件相同的路径上。

这周,我遇到了问题:

---------------------------
Error
---------------------------
Traceback (most recent call last):

  File "<string>", line 1, in <module>

ModuleNotFoundError: No module named 'LoadSAP'

Press Ctrl+C to copy this message to the clipboard.
---------------------------
OK   
---------------------------

类似于xlwings module.py call python from excel

3 - 所以,我尝试执行 LoadSAP.py,我在 C://Documents 上做了一个副本,我在 Jupyter notebook 文件上运行了这个函数,现在奇迹般地 Excel识别 LoadSAP。 Ps.: 我需要知道如何解决这个问题,因为我对 loadMaps.py 函数有同样的看法。

4 - 现在Excel上的运行出现了新的问题:程序无法识别OneDrive的路径,建议换成link https.

OSError: [WinError 123] The syntax of the file name, directory name, or volume label is incorrect: 'https://.../Documents/.../Excel file.xlsm'

5 - 在我按照项目 4 的要求进行更改后,在这一行代码中(读取其他 Excel 文件以推送一些数据)

gerder = read_excel(path_gerder + file_gerder, sheet_name='Base')

我有 2 个问题:

有时 returns:

'ascii' codec can't encode character '\xea' in position 56: ordinal not in range(128)

有时 returns:

No such file: 'https://.../Documents/.../gerder file.xls'

Ps.: 如果我使用 link 到 brownser 上的文件,它会被下载。

我有很多单数问题,一周前它运行良好:Excel 识别所有 *.py,程序直接读取 OneDrive 等的所有 Excel 文件。所以如果你有什么想法或者建议,我打开试试。

解决方法:

首先,就像 Felix Zumstein 评论的那样:

1 - 在 Excel 文件上设置 PYTHONPATH:How to set the PYTHONPATH on Excel by xlwings

2 - 我不知道为什么,但 xlwings 不能像 'C:/.../Documents/.../Excel file.xlsm' 那样识别 OneDrive 路径。因此,出现此错误:

OSError: [WinError 123] The syntax of the file name, directory name, or volume label is incorrect: 'https://.../Documents/.../Excel file.xlsm'

同时,Pandas的函数read_excel等函数无法识别OneDrive路径,如'https://.../Documents/.../Excel file.xlsm'。并且应该出现此错误:

'ascii' codec can't encode character '\xea' in position 56: ordinal not in range(128)

解决方案:我对任何 xlwings 函数都使用了类似 'https://.../Documents/.../Excel file.xlsm' 的路径。对于其他函数,我使用了路径:'C:/.../Documents/.../Excel file.xlsm'.