How To Create Custom Impact Chart Using xlwings?

Example

Code

import xlwings as xw
import numpy as np
import os

def set_style(cht):
    cht.ChartArea.Format.Line.Visible=False
    cht.PlotArea.Format.Fill.Visible = False
    cht.PlotArea.Format.Line.Visible = True
    cht.PlotArea.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((200,200,200))
    #cht.PlotArea.Format.Line.ForeColor.ObjectThemeColor = msoThemeColorText1
    ax1 = cht.Axes(1)
    ax2 = cht.Axes(2)
    ax1.HasTitle = True
    ax1.AxisTitle.Text = 'Categories'
    ax1.AxisTitle.Font.Size = 10
    ax1.TickLabels.Font.Size = 8
    #ax1.TickLabels.NumberFormat = '0.00'
    ax1.HasMajorGridlines = False
    ax2.HasTitle = True
    ax2.AxisTitle.Text = 'Values'
    ax2.AxisTitle.Font.Size = 10
    ax2.TickLabels.Font.Size = 8
    ax2.HasMajorGridlines = False
    cht.HasTitle = True
    #cht.ChartTitle.Caption = 'Plot'
    #cht.ChartTitle.Font.Size = 12

def draw_poly_4(cht,pts,r,g,b,alpha):
    pt=[[0 for _ in range(2)] for _ in range(5)]
    pt[0][0]=shape_x(cht, pts[0][0])
    pt[0][1]=shape_y(cht, pts[0][1])
    pt[1][0]=shape_x(cht, pts[1][0])
    pt[1][1]=shape_y(cht, pts[1][1])
    pt[2][0]=shape_x(cht, pts[2][0])
    pt[2][1]=shape_y(cht, pts[2][1])
    pt[3][0]=shape_x(cht, pts[3][0])
    pt[3][1]=shape_y(cht, pts[3][1])
    pt[4][0]=pt[0][0]
    pt[4][1]=pt[0][1]
    shp=cht.Shapes.AddPolyline(pt)
    if alpha==0:
        shp.Fill.ForeColor.RGB=xw.utils.rgb_to_int((r,g,b))
    else:
        shp.Fill.Transparency=0.5
        shp.Fill.ForeColor.RGB=xw.utils.rgb_to_int((r,g,b))
    shp.Line.Visible=False


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')
data=sht.range('B1:D6').value
app.kill()

from comtypes.client import CreateObject
app2=CreateObject("Excel.Application")
app2.Visible=True
app2.ScreenUpdating=False
wb2=app2.Workbooks.Open(root+r'/data.xlsx')
sht2=wb2.Sheets('Sheet1')

shp=sht2.Shapes.AddChart2()
shp.Left=20
cht=shp.Chart
cht.ChartType=-4169
ax1=cht.Axes(1)
ax2=cht.Axes(2)
ax1.MinimumScale=0
ax1.MaximumScale=7
ax2.MinimumScale=0
ax2.MaximumScale=0.9

set_style(cht)

cht.SeriesCollection().NewSeries()

dt=np.zeros([6,4])
for i in range(6):
    my_sum=0
    for j in range(3):
        #print(data[i][j])
        my_sum=my_sum+data[i][j]
        dt[i][j+1]=my_sum
colors=[[0,176,240],[146,208,80],[255,192,0]]

pts=np.zeros([4,2])
for i in range(6):
    for j in range(3):
        pts[0][0]=i+1-0.25
        pts[0][1]=dt[i][j]
        pts[1][0]=i+1+0.25
        pts[1][1]=dt[i][j]
        pts[2][0]=i+1+0.25
        pts[2][1]=dt[i][j+1]
        pts[3][0]=i+1-0.25
        pts[3][1]=dt[i][j+1]
        draw_poly_4(cht,pts,colors[j][0],colors[j][1],colors[j][2],0)

for i in range(5):
    for j in range(3):
        pts[0][0]=i+1+0.25
        pts[0][1]=dt[i][j]
        pts[1][0]=i+2-0.25
        pts[1][1]=dt[i+1][j]
        pts[2][0]=i+2-0.25
        pts[2][1]=dt[i+1][j+1]
        pts[3][0]=i+1+0.25
        pts[3][1]=dt[i][j+1]
        draw_poly_4(cht,pts,colors[j][0],colors[j][1],colors[j][2],1)

app2.ScreenUpdating=True

#wb2.Save()
#os.system('taskkill /f /im EXCEL.EXE')
May 13, 2026 (0)


Leave a Reply

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