Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

win32com Excel.Application can't open documents anymore

This was working last week but for some reason it stopped working today, maybe because of the new year?

def remove_strikethroughs(xlsx):
    excel = win32com.client.Dispatch('Excel.Application')

    xl = pd.ExcelFile(xlsx)
    sheet_names = xl.sheet_names
    for sheet in sheet_names:
        if any(tab in sheet for tab in tabs_used):
            #print (sheet)

            wb = excel.Workbooks.Open(xlsx)
            ws = wb.WorkSheets(sheet)
            for cell in ws.Range('A5:B150'):
                if cell.Font.Strikethrough == True:
                    cell.value = '[MDU]' + str(cell)
            wb.Save()
            wb.Close()
    excel.Visible = True
    excel.DisplayAlerts = True
    excel.Application.Quit()

I get the following error message:

"AttributeError: '<win32com.gen_py.Microsoft Excel 15.0 Object Library.Workbooks instance at 0x20920640>' object has no attribute 'open'"

Can someone please help?

Thanks!

like image 576
Andy Do Avatar asked Oct 17 '25 02:10

Andy Do


2 Answers

I came looking for this error when a code ran just fine for the first time but threw an exception "AttributeError: Excel.Application.Workbooks" when I ran it again.

This is not a tech solution, this is just a stupidity (which is very common) filter.

I had an Excel file open in the background, and the code closes the Excel application (of course, we should close the workbook only). When Excel was closed, the save dialogue box popped up in the background for my file which was open. And when I ran the same code again, Excel Application became a problem because it has not been released yet from Python.

Maybe, just maybe, you are also doing something similar. High five!! you are not the only one!

like image 143
Rupak Gupta Avatar answered Oct 19 '25 15:10

Rupak Gupta


Strangely enough, I ran into the same issue as @AndyDo. The code I originally used to access the Excel Application stopped working.

Original (non-working):

** Note - It's clear that I didn't match case from the example I used. However, I'm not sure why the code worked without error previously.

Source: How to open a password protected excel file using python?

import win32com.client as w3c

xlapp = w3c.Dispatch('Excel.Application')
xlwb = xlapp.Workbooks.open(file.xlsx, False, True, None, file_password)
xlsheet = xlwb.WorkSheets('my_sheet_name')

Then, I updated the case as seen in the code below to rectify the Attribute Error.

Revised (working):

Source - Python Excel Mini Cookbook

import win32com.client as w3c

xlapp = w3c.gencache.EnsureDispatch('Excel.Application')
xlwb = xlapp.Workbooks.Open(file.xlsx, False, True, None, file_password)
xlsheet = xlwb.Worksheets('my_sheet_name')

I'm wondering if another open workbook in which the formula bar was activated affected the issue. I'll have to do more investigating.

like image 39
datalifenyc Avatar answered Oct 19 '25 15:10

datalifenyc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!