This article discusses how to use pure Python code to export a chart in Excel to a picture in Excel by saving the chart as a picture in the article, which tells you how to export a chart chart in Excel to a picture from a VBA macro. The modules that need to be used here are win32com, pythoncom modules.
On-line query someone has written the module Pyxlchart, the specific code is as follows:
From win32com.client import Dispatch
Import OS
Import pythoncom
Class Pyxlchart (object):
"""
This class exports charts in a 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 spaces in self. Workbookfilename with the value given in self. Replacewhitespacechar
If self. Replacewhitespacechar is a 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 ()"
It also uses Excel VBA to save the chart as a chart_column.xlsx table created in a picture article, using the above modules in the following ways:
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 ()
Because there are more than one chart in the table, XL is not specified above. Imagefilename, use the following example: