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?
–
–
–
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]!="~"
–
–
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?
–
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 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.