添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
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?

Does the user running the code have access to files at those locations. Also check whether the files are zipfile using the zipfile library. – Charlie Clark Nov 23, 2015 at 15:15 I'm stuck with this problem in Ubuntu server. Works fine on windows, but when I deploy to my server, I got this. I've tried the answers of crussell, ericksonla and Manish Chaudhary without success. My .xlsx file is not password protected. – C. S. F. Junior May 7, 2020 at 13:06 How do I do that? I don't see a paramete in load_workbook that would set it to binary mode. – Omega Dec 2, 2020 at 11:06

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!'?

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]!="~"
                Because I was using openpxl on my mac and received the same error, openpyxl is a python library, sooooooo.......
– BryanE
                Apr 5, 2019 at 13:31
                Hi lucida lee, welcome at StackOverflow! Your answer might already have been given by [will johnson|stackoverflow.com/a/50475794/5488275] . What you can do, is upvote his answer and, in this case, edit his answer (since it can be improved). That is better than adding a duplicate answer :)
– Nander Speerstra
                Mar 20, 2020 at 8:58

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?

Yeah, that is the source of your problem. Openpyxl does not support password protected files. – crussell Nov 23, 2015 at 18:46

I had the same issue and thank you all for your hints. My problem was that the excel .xlsx file I was opening was corrupted. Hence openpyxl couldn't open. I had to recreate the file.

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.

I've tried this one without success. On windows 10 my project works fine. But when I deploy to Ubuntu 18.04 server, I got the same error message. Is there another version that could solve this? I've tried 2.6.3 and the last one 3.0.3 – C. S. F. Junior May 7, 2020 at 13:14 there is no other versions in my knowledge till now, but now try using xlwings, i find it quite workable – Manish Chaudhary May 8, 2020 at 5:53 It's only for MAC or Windows. I got this when I tried to install: OSError: xlwings requires an installation of Excel and therefore only works on Windows and macOS. To enable the installation on Linux nevertheless, do: export INSTALL_ON_LINUX=1; pip install xlwings – C. S. F. Junior May 8, 2020 at 13:20

I had the same problem when i directly changed CSV to XLSX in the file directory. Even though you create the new XLSX it does not include the required meta data.

Manual Suggestion is to open the CSV and export to XLSX within excel itself. If the file is to large to open in Excel itself you can use the below commands.

import pandas as pd
read_file = pd.read_csv (r'FY2021_All_Contracts_Full_20220510_3.csv', low_memory = False)
read_file.to_excel (r'FY2021_All_Contracts_Full_20220510_3', index = None, header=True)

This will programatically convert it to Excel if it is to large to be opened/ you don't want to do it manually.