Using win32com via python to scrape excel file for chart objects and convert them to images -


i trying scrape .xlsx excel file chart objects , export them images. similar stackoverflow question found this one attempts same thing. script, however, not seem work (even when correct syntax/methods). willing running in either python 2.7.9 or 3.4.0. have both versions running on computer. here code working with:

import win32com.client win32 excel = win32.gencache.ensuredispatch('excel.application')  wb = excel.workbooks.open(r'c:\users\emilyn\desktop\charttest.xlsx')  excel.visible = true wb.sheets("sheet1").select()  wbsheetone = wb.sheets(1)  wb.displayalerts = false  = 0     chart in wbsheetone.chartobjects():         print(chart.name)         chart.copypicture()         excel.activeworkbook.sheets.add(after =excel.activeworkbook.sheets(3)).name="temp_sheet" + str(i)         temp_sheet = wb.activesheet         cht = wb.activesheet.chartobjects().add(0,0,800,600)         cht.chart.export("chart" + str(i) + ".png")         = i+1 excel.activeworkbook.close wb.displayalerts = true 

this opens excel file, generates 3 .png images in documents folder, , creates 3 new worksheets images, images blank.i not sure can chart objects in excel file correctly copy these newly created images. on appreciated there seems no in depth documentation on pywin/win32com anywhere.

i've been searching internet mad , trying work day or 2 now... it's hard work when don't know of methods available, or of methods do.

(yes, have read "read me" files came library , read offered on website well.)

i figured out suppose i'll post future users.

for index in range(1, count + 1):     currentchart = wbsheet.chartobjects(index)     currentchart.copy     currentchart.chart.export("chart" + str(index) + ".png") 

i used count loop, way dynamically read amount of chart objects in excel file.

also, reason started range @ 1 because vb in excel starts index of objects @ 1, not zero.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -