Python 3.x 将excel电子表格中的cellrange另存为python中的图像
我目前正在编写一个Python脚本,该脚本使用excel2image模块保存Excel文件中的屏幕截图 我的代码非常简单:Python 3.x 将excel电子表格中的cellrange另存为python中的图像,python-3.x,win32com,Python 3.x,Win32com,我目前正在编写一个Python脚本,该脚本使用excel2image模块保存Excel文件中的屏幕截图 我的代码非常简单: import excel2img excel2img.export_img(Workpath + "/" + "CB_TEMP.xlsx", "alarm_BB1.png", "Deckblatt", "A2:C20") 不幸的是,我总是收到以下错误消息: --------
import excel2img
excel2img.export_img(Workpath + "/" + "CB_TEMP.xlsx", "alarm_BB1.png", "Deckblatt", "A2:C20")
不幸的是,我总是收到以下错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-18-a119e849f4d5> in <module>
----> 1 excel2img.export_img(Workpath + "/" + "CB_TEMP.xlsx", "alarm_BB1.png", "Deckblatt", "A2:C20")
~\anaconda3\lib\site-packages\excel2img\excel2img.py in export_img(fn_excel, fn_image, page, _range)
111
112 # See http://stackoverflow.com/a/42465354/1924207
--> 113 for shape in rng.parent.Shapes: pass
114
115 xlScreen, xlPrinter = 1, 2
~\anaconda3\lib\site-packages\win32com\client\__init__.py in __getattr__(self, attr)
471 args=self._prop_map_get_.get(attr)
472 if args is None:
--> 473 raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
474 return self._ApplyTypes_(*args)
475
AttributeError: '<win32com.gen_py.Microsoft Excel 16.0 Object Library.Range instance at 0x2460934736048>' object has no attribute 'parent'
也许有人能帮我
亲切问候,,
Patrick我通过删除excel2img模块解决了这个问题 我在xlwings with Pillow中编写了新代码,它比excel2img的工作速度更快:
import xlwings as xw
from PIL import ImageGrab
try:
excel_app = xw.App(visible=False)
excel_book = excel_app.books.open(excel_filepath)
for image in df_img.index:
excel_book.sheets[df_img.at[image, "sheet_name"]][
df_img.at[image, "Range"]].copy(destination=None)
img = ImageGrab.grabclipboard()
img.save(df_img.at[image, "Bild"], 'JPEG')
excel_book.close()
excel_app.quit()
excel_app.kill()
except:
pass
你有什么版本的office,office 2016似乎有问题,我会看看是否能找到修复程序。我正在使用office 365(V16.0.13801.20294)。好的,你正在使用的库只在office 2013中测试过,所以可能会找到另一个库……是的,我也这么认为,但找不到任何库。正如我提到的,在我在同一个python脚本中使用win32com模块之前,它工作得很好。查看代码,我发现excel2image使用了相同的库,这可能会导致错误?也许,您可以使用win32com修改代码以使其正常工作,我将在下个小时尝试发布并回答
import xlwings as xw
from PIL import ImageGrab
try:
excel_app = xw.App(visible=False)
excel_book = excel_app.books.open(excel_filepath)
for image in df_img.index:
excel_book.sheets[df_img.at[image, "sheet_name"]][
df_img.at[image, "Range"]].copy(destination=None)
img = ImageGrab.grabclipboard()
img.save(df_img.at[image, "Bild"], 'JPEG')
excel_book.close()
excel_app.quit()
excel_app.kill()
except:
pass