Even with 'r' prepended to filename, openp

2019-03-01 06:21发布

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

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'

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.

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

1条回答
放我归山
2楼-- · 2019-03-01 07:01

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.

查看更多
登录 后发表回答