可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to open both an xlsx file and an xlsm file both give me the same error
badzipfile: file is not a zip file
here is what I'm typing:
import openpyxl
wb=openpyxl.load_workbook('c:\\users\\me\\documents\\filename.xlsm', keep_vba=True)
wb2=openpyxl.load_workbook('c:\\users\\me\\documents\\filename2.xlsx')
both load_workbook
commands result in the same error. They both exist at that location. why am I getting this error?
回答1:
The same problem occurred to me, and then I noticed the following:
When I created the .xlsx file from file manager, by creating new document with .xlsx format, I had the same error. But when I created the file with some spreadsheet software (libreoffice-calc) in my case, it just worked fine. I hope this may help.
回答2:
It's because you created empty .xlsx with no metadata which is an empty file with no cells formatting. use exel or equivalent spreadsheet software to save empty file in that directory, which will create an xlsx file with cell formating, after saving try to load file using openpyxl
回答3:
I'm late to the party, but I received the same error, but for different reasons.
On my mac, when I cut and pasted the .xlsx file into the directory I desired, it didn't actually place the file itself, it placed a symbolic link. That, for some reason, would raise the same error. I remedied this by opening the file, and using "save as".
回答4:
My hunch is that either you openpyxl version is not the latest (2.3.1) or that there is a problem with your source file. To upgrade to the newest version to openpyxl, use:
pip install openpyxl --upgrade
Is the source file encrypted at all?
回答5:
This also happened to me when I pointed openpyxl to an alias of the file instead of the original file.
回答6:
I did something really stupid and got the same error.
Basically, today was my first time trying this and I got it to work with the 'Automate' example and then tried my Excel.
Didn't work!
Took me a while to realize the error was due to having workbook password protected.
The error doesn't match that at all, but when I removed the protection from the workbook, it worked!
What can I say but 'duh' and 'yeah!'?
回答7:
If you call openpyxl.load_workbook
with file-like object, be sure to open it in binary mode.
回答8:
I have been facing this error for a while and so i just uninstalled openpyxl and reinstall version 2.6.3 and it worked well.
This might help you too, no need to change anything just run these commands using pip
pip uninstall openpyxl
pip install openpyxl==2.6.3
Hope it helps you.
回答9:
The xlsx file may have password and you are not granted to access.
Delete the password and you can use openpyxl.load_workbook() to access it.
回答10:
I had the same problem. It is because the file I was trying to open in Python was already running. You need to check if the file you are trying to open in Python is not running in tasks.
回答11:
The XLSX or XLS or XLSM files you are trying to open are excel recovery files start with "~". you can check by:
for file in path.glob('*.xlsx'):print(file)
you can skip those files by checking,get filename from full path:
filename=str(filename).split("\\")[-1:][0]
checking if the filename starts with "~" as all recovery files will start with "~"
if filename[0]!="~"