简体   繁体   English

Xlwings:从 Excel 文件运行 python

[英]Xlwings: run python from Excel file

First, I should say that this code was ok last wee (that means it was running and no problems).首先,我应该说这段代码在昨天没问题(这意味着它正在运行并且没有问题)。

I don't know why, but: 1 - I have a Excel file on OneDrive.我不知道为什么,但是: 1 - 我在 OneDrive 上有一个 Excel 文件。 This file have the xlwings imported and this function on VBA:此文件已导入 xlwings 和 VBA 上的此 function:

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

2 - The LoadSAP.py was placed on the same path of the Excel file. 2 - LoadSAP.py 被放置在 Excel 文件的相同路径上。

This week, I have the issue:本周,我有一个问题:

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

It's similar to xlwings module.py call python from excel它类似于xlwings module.py 从 excel 调用 python

3 - So, I tried to execute the LoadSAP.py, I maked a copy on the C://Documents, I runned the function on Jupyter notebook file, and miraculously the Excel now recognize the LoadSAP. 3 - 所以,我尝试执行 LoadSAP.py,我在 C://Documents 上制作了一个副本,我在 Jupyter 笔记本文件上运行了 function,现在奇迹般地识别了 ZC1D81AF589SAP1ED8FDCE9。 Ps.: I need to know how to fix this issue because I have the same to the loadMaps.py function. Ps.:我需要知道如何解决这个问题,因为我对 loadMaps.py function 有同样的问题。

4 - Now, running on the Excel, a new problem appears: the program doesn't recognize the path of OneDrive, and it suggest to change to the link https. 4 - 现在,在Excel上运行,出现新问题:程序无法识别OneDrive的路径,建议更改为链接https。

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

5 - After I did the change like demanded on item 4, on this line code (this read na other Excel file to push some data) 5 - 在我按照第 4 项的要求进行更改后,在此行代码上(这读取其他 Excel 文件以推送一些数据)

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

I have 2 issues:我有两个问题:

Sometimes it returns:有时它会返回:

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

Sometimes it returns:有时它会返回:

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

Ps.: If I use the link to the file on the brownser, it's downloaded. Ps.:如果我使用browser上文件的链接,它已下载。

I have many singulars issues and it worked well just one week ago: the Excel recognized all the *.py, the program readed all the Excel file directly of OneDrive, etcs etcs.我有很多单数问题,一周前它运行良好:Excel 识别所有 *.py,程序直接读取 OneDrive 等的所有 Excel 文件等。 So if you have any idea or suggestion, I open to try.因此,如果您有任何想法或建议,我愿意尝试。

The solution:解决方案:

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

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

2 - I don't know exactly why, but xlwings don't recognize the OneDrive path like 'C:/.../Documents/.../Excel file.xlsm' . 2 - 我不知道具体原因,但 xlwings 无法识别 OneDrive 路径,例如'C:/.../Documents/.../Excel file.xlsm' So, this error appears:因此,出现此错误:

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

At the same time, the funcion read_excel of Pandas and others functions don't recognize the OneDrive path like 'https://.../Documents/.../Excel file.xlsm' .同时Pandas等函数的read_excel函数无法识别'https://.../Documents/.../Excel file.xlsm'这样的OneDrive路径。 And this error should appear:并且应该出现这个错误:

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

Solution: I used the path like 'https://.../Documents/.../Excel file.xlsm' to any xlwings functions.解决方案:我对任何 xlwings 函数都使用了'https://.../Documents/.../Excel file.xlsm'之类的路径。 For the others functions, I used the path: 'C:/.../Documents/.../Excel file.xlsm' .对于其他功能,我使用了路径: 'C:/.../Documents/.../Excel file.xlsm'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM