这在上周工作但由于某种原因它今天停止工作,也许是因为新年?
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'"
有人可以帮忙吗?
谢谢!
原文由 Andy Do 发布,翻译遵循 CC BY-SA 4.0 许可协议
当代码第一次运行良好时,我来寻找这个错误,但当我再次运行它时抛出异常“AttributeError: Excel.Application.Workbooks”。
这不是技术解决方案,这只是一个愚蠢的(很常见的)过滤器。
我在后台打开了一个 Excel 文件,代码关闭了 Excel 应用程序(当然,我们应该只关闭工作簿)。关闭 Excel 时,在后台弹出我打开的文件的保存对话框。当我再次运行相同的代码时,Excel 应用程序成为了一个问题,因为它还没有从 Python 中发布。
也许,只是也许,你也在做类似的事情。举手击掌!!你不是唯一一个!