How To Set Built-in Styles for Excel Charts Using xlwings?

Example

Code

import xlwings as xw
import os

root=os.getcwd()
app=xw.App(visible=True,add_book=False)
wb=app.books.open(root+r'/data.xlsx',read_only=False)
sht=wb.sheets('Sheet1')

sht.api.Range('A2:C11').Select()  #数据
cht1=sht.api.Shapes.AddChart2(228,xw.constants.ChartType.xlLine,10,20,350,250,True).Chart
cht2=sht.api.Shapes.AddChart2(230,xw.constants.ChartType.xlLine,20,20,350,250,True).Chart
cht3=sht.api.Shapes.AddChart2(232,xw.constants.ChartType.xlLine,30,20,350,250,True).Chart
cht4=sht.api.Shapes.AddChart2(233,xw.constants.ChartType.xlLine,40,20,350,250,True).Chart

cht1.Export(root+'/cht1.jpg')
cht1.Export(root+'/cht1.svg')
cht1.ExportAsFixedFormat(0,root+'/cht1.pdf')

cht2.Export(root+'/cht2.jpg')
cht2.Export(root+'/cht2.svg')
cht2.ExportAsFixedFormat(0,root+'/cht2.pdf')

cht3.Export(root+'/cht3.jpg')
cht3.Export(root+'/cht3.svg')
cht3.ExportAsFixedFormat(0,root+'/cht3.pdf')

cht4.Export(root+'/cht4.jpg')
cht4.Export(root+'/cht4.svg')
cht4.ExportAsFixedFormat(0,root+'/cht4.pdf')

#wb.save()
#app.kill()
March 30, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *