Mac 安装 xlwings
1、环境介绍
-
我的环境介绍
MacBook M1 + Python3 + PyCharm2021.1 + xlwings 0.23.1 + MicroSoft Excel 365
-
一段代码
执行这段代码
if __name__ == '__main__': import xlwings as xw wb = xw.Book('Book1.xlsx') sheet = wb.sheets['template'].copy(name='report') sheet.render_template(title='A Demo!', table=[[1, 2], [3, 4]]) wb.to_pdf()
2、PyCharm使用xlwings
-
PyCharm 使用
xlwings
的问题The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Users/kylewhite/dev/pythonProject/demo_project/file/test.py", line 4, in <module> wb = xw.Book('Book1.xlsx') File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/xlwings/main.py", line 539, in __init__ for wb in app.books: File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/xlwings/main.py", line 64, in __iter__ for impl in self.impl: File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/xlwings/_xlmac.py", line 235, in __iter__ n = len(self) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/xlwings/_xlmac.py", line 200, in __len__ return self.app.xl.count(each=kw.workbook) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/aeosa/appscript/reference.py", line 518, in __call__ raise CommandError(self, (args, kargs), e, self.AS_appdata) from e appscript.reference.CommandError: Command failed: OSERROR: -1743 MESSAGE: The user has declined permission. COMMAND: app(pid=75376).count(each=k.workbook)
-
解决方案
不用试了目前没有解决方案,解决方案的更新可以看这里,我已经提交了这个问题
重点是:使用终端可以执行。当我第一次使用终端执行时,它向我要了权限,当我给了权限后,就可以执行了。所以我认为
Pycharm
没有权限导致的,Pycharm
甚至没有向我要权限
3、终端执行问题
-
xlwings的安装文档
-
第一个报错:raise ImportError(“Couldn’t find ‘cryptography’, a dependency of xlwings PRO.”) from None
错误:
解决:
pip3 install "xlwings[pro]"
-
第二个报错:xlwings.LicenseError: Invalid license key.
错误:
解决:
去申请一个 xlwings 的 License,填写信息后你将获取收到一封邮件,然后直接将命令复制并运行
申请地址:https://www.xlwings.org/trial
xlwings license update -k gAAAAABgnQwSIbTc_PuPNDDuPi0ehvxZsnSMe4tDA_9otuRQelH27J6gK6j5xhVrw5l3Ge5-unsT_-WC0YdMNEk3D