python 获取excel中的图表_使用Python将Excel中的图表导出为图像

我知道这是一个老问题,但它有助于我走上正确的轨道,所以我回来分享我完成的脚本,找到工作表中的所有图表,并将它们导出为.png。

上面的脚本可以工作,但是由于它只是复制工作表中的一个区域,所以您要依赖于正好位于该位置的图形。import win32com.client as win32

from win32com.client import Dispatch

import os

xlApp = Dispatch('Excel.Application')

workbook = xlApp.Workbooks.Open("Book1.xls")

xlApp.Sheets("Sheet1").Select()

xlSheet1 = xlApp.Sheets(1)

#WARNING: The following line will cause the script to discard any unsaved changes in your workbook

#Ensure to save any work before running script

xlApp.DisplayAlerts = False

i = 0

for chart in xlSheet1.ChartObjects():

print chart.Name

chart.CopyPicture()

#Create new temporary sheet

xlApp.ActiveWorkbook.Sheets.Add(After=xlApp.ActiveWorkbook.Sheets(3)).Name="temp_sheet" + str(i)

temp_sheet = xlApp.ActiveSheet

#Add chart object to new sheet.

cht = xlApp.ActiveSheet.ChartObjects().Add(0,0,800, 600)

#Paste copied chart into new object

cht.Chart.Paste()

#Export image

cht.Chart.Export("chart" + str(i) + ".png")

#This line is not entirely neccessary since script currently exits without saving

temp_sheet.Delete()

i = i+1

xlApp.ActiveWorkbook.Close()

#Restore default behaviour

xlApp.DisplayAlerts = True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值