在Excel vba将chart另存为图片篇中讲了如何通过vba宏将excel 中的chart图表导出为的图片,本篇讲下如何使用纯python代码将excel 中的图表导出为图片。这里需要使用的模块有win32com、pythoncom模块。
网上经查询有人已经写好的模块pyxlchart,具体代码如下:
from win32com.client import Dispatch
import os
import pythoncom
class Pyxlchart(object):
"""
This class exports charts in an Excel Spreadsheet to the FileSystem
win32com libraries are required.
"""
def __init__(self):
pythoncom.CoInitialize()
self.WorkbookDirectory = ''
self.WorkbookFilename = ''
self.GetAllWorkbooks = False
self.SheetName = ''
self.ChartName = ''
self.GetAllWorkbookCharts = False
self.GetAllWorksheetCharts = False
self.ExportPath = ''
self.ImageFilename = ''
self.ReplaceWhiteSpaceChar = '_'
self.ImageType = 'jpg'
def __del__(self):
pass
def start_export(self):
if self.WorkbookDirectory == '':
return "WorkbookDirectory not set"
else:
self._export()
def _export(self):
"""
Exports Charts as determined by the settings in class variabels.
"""
excel = Dispatch("excel.application")
excel.Visible = False
wb = excel.Workbooks.Open(os.path.join(self.WorkbookDirectory ,self.WorkbookFilename))
self._get_Charts_In_Worksheet(wb,self.SheetName,self.ChartName)
wb.Close(False)
excel.Quit()
def _get_Charts_In_Worksheet(self,wb,worksheet = "", chartname = ""):
if worksheet != "" and chartname != "":
sht = self._change_sheet(wb,worksheet)
cht = sht.ChartObjects(chartname)
self._save_chart(cht)
return
if worksheet == "":
for sht in wb.Worksheets:
for cht in sht.ChartObjects():
if chartname == "":
self._save_chart(cht)
else:
if chartname == cht.Name:
self._save_chart(cht)
else:
sht = wb.Worksheets(worksheet)
for cht in sht.ChartObjects():
if chartname == "":
self._save_chart(cht)
else:
if chartname == cht.Name:
self._save_chart(cht)
def _change_sheet(self,wb,worksheet):
try:
return wb.Worksheets(worksheet)
except:
raise NameError('Unable to Select Sheet: ' + worksheet + ' in Workbook: ' + wb.Name)
def _save_chart(self,chartObject):
imagename = self._get_filename(chartObject.Name)
savepath = os.path.join(self.ExportPath,imagename)
print savepath
chartObject.Chart.Export(savepath,self.ImageType)
def _get_filename(self,chartname):
"""
Replaces white space in self.WorkbookFileName with the value given in self.ReplaceWhiteSpaceChar
If self.ReplaceWhiteSpaceChar is an empty string then self.WorkBookFileName is left as is
"""
if self.ImageFilename == '':
self.ImageFilename == chartname
if self.ReplaceWhiteSpaceChar != '':
chartname.replace(' ',self.ReplaceWhiteSpaceChar)
if self.ImageFilename != "":
return self.ImageFilename + "_" + chartname + "." + self.ImageType
else:
return chartname + '.' + self.ImageType
if __name__ == "__main__":
xl = Pyxlchart()
xl.WorkbookDirectory = "\\maawtns01\discipline\procurement\MATERIEL\Raw Material\Data Management\Hawk"
xl.WorkbookFilename = "Hawk Workability KPI.xlsm"
xl.SheetName = ""
xl.ImageFilename = "MyChart1"
xl.ExportPath = "d:\pycharts"
xl.ChartName = ""
xl.start_export()
print "This file does not currently allow direct access"
print "Please import PyXLChart and run start_export()"
这里还使用Excel vba将chart另存为图片篇中创建的chart_column.xlsx表,使用上面的模块的方法如下:
from pyxlchart import Pyxlchart
xl = Pyxlchart()
xl.WorkbookDirectory = "D:\"
xl.WorkbookFilename = "chart_column.xlsx"
xl.SheetName = ""
#xl.ImageFilename = "MyChart1"
xl.ExportPath = "d:\"
xl.ChartName = ""
xl.start_export()
由于有该表里有多张图表,所以上面未指定xl.ImageFilename ,使用示例如下:
我的博物馆故事 官方安卓版v1.61.2
我的博物馆故事是一款以消除为主题的经营养成类手游,在这里玩家
专业模拟飞行10 手机版v12.2.4
专业模拟飞行10安卓版是一款飞行休闲手游,顶尖的物理飞行引擎
动物起义战斗模拟器二琳同款 最新版v4.1.1
动物起义战斗模拟器是一个非常有趣的模拟类游戏,玩家可以召唤各
迷你世界七周年 安卓手机版v1.43.0
迷你世界7周年是一款由《迷你世界》官方推出的庆祝特别版本,在
劫后公司无限资源版 v1.0.5.1
劫后公司内置菜单版是游戏的破解版本,在该版本中为玩家提供了内