简体   繁体   English

通过python使用win32com刮取图表对象的Excel文件并将其转换为图像

[英]Using win32com via python to scrape excel file for chart objects and convert them to images

I am trying to scrape a .xlsx excel file for chart objects and export them as images. 我正在尝试为chart objects抓取.xlsx excel文件并将其导出为图像。 The only similar stackoverflow question I found was this one which attempts to do the same thing. 唯一的类似计算器的问题,我发现这一个 ,它试图做同样的事情。 The script, however, does not seem to work (even when I correct the syntax/methods). 但是,该脚本似乎不起作用(即使我更正了语法/方法)。 I am willing to get this running in either Python 2.7.9 or 3.4.0. 我愿意在Python 2.7.93.4.0.运行它3.4.0. as I have both versions running on my computer. 因为我的计算机上同时运行了两个版本。 Here is the code I am working with: 这是我正在使用的代码:

import win32com.client as 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 
i = 0
    for 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 = i+1
excel.ActiveWorkbook.close
wb.DisplayAlerts = True

This opens my excel file, generates three .png images in my documents folder, and creates three new worksheets for the images, but the images are all blank.I am not sure what I can do to get the chart objects in my excel file to correctly copy to these newly created images. 这将打开我的excel文件,在我的documents文件夹中生成三个.png图像,并为这些图像创建三个新的worksheets ,但是这些图像都是空白的。我不确定如何才能将excel文件中的chart objects转换为正确复制到这些新创建的图像。 Any help I could get on this would be greatly appreciated as there seems to be no in depth documentation on pywin/win32com anywhere. 对此我能提供的任何帮助将不胜感激,因为pywin/win32com上似乎没有任何深入的文档。

I've been searching the internet like mad and trying to get this to work for a day or two now... It's hard to get something to work when you don't know all of the methods available, or even what some of the methods do. 我一直在疯狂地搜索互联网,现在试图使它工作一两天。如果您不知道所有可用的方法,甚至不知道某些可用的方法,都很难工作。方法呢。

(Yes, I have read all the "read me" files that came with the library and read what they offered on their website as well.) (是的,我已经阅读了图书馆随附的所有“自述”文件,并阅读了它们在网站上提供的内容。)

I already figured out what to do but I suppose I'll post it for future users. 我已经弄清楚该怎么做,但是我想将其发布给以后的用户。

for index in range(1, count + 1):
    currentChart = wbSheet.ChartObjects(index)
    currentChart.Copy
    currentChart.Chart.Export("chart" + str(index) + ".png")

I used a count to do a for loop, this way you dynamically read the amount of chart objects in an excel file. 我使用了一个count来进行for循环,这样您可以动态读取excel文件中图表对象的数量。

Also, the reason I started the range at 1 is because VB in excel starts index of objects at 1, not zero. 另外,我将范围从1开始的原因是因为excel中的VB将对象的索引从1开始,而不是零。

You copy an existing chart as a picture, but don't do anything with it. 您将现有图表复制为图片,但不对其进行任何操作。

You insert a worksheet, without adding any data, then you embed a chart in that worksheet, which must be blank since there's no data to display, then export the blank chart as a png file. 插入工作表时,不添加任何数据,然后在该工作表中嵌入一个图表,该图表必须为空白,因为没有要显示的数据,然后将空白图表导出为png文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM