win32com Excel.Application can't open documents anymore

丶灬走出姿态 提交于 2019-12-08 07:00:50

问题


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!


回答1:


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.




回答2:


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




回答3:


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!



来源:https://stackoverflow.com/questions/41492210/win32com-excel-application-cant-open-documents-anymore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!