Archive

How To Set Chart Area Using xlwings?

Method

The chart area is the rectangular area that contains the entire chart, while the plot area is the rectangular area defined by the two axes. In Excel, the chart area is represented by the `ChartArea` object, and the plot area is represented by the `PlotArea` object. You can access these areas using the `ChartArea` and `PlotArea` properties of the `Chart` object.

 

By continuously referencing the `ChartArea` and `PlotArea` objects’ `Format.Fill` property, you can set the fill properties of both areas, such as their color, transparency, gradient fills, pattern fills, picture fills, texture fills, etc.

 

Using the `Format.Shadow` property of the `ChartArea` and `PlotArea` objects, you can set additional shadow properties for these areas. The `Format.Shadow` property returns a `ShadowFormat` object with the following main properties:

– **Visible**: Determines whether the shadow is visible.

– **Blur**: Gets or sets the blur radius of the shadow.

– **Transparency**: Gets or sets the transparency of the shadow (from 0.0 for opaque to 1.0 for fully transparent).

– **OffsetX**: Gets or sets the horizontal offset of the shadow in points. Positive values shift the shadow to the right, while negative values shift it to the left.

– **OffsetY**: Gets or sets the vertical offset of the shadow in points. Positive values shift the shadow downward, while negative values shift it upward.

 

sht.api.Range(‘A1:B7’).Select()

cht=sht.api.Shapes.AddChart().Chart

cha=cht.ChartArea    #Chart Area

cha.Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((155,255,0))

cha.Shadow=True    #Plot area shows shadow

pla=cht.PlotArea    #Plot area

pla.Format.Fill.UserPicture(root+r’/picpy2.jpg’)    #Picture fill

cht.SeriesCollection(1).Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,0))

cht.Axes(2).HasMajorGridlines=False

cha.Shadow=False

pla.Format.Shadow.Visible=True    #Plot area shows shadow

pla.Format.Shadow.OffsetX=3    #Horizontal offset of the shadow

pla.Format.Shadow.OffsetY=3    #Vertical offset of the shadow

 

Example

Code

import xlwings as xw
import os

def set_style(cht):
    cht.ChartArea.Format.Line.Visible=False
    cht.PlotArea.Format.Fill.Visible=True
    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.AxisTitle.Font.Color = xw.utils.rgb_to_int((255,255,255))
    ax1.TickLabels.Font.Size=8
    ax1.TickLabels.Font.Color = xw.utils.rgb_to_int((255,255,255))
    #ax1.TickLabels.NumberFormat='0.00'
    ax1.HasMajorGridlines=False
    ax2.HasTitle=True
    ax2.AxisTitle.Text='Values'
    ax2.AxisTitle.Font.Size=10
    ax2.AxisTitle.Font.Color = xw.utils.rgb_to_int((255,255,255))
    ax2.TickLabels.Font.Size=8
    ax2.TickLabels.Font.Color = xw.utils.rgb_to_int((255,255,255))
    ax2.HasMajorGridlines=False
    cht.HasTitle=True
    #cht.ChartTitle.Caption='Plot'
    #cht.ChartTitle.Font.Size=12

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('A1:B7').Select()    #
cht=sht.api.Shapes.AddChart2(-1, \
          xw.constants.ChartType.xlColumnClustered,20,20,350,250,True).Chart
cht.ChartArea.Format.Fill.UserPicture('d:/pic.jpg')
cht.SeriesCollection(1).Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,0))

set_style(cht)
cht.Legend.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,255))

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

#wb.save()
#app.kill()

How To Set Plot Area Using xlwings? 2

Method

The chart area is the rectangular area that contains the entire chart, while the plot area is the rectangular area defined by the two axes. In Excel, the chart area is represented by the `ChartArea` object, and the plot area is represented by the `PlotArea` object. You can access these areas using the `ChartArea` and `PlotArea` properties of the `Chart` object.

By continuously referencing the `ChartArea` and `PlotArea` objects’ `Format.Fill` property, you can set the fill properties of both areas, such as their color, transparency, gradient fills, pattern fills, picture fills, texture fills, etc.

Using the `Format.Shadow` property of the `ChartArea` and `PlotArea` objects, you can set additional shadow properties for these areas. The `Format.Shadow` property returns a `ShadowFormat` object with the following main properties:

– **Visible**: Determines whether the shadow is visible.

– **Blur**: Gets or sets the blur radius of the shadow.

– **Transparency**: Gets or sets the transparency of the shadow (from 0.0 for opaque to 1.0 for fully transparent).

– **OffsetX**: Gets or sets the horizontal offset of the shadow in points. Positive values shift the shadow to the right, while negative values shift it to the left.

– **OffsetY**: Gets or sets the vertical offset of the shadow in points. Positive values shift the shadow downward, while negative values shift it upward.

sht.api.Range(‘A1:B7’).Select()

cht=sht.api.Shapes.AddChart().Chart

cha=cht.ChartArea    #Chart Area

cha.Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((155,255,0))

cha.Shadow=True    #Plot area shows shadow

pla=cht.PlotArea    #Plot area

pla.Format.Fill.UserPicture(root+r’/picpy2.jpg’)    #Picture fill

cht.SeriesCollection(1).Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,0))

cht.Axes(2).HasMajorGridlines=False

cha.Shadow=False

pla.Format.Shadow.Visible=True    #Plot area shows shadow

pla.Format.Shadow.OffsetX=3    #Horizontal offset of the shadow

pla.Format.Shadow.OffsetY=3    #Vertical offset of the shadow

 

Example

Code

import xlwings as xw
import os

def set_style(cht):
    cht.ChartArea.Format.Line.Visible=False
    cht.PlotArea.Format.Fill.Visible=True
    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

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('A1:B7').Select()    #
cht=sht.api.Shapes.AddChart2(-1, \
          xw.constants.ChartType.xlColumnClustered,20,20,350,250,True).Chart
cht.SeriesCollection(1).Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,0))
cht.PlotArea.Format.Fill.UserPicture('d:/picpy2.jpg')

set_style(cht)

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

#wb.save()
#app.kill()

How To Set Plot Area Using xlwings?

Method

The chart area is the rectangular area that contains the entire chart, while the plot area is the rectangular area defined by the two axes. In Excel, the chart area is represented by the `ChartArea` object, and the plot area is represented by the `PlotArea` object. You can access these areas using the `ChartArea` and `PlotArea` properties of the `Chart` object.

By continuously referencing the `ChartArea` and `PlotArea` objects’ `Format.Fill` property, you can set the fill properties of both areas, such as their color, transparency, gradient fills, pattern fills, picture fills, texture fills, etc.

Using the `Format.Shadow` property of the `ChartArea` and `PlotArea` objects, you can set additional shadow properties for these areas. The `Format.Shadow` property returns a `ShadowFormat` object with the following main properties:

– **Visible**: Determines whether the shadow is visible.

– **Blur**: Gets or sets the blur radius of the shadow.

– **Transparency**: Gets or sets the transparency of the shadow (from 0.0 for opaque to 1.0 for fully transparent).

– **OffsetX**: Gets or sets the horizontal offset of the shadow in points. Positive values shift the shadow to the right, while negative values shift it to the left.

– **OffsetY**: Gets or sets the vertical offset of the shadow in points. Positive values shift the shadow downward, while negative values shift it upward.

sht.api.Range(‘A1:B7’).Select()

cht=sht.api.Shapes.AddChart().Chart

cha=cht.ChartArea    #Chart Area

cha.Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((155,255,0))

cha.Shadow=True    #Plot area shows shadow

pla=cht.PlotArea    #Plot area

pla.Format.Fill.UserPicture(root+r’/picpy2.jpg’)    #Picture fill

cht.SeriesCollection(1).Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,0))

cht.Axes(2).HasMajorGridlines=False

cha.Shadow=False

pla.Format.Shadow.Visible=True    #Plot area shows shadow

pla.Format.Shadow.OffsetX=3    #Horizontal offset of the shadow

pla.Format.Shadow.OffsetY=3    #Vertical offset of the shadow

 

Example

import xlwings as xw
import os

def set_style(cht):
    cht.ChartArea.Format.Line.Visible=False
    cht.PlotArea.Format.Fill.Visible=True
    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

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('A1:B7').Select()    #
cht=sht.api.Shapes.AddChart2(-1, \
          xw.constants.ChartType.xlColumnClustered,20,20,350,250,True).Chart
cht.PlotArea.Format.Fill.ForeColor.RGB=xw.utils.rgb_to_int((255,255,26))
cht.PlotArea.Format.Fill.OneColorGradient(1,1,1)    #msoGradientHorizontal
      
set_style(cht)

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

#wb.save()
#app.kill()

How To Set Chart Title Using xlwings?

Example

Code

import xlwings as xw
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

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('A1:B7').Select()    #
cht=sht.api.Shapes.AddChart2(-1,\
          xw.constants.ChartType.xlColumnClustered,20,20,350,250,True).Chart
cht.HasTitle=True
cht.ChartTitle.Caption='Chart Title'
cht.ChartTitle.Characters.Font.Name='Times New Roman'
cht.ChartTitle.Characters.Font.Size=16
cht.ChartTitle.Characters.Font.Bold=True
cht.ChartTitle.Characters.Font.Color=xw.utils.rgb_to_int((255,0,0))
      
set_style(cht)

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

#wb.save()
#app.kill()

How To Use SetElement Method Using xlwings?

Method

The `SetElement` method of the `Chart` object allows you to set chart elements for the specified chart. The method has one parameter that provides options for setting. The possible values of the parameter are shown in the table below.

Name

Value

Description

msoElementChartFloorNone

1200

Do not display chart floor

msoElementChartFloorShow

1201

Display chart floor

msoElementChartTitleAboveChart

2

Display title above the chart

msoElementChartTitleCenteredOverlay

1

Display title centered as overlay

msoElementChartTitleNone

0

Do not display chart title

msoElementChartWallNone

1100

Do not display chart background wall

msoElementChartWallShow

1101

Display chart background wall

msoElementDataLabelBestFit

210

Use best fit data labels

msoElementDataLabelBottom

209

Display data labels at the bottom

msoElementDataLabelCallout

211

Display data labels as callouts

msoElementDataLabelCenter

202

Centered data labels

msoElementDataLabelInsideBase

204

Display data labels inside the base

msoElementDataLabelInsideEnd

203

Display data labels inside the end

msoElementDataLabelLeft

206

Display data labels on the left

msoElementDataLabelNone

200

Do not display data labels

msoElementDataLabelOutSideEnd

205

Display data labels outside the end

msoElementDataLabelRight

207

Display data labels on the right

msoElementDataLabelShow

201

Display data labels

msoElementDataLabelTop

208

Display data labels at the top

msoElementDataTableNone

500

Do not display data table

msoElementDataTableShow

501

Display data table

msoElementDataTableWithLegendKeys

502

Display data table with legend keys

msoElementErrorBarNone

700

Do not display error bars

msoElementErrorBarPercentage

702

Display percentage error bars

msoElementErrorBarStandardDeviation

703

Display standard deviation error bars

msoElementErrorBarStandardError

701

Display standard error error bars

msoElementLegendBottom

104

Display legend at the bottom

msoElementLegendLeft

103

Display legend on the left

msoElementLegendLeftOverlay

106

Overlay legend on the left

msoElementLegendNone

100

Do not display legend

msoElementLegendRight

101

Display legend on the right

msoElementLegendRightOverlay

105

Overlay legend on the right

msoElementLegendTop

102

Display legend at the top

msoElementLineDropHiLoLine

804

Display drop line and high/low lines

msoElementLineDropLine

801

Display drop line

msoElementLineHiLoLine

802

Display high/low line

msoElementLineNone

800

Do not display lines

msoElementLineSeriesLine

803

Display series lines

msoElementPlotAreaNone

1000

Do not display plot area

msoElementPlotAreaShow

1001

Display plot area

How To Set Axis Reversing Using xlwings?

Method

axs.ReversePlotOrder=True

axs2.ReversePlotOrder=True

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'/data2.xlsx',read_only=False)
sht=wb.sheets('Sheet1')

sht.api.Range('A1:B7').Select()    #
shp=sht.api.Shapes.AddChart()    #
shp.Left=20
cht=shp.Chart
axs=cht.Axes(1)    #
axs.Border.ColorIndex=3    #
axs.Border.Weight=3    #
axs2=cht.Axes(2)    #
axs.ReversePlotOrder=True
axs2.ReversePlotOrder=True

#wb.save()
#app.kill()

How To Set Crosses Using xlwings?

Method

Use the **AxisBetweenCategories** property of the **Axis** object to set the intersection point between the value axis and the category axis. If the value is `True`, the intersection occurs in the middle of the categories; if it is `False`, the intersection occurs at the middle point of the categories.

 

The **Crosses** property of the **Axis** object returns or sets the point at which the axis intersects with another axis. The possible values for this property are as follows:

 

Name

Value

Description

xlAxisCrossesAutomatic

-4105

Automatically set by Excel

xlAxisCrossesCustom

-4114

Set by **CrossesAt** property

xlAxisCrossesMaximum

2

Axis crosses at maximum value

xlAxisCrossesMinimum

4

Axis crosses at minimum value

 

sht.api.Range(‘A1:B7’).Select()

cht=sht.api.Shapes.AddChart().Chart

axs2=cht.Axes(2)

axs2.Crosses=2

#axs2.Crosses=-4114

#axs2.CrossesAt=50

 

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'/data2.xlsx',read_only=False)
sht=wb.sheets('Sheet1')

sht.api.Range('A1:B7').Select()    #
shp=sht.api.Shapes.AddChart()    #
shp.Left=20
cht=shp.Chart
axs=cht.Axes(1)    #
axs.Border.ColorIndex=3    #
axs.Border.Weight=3    #
axs2=cht.Axes(2)    #
axs.Crosses=xw.constants.AxisCrosses.xlAxisCrossesMaximum
axs2.CrossesAt=10

#wb.save()
#app.kill()

How To Set Logarithmic Scale Chart Using xlwings?

Method

The **ScaleType** property of the **Axis** object returns or sets the scale type for the value axis, as shown in the table below. When the **ScaleType** property is set to `xw.constants.ScaleType.xlScaleLogarithmic`, the axis uses a logarithmic scale, allowing you to create a logarithmic scale chart.

Name

Value

Description

xlScaleLinear

-4132

Linear scale

xlScaleLogarithmic

-4133

Logarithmic scale

sht.api.Range(‘A1:B7’).Select()

cht=sht.api.Shapes.AddChart().Chart

cht.Axes(2).ScaleType=xw.constants.ScaleType.xlScaleLogarithmic    #Logarithmic scale

cht.Axes(2).HasMinorGridlines=True

 

Example

Code

#Coordinate system - Logarithmic scale chart

import xlwings as xw
import os

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

sht.api.Range('A1:B7').Select()
cht=sht.api.Shapes.AddChart().Chart
cht.Axes(2).ScaleType=xw.constants.ScaleType.xlScaleLogarithmic    #Logarithmic scale
cht.Axes(2).HasMinorGridlines=True

#wb.save()
#wb.close()
#app.kill()

How To Set Multiple Axes Chart Using xlwings?

Method

– Bind a series to the primary or secondary axis 

The **AxisGroup** property of the **Series** object is used to assign a series to either the primary axis (when the value is 1) or the secondary axis (when the value is 2).

– Set up the axes 

You can access the **Axis** object through the **Chart** object with the following syntax:

axs=cht.Axes(Type,AxisGroup)

Where `cht` is the **Chart** object. The **Type** parameter indicates the type of axis. A value of 1 means a category axis, while 2 means a value axis. The **AxisGroup** parameter specifies whether the axis is primary (1) or secondary (2). By default, the primary axis is displayed on the left, and the secondary axis is displayed on the right. This allows the creation of a dual-axis chart, where two charts are overlaid using the same horizontal axis and different vertical axes.

sht.api.Range(‘A1:B7’).Select()    #Data

cht=sht.api.Shapes.AddChart2(-1,xw.constants.ChartType.xlColumnClustered,\

                        20,20,350,200,True).Chart

cht.SeriesCollection(1).AxisGroup=1    #Y axis for series 1

cht.SeriesCollection(2).AxisGroup=2    #Y axis for series 2

cht.SeriesCollection(2).ChartType=xw.constants.ChartType.xlLine

cht.SeriesCollection(2).MarkerStyle=xw.constants.MarkerStyle.xlMarkerStyleTriangle

cht.SeriesCollection(2).MarkerForegroundColor=xw.utils.rgb_to_int((0,0,255))

cht.SeriesCollection(2).MarkerSize=8

cht.SeriesCollection(2).HasDataLabels=True

cht.SeriesCollection(1).HasDataLabels=True

 

axs1=cht.Axes(2,1)

axs1.MinimumScale=0

axs1.MaximumScale=60

axs1.HasTitle=True

axs1.AxisTitle.Text=’Y Axis 1′

 

axs2=cht.Axes(2,2)

axs2.MinimumScale=10

axs2.MaximumScale=160

axs2.HasTitle=True

axs2.AxisTitle.Text=’Y Axis 2′

 

Example

Code

#Coordinate system - Multi-axis chart

import xlwings as xw
import os

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

sht.api.Range('A1:B7').Select()    #Data
cht=sht.api.Shapes.AddChart2(-1,xw.constants.ChartType.xlColumnClustered,\
                        20,20,350,200,True).Chart
cht.SeriesCollection(1).AxisGroup=1    #Y axis for series 1
cht.SeriesCollection(2).AxisGroup=2    #Y axis for series 2
cht.SeriesCollection(2).ChartType=xw.constants.ChartType.xlLine
cht.SeriesCollection(2).MarkerStyle=xw.constants.MarkerStyle.xlMarkerStyleTriangle
cht.SeriesCollection(2).MarkerForegroundColor=xw.utils.rgb_to_int((0,0,255))
cht.SeriesCollection(2).MarkerSize=8
cht.SeriesCollection(2).HasDataLabels=True
cht.SeriesCollection(1).HasDataLabels=True

axs1=cht.Axes(2,1)
axs1.MinimumScale=0
axs1.MaximumScale=60
axs1.HasTitle=True
axs1.AxisTitle.Text='Y Axis 1'

axs2=cht.Axes(2,2)
axs2.MinimumScale=10
axs2.MaximumScale=160
axs2.HasTitle=True
axs2.AxisTitle.Text='Y Axis 2'
  
cht.ChartTitle.Caption='Multi-axis Plot'

#wb.save()
#wb.close()
#app.kill()

How To Set Gridlines Using xlwings?

Method

Gridlines are represented by the **Gridlines** object. You can use its **Border** or **Format** properties to set the gridline color, line style, width, and other attributes. The **MajorGridlines** and **MinorGridlines** properties of the **Axis** object return **Gridlines** objects for the major and minor gridlines, respectively. Before setting these properties, the **HasMajorGridlines** and/or **HasMinorGridlines** properties of the **Axis** object must be set to `True`.

sht.api.Range(‘A1:B7’).Select()    #Data

cht=sht.api.Shapes.AddChart().Chart    #Add chart

axs=cht.Axes(1)    #Horizontal axis

axs2=cht.Axes(2)    #Vertical axis

axs.HasMajorGridlines=True    #Show major gridlines for horizontal axis

axs.MajorGridlines.Border.ColorIndex =3    #Red

axs.MajorGridlines.Border.LineStyle = xw.constants.LineStyle.xlDash    #Line style

axs2.HasMajorGridlines=True    #Show major gridlines for vertical axis

axs2.MajorGridlines.Border.ColorIndex = 3    #Red

axs2.MajorGridlines.Border.LineStyle = xw.constants.LineStyle.xlDash    #Line style

#Can also use the following code for setting

#axs.MajorGridlines.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0))

#axs.MajorGridlines.Format.Line.DashStyle=4

#axs2.MajorGridlines.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0))

#axs2.MajorGridlines.Format.Line.DashStyle=4

 

Example

Code

#Coordinate system - Gridlines

import xlwings as xw
import os

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

sht.api.Range('A1:B7').Select()    #Data
cht=sht.api.Shapes.AddChart().Chart    #Add chart
axs=cht.Axes(1)    #Horizontal axis
axs2=cht.Axes(2)    #Vertical axis
axs.HasMajorGridlines=True    #Show major gridlines for horizontal axis
axs.MajorGridlines.Border.ColorIndex =3    #Red
axs.MajorGridlines.Border.LineStyle = xw.constants.LineStyle.xlDash    #Line style
axs2.HasMajorGridlines=True    #Show major gridlines for vertical axis
axs2.MajorGridlines.Border.ColorIndex = 3    #Red
axs2.MajorGridlines.Border.LineStyle = xw.constants.LineStyle.xlDash    #Line style
#Can also use the following code for setting
#axs.MajorGridlines.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0))
#axs.MajorGridlines.Format.Line.DashStyle=4
#axs2.MajorGridlines.Format.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0))
#axs2.MajorGridlines.Format.Line.DashStyle=4

#wb.save()
#wb.close()
#app.kill()