4

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!

Andy Do
  • 99
  • 1
  • 2
  • 14
  • There is no call to a lowercase "open" anywhere in there, are you sure you posted the code you are using? – Oliver Jan 06 '17 at 06:05

5 Answers5

5

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!

Rupak Gupta
  • 51
  • 1
  • 1
3

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.

datalifenyc
  • 2,028
  • 1
  • 20
  • 18
  • Your revised working code worked for me while looping through .xlsb files that were password protected. I did this blindly since I can't really find any documentation - 1) thank you for this answer, 2) What are the arguments doing/referring to in xlapp.workbooks.open? File, False, False, None, Password didn't work - but file, False, True, None, password did the trick. – Hatt May 29 '18 at 14:20
1

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.

0
  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')
Ruli
  • 2,592
  • 12
  • 30
  • 40
-1

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

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • 1
    Hello, this worked last week with "open", after I changed it to "Open" it has this error message: AttributeError: '' object has no attribute 'WorkSheets' – Andy Do Jan 05 '17 at 19:14
  • 1
    @AndyDo: that's probably because the attribute is named `Worksheets`, not `WorkSheets`. See https://msdn.microsoft.com/en-us/library/office/ff835542.aspx. – Luke Woodward Jan 05 '17 at 19:41
  • @AndyDo that is the exact same error. Remember, **python is case-sensitive**, so `WorkSheets != Worksheets`. – David Zemens Jan 05 '17 at 20:39