python - Xlwings:从 Excel 文件运行 python
问题描述
首先,我应该说这段代码在昨天没问题(这意味着它正在运行并且没有问题)。
我不知道为什么,但是: 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 从 excel 调用 python
3 - 所以,我尝试执行 LoadSAP.py,我在 C://Documents 上制作了一个副本,我在 Jupyter 笔记本文件上运行了该函数,奇迹般地 Excel 现在可以识别 LoadSAP。Ps.:我需要知道如何解决这个问题,因为我对 loadMaps.py 函数有同样的看法。
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 - 在我按照第 4 项的要求进行更改后,在这一行代码上(这读取了其他 Excel 文件以推送一些数据)
gerder = read_excel(path_gerder + file_gerder, sheet_name='Base')
我有两个问题:
有时它会返回:
'ascii' codec can't encode character '\xea' in position 56: ordinal not in range(128)
有时它会返回:
No such file: 'https://.../Documents/.../gerder file.xls'
Ps.:如果我使用browser上文件的链接,它已下载。
我有很多单数问题,一周前它运行良好:Excel 识别所有 *.py,程序直接读取 OneDrive 的所有 Excel 文件等。因此,如果您有任何想法或建议,我愿意尝试。
解决方案
解决方案:
首先,就像 Felix Zumstein 评论的那样:
1 - 在 Excel 文件上设置 PYTHONPATH:如何通过 xlwings 在 Excel 上设置 PYTHONPATH
2 - 我不知道具体原因,但 xlwings 无法识别 OneDrive 路径,例如'C:/.../Documents/.../Excel file.xlsm'
. 因此,出现此错误:
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)
解决方案:我使用了'https://.../Documents/.../Excel file.xlsm'
任何 xlwings 函数的路径。对于其他功能,我使用了路径:'C:/.../Documents/.../Excel file.xlsm'
.
推荐阅读
- r - 分组变量上的ggplot和ggsignif错误
- python - TypeError:float()参数必须是python的字符串或数字
- vue.js - 从 index.html 导入 SCSS 文件不起作用
- content-management-system - TYPO3 9.5.2 Slug:多语言:找不到页面 404 异常,如果不存在页面翻译
- ajax - Zend2:通过 ajax 动态加载部分(例如标签)
- json - 我的片段无法显示来自 json url 的 recyclerview 和数据
- node.js - Node.js 命令行变量无法识别
- c# - 隐藏 xaml 元素,但也能够在 UWP 中使用 RenderTargetBitmap 进行渲染
- python - 我想创建一个表并使用 python 使用行和列选择值
- javascript - 离子日期选择器本地日期格式?