Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while converting from xls to xlsx using win32com. This program throws error if i work with other excel sheet

import pandas as pd
import os
import win32com.client
import win32com.client.gencache
fname = "C:\\Users\\prashanth\\Desktop\\student.xls"
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(fname)
wb.SaveAs(fname+"x", FileFormat = 51)    
wb.Close()                               
excel.Application.Quit()

Traceback (most recent call last): File "c:\users\prashanth\appdata\local\programs\python\python36-32\lib\site-packages\win32com\client\gencache.py", line 536, in EnsureDispatch ti = disp.oleobj.GetTypeInfo() pywintypes.com_error: (-2147418111, 'Call was rejected by callee.', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "dailyreports2.py", line 6, in excel = win32com.client.gencache.EnsureDispatch('Excel.Application') File "c:\users\prashanth\appdata\local\programs\python\python36-32\lib\site-packages\win32com\client\gencache.py", line 547, in EnsureDispatch raise TypeError("This COM object can not automate the makepy process - please run makepy manually for this object") TypeError: This COM object can not automate the makepy process - please run makepy manually for this object

like image 631
john Avatar asked Aug 22 '17 07:08

john


People also ask

Why can’t I open xlsx file in Excel?

“Excel cannot open the file ‘ (filename)’.xlsx because the file format for file extension is not valid” is a common error that any user can face while opening a .xlsx file. With the help of the above methods, this error can easily be resolved without affecting the integrity of the file.

How to convert XLS file to excel file?

When you open older format files, you will see "Compatibility Mode" in the title bar after the file name. You can use the "convert" function. Click on the Office button, then Convert. You can also try opening the .xls file in Excel > then use the Save As command to save as an "Excel Workbook".

How to fix Excel file extension error?

[Solution] Excel File Extension Error 1 Change the Extension of The Worksheet 2 Many times, this error occurs due to the unsupported file extension of your file ... 3 open MS Excel and go to " File ". 4 Now, select " Export " and choose " Change File Type ". 5 Check whether it is showing the option of .xlsx or not ... See More....

Is the Excel file not compatible with your version of Excel?

The Excel file is not compatible with the version of MS Excel you’re using. There are various Excel errors and ways to fix them, and this error can also be fixed easily. In this article, we have mentioned some of the useful methods to solve this error.


2 Answers

I got the same error after these lines:

import win32com.client
import win32com.client.gencache
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')

A machine restart worked for me.

like image 129
Eugene Lycenok Avatar answered Oct 18 '22 03:10

Eugene Lycenok


It has to do with the automated creation of folder of python scripts related to the com object. C:\Users\[username]\AppData\Local\Temp\gen_py

When you EnsureDispatch, that folder is created every time through makepy. When you Dispatch, it references a existing folder.

I'm dealing with the same issue and sometimes deleting the contents of 3.7 in the gen_py folder works. I'm still wrapping my head around it but am busy at work

More info can be found in this thread

like image 41
Jack Avatar answered Oct 18 '22 04:10

Jack