Issue in using win32com to access Excel file
If I want to make sure that python starts a new excel instance (for instance to access macros in my xlsm files), I use
xlApp = win32com.client.DispatchEx("Excel.Application")
That way I can close the application without hurting the instance that was already open.
Otherwise, I could simply use
xlApp = win32com.client.Dispatch("Excel.Application")
Does that work for you?
python-win32com excel com model started generating errors
I had the same issue and I resolved it by following the instructions here: https://mail.python.org/pipermail/python-win32/2007-August/006147.html
Deleting the gen_py output directory and re-running makepy SUCCEEDS
and subsequently the test application runs OK again.So the symptom is resolved, but any clues as to how this could have
happened. This is a VERY long running application (think 24x7 for
years) and I'm concerned that whatever caused this might occur again.
To find the output directory, run this in your python console / python session:
import win32com
print(win32com.__gen_path__)
Based on the exception message in your post, the directory you need to remove will be titled '00020813-0000-0000-C000-000000000046x0x1x9'. So delete this directory and re-run the code. And if you're nervous about deleting it (like I was) just cut the directory and paste it somewhere else.
I have no idea why this happens nor do I know how to prevent it from happening again, but the directions in the link I provided seemed to work for me.
Problems with win32com
SOLUTION! AMAZINGLY:
I could not believe how this error was obscure and we found the solution looking for similar problems with the equivalent .NET code:
To fix it, create a folder named 'Desktop' under 'C: \ Windows \ SysWOW64 \ config \ systemprofile \' on 64-bit architecture or 'C: \ Windows \ System32 \ config \ systemprofile \' on 32-bit servers.
WORKED WITH PERFECTION.
(Source: StackOverflow answers in English: excel access denied with win32 python pywin32)
Python: error opening excel workbook with win32com
I think your problem comes from:
wb = excel.Workbooks(r'C:\Users\<username>\Documents\<more file path>\test.xls').Open()
The path to your file should be in Open()
such as:
wb = excel.Workbooks.Open(r'C:\Users\<username>\Documents\<more file path>\test.xls')
Note also that there is no () after Workbooks
as in your second code. This way works for me.
Related Topics
How to Install Colorama in Python
Opencv - Saving Images to a Particular Folder of Choice
How to Deal With Certificates Using Selenium
Replacing Special Characters in a List in Python
Pandas: Difference Between Pivot and Pivot_Table. Why Is Only Pivot_Table Working
Python/Pandas: Convert Month Int to Month Name
Python Does Not Match Format '%Y-%M-%Dt%H:%M:%S%Z.%F'
How to Compile Multiple Python Files into Single .Exe File Using Pyinstaller
How to Embed Matplotlib Graph in Django Webpage
Python3: How to Print Out User Input String and Print It Out Separated by a Comma
How to Print Numbers in a List That Are Less Than a Variable. Python
Incorrect Column Alignment When Printing Table in Python Using Tab Characters
Implement K-Fold Cross Validation in Mlpclassification Python
Regex to Match Digits and At Most One Space Between Them
Python 2D List Performance, Without Numpy
Pandas Rank by Multiple Columns
How to Find the Most Common Element in the List of List in Python