excel2img在第二次使用时出现错误“对象没有属性“父级””

时间:2019-10-22 13:57:19

标签: python-3.x

使用excel2img库捕获工作表的特定部分时,出现以下错误。

AttributeError: '<win32com.gen_py.Microsoft Excel 16.0 Object Library.Range instance at 0x2697515534376>' object has no attribute 'parent'

第一次使用相同的代码即可。但此后它停止了工作。只是为了说明一点,我使用的是与以前相同的Office版本,我之间所做的唯一更改是我安装了几个库。我不知道是否是由于这个原因。

我试图将excel版本从.xlsx更改为.xls。但是然后它返回错误

"OSError: Failed to open snap.xlsx".

下面是我正在使用的代码:

import excel2img

excel2img.export_img("snap.xls","alarm_BB1.png","Sheet1!A2:Q20")
excel2img.export_img("snap.xls","VSWR_BB1.png","","Sheet2!A2:Q50")
excel2img.export_img("snap.xls","alarm_BB2.png","","Sheet3!A2:Q20")
excel2img.export_img("snap.xls","VSWR_BB2.png","","Sheet4!A2:Q50")

以下是我得到的错误:

 AttributeError: '<win32com.gen_py.Microsoft Excel 16.0 Object 
 Library.Range instance at 0x2697515188064>' object has no attribute 
 'parent'

下面是快照excel文件。格式为xlsxlsxsnap.xlsx

snap.xls

1 个答案:

答案 0 :(得分:0)

通过使用XLS文件:

import excel2img

excel2img.export_img("snap.xls", "alarm_BB1.png", "Sheet1", "A2:Q20")
excel2img.export_img("snap.xls", "VSWR_BB1.png", "Sheet2", "A2:Q50")
excel2img.export_img("snap.xls", "alarm_BB2.png", "Sheet3", "A2:Q20")
excel2img.export_img("snap.xls", "VSWR_BB2.png", "Sheet4", "A2:Q50")

这适用于我的系统:python 3.6 / Office 2013

我也尝试了XLSX文件,但是这些我无法打开,最终文件损坏了。 excel2img支持XLSX文件,我在具有此扩展名的项目中使用此模块。

如果我使用Python 3.7,则会收到模块错误(win32api),并且如果您查看项目站点:python 3.7 https://pypi.org/project/excel2img/

中没有列出支持

为您的项目尝试Python 3.6。 在另一个线程上,我读到Office 2016可能也是excel2img的问题。 Python's excel2img package not working with MS office 2016

但是我无法测试...

希望对您有帮助。