即使在文件名前加上"r",openpyxl也无法在Windows上保存 [英] Even with 'r' prepended to filename, openpyxl not able to save on Windows

查看:311
本文介绍了即使在文件名前加上"r",openpyxl也无法在Windows上保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OS X上,openpyxl.save()对于名为"all_done.xslx"的文件正常工作.

On OS X, openpyxl.save() is working fine for a file called 'all_done.xslx'.

但是,在Windows上尝试时,结果为:

Yet when it is attempted on Windows, it results in:

c:\Users\Tony\Desktop\ROI>python roi_cut6.py > log.txt
Traceback (most recent call last):
File "roi_cut6.py", line 373, in <module>
main()
File "roi_cut6.py", line 369, in main
processSource(wb, 'Gemini', totalGeminiSpends, geminiRevenues)
File "roi_cut6.py", line 269, in processSource
wb.save(r'all_done.xlsx')
File "C:\Python27\lib\site-packages\openpyxl\workbook\workbook.py", line 298,
in save
save_workbook(self, filename)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 198, in sa
ve_workbook
writer.save(filename, as_template=as_template)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 180, in sa
ve
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Python27\lib\zipfile.py", line 756, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 22] invalid mode ('wb') or filename: 'all_done.xlsx'

帖子和这篇帖子建议添加文件名前的r.我已经尝试过了(可以在上面的日志中看到).该文件位于当前目录中,因此反斜杠或正斜杠都不是问题.该文件可在OS X和Windows上的Excel中打开.还有什么可能是问题?不幸的是,我本人无法访问Windows mahcine,因此无法直接在那里进行故障排除.

This post and this post suggest adding r before the filename. This I've already tried (as can be seen in the log above). And the file is in the current directory, so it's not an issue with the backslashes or forward slashes. The file is openable in Excel on both OS X and Windows. What else could be the problem? Unfortunately I don't have access to the Windows mahcine myself so cannot troubleshoot directly there.

保存到的文件最初是直接在Excel中创建还是直接由openpyxl创建,这有什么区别吗?

Does it make any difference whether the file being saved to was originally created directly in Excel or rather with openpyxl?

推荐答案

这可能是权限错误,与openpyxl无关.使用r的想法是完全错误的.使用os.path组成一个路径,无论运行脚本的任何用户都具有写权限.

This is probably a permissions error and nothing to do with openpyxl. The idea of using r is totally wrong. Use os.path to compose a path where whatever user is running the script has the permission to write.

这篇关于即使在文件名前加上"r",openpyxl也无法在Windows上保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆