Andy Do
Andy Do

Reputation: 99

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!

Upvotes: 4

Views: 40282

Answers (5)

Hussein Bani Odeh
Hussein Bani Odeh

Reputation: 59

I face this issue "AttributeError: Excel.Application.Workbooks" and to fix it, just close any Excel sheet opened in the background and it should be fixed.

Upvotes: 2

Michael Cohen
Michael Cohen

Reputation: 1

  1. I had similar Problem - While running the following code :
  from win32com.client import Dispatch    
  import os

  list_of_files=os.listdir(path)
  xl = Dispatch('Excel.Application')
  xlWb = xl.Workbooks.Open(file)

It used to work then today it failed while trying to run this code.

  1. Solution was to :
    • Add : import win32com.client as w3c
    • Change : xl = Dispatch('Excel.Application') to xl = w3c.Dispatch('Excel.Application')

Upvotes: 0

Rupak Gupta
Rupak Gupta

Reputation: 61

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!

Upvotes: 6

datalifenyc
datalifenyc

Reputation: 2248

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.

Upvotes: 3

David Zemens
David Zemens

Reputation: 53663

There is no open method, it's Open. Python is case-sensitive :)

Upvotes: -1

Related Questions