win32com Excel.Application无法再打开文档 [英] 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()
我收到以下错误消息:
"AttributeError: '<win32com.gen_py.Microsoft Excel 15.0 Object Library.Workbooks instance at 0x20920640>' object has no attribute 'open'"
有人可以帮忙吗?
谢谢!
推荐答案
奇怪的是,我遇到了与@AndyDo相同的问题.我最初用于访问Excel应用程序的代码停止工作.
Strangely enough, I ran into the same issue as @AndyDo. The code I originally used to access the Excel Application stopped working.
原始(无效):
** 注意-很明显,我使用的示例中的大小写与我不匹配.但是,我不确定为什么以前的代码能正常工作.
来源:如何打开受密码保护的Excel使用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.
修订(工作中):
来源- Python Excel迷你食谱
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.
这篇关于win32com Excel.Application无法再打开文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!