Blog
How To Set Face Properties Using xlwings? – Solid Fill
Method
In Excel, a face is represented by the `FillFormat` object. You can access this object via the `Fill` property of a `Shape` object and then programmatically set its properties.
You can specify the color using the `ForeColor` property and apply a solid fill using the `Solid` method.
shp=sht.api.Shapes.AddShape(9, 100, 50, 200, 100)
ff=shp.Fill
ff.ForeColor.RGB= xw.utils.rgb_to_int((255,0,0))
Sample Code
#Area properties - Solid color fill
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
shp=sht.api.Shapes.AddShape(9, 100, 50, 200, 100)
ff=shp.Fill
ff.ForeColor.RGB= xw.utils.rgb_to_int((255,0,0))
#bk.save()
#bk.close()
#app.kill()

How To Set Line Properties Using xlwings? – Pattern Fill
Method
You can apply a pattern fill to a line object using the `Pattern` property of the `LineFormat` object. This property sets or returns an `MsoPatternType` enumeration value, which defines the pattern of the fill.
|
Name |
Value |
Description |
|
msoPatternCross |
51 |
Crosshatch pattern |
|
msoPatternDarkDownwardDiagonal |
15 |
Dark downward diagonal |
|
msoPatternDarkHorizontal |
13 |
Dark horizontal lines |
|
msoPatternDarkUpwardDiagonal |
16 |
Dark upward diagonal |
|
msoPatternDarkVertical |
14 |
Dark vertical lines |
|
msoPatternHorizontal |
49 |
Horizontal lines |
|
msoPatternVertical |
50 |
Vertical lines |
|
msoPatternSmallGrid |
23 |
Small grid pattern |
|
msoPatternWave |
48 |
Wavy lines |
|
…… |
|
|
shp=sht.api.Shapes.AddLine(100, 55, 400, 125)
shp.Line.Weight=8
shp.Line.Pattern=16 #Set pattern fill for line segments
Sample Code
#Line Properties - Pattern Fill
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
shp=sht.api.Shapes.AddLine(100, 55, 400, 125)
shp.Line.Weight=8
shp.Line.Pattern=16 #Set pattern fill for line segments
#bk.save()
#bk.close()
#app.kill()

How To Set Line Properties Using xlwings? – Transparency
Method
You can set or get the line’s transparency using the `Transparency` property of the `LineFormat` object. The value ranges from 0.0 (opaque) to 1.0 (fully transparent).
shp2=sht.api.Shapes.AddLine(100, 125, 400, 125) #Second line segment
shp2.Line.Weight=8 #Line width
shp2.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0)) #Red
shp2.Line.Transparency=0.7 #Transparency 0.7
Sample Code
#Line properties - Transparency
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
sht.api.Shapes.AddShape(9, 150, 50, 200, 100) #Elliptical area
shp=sht.api.Shapes.AddLine(100, 75, 400, 75) #First line segment
shp.Line.Weight=8 #Line width
shp.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0)) #Red
shp2=sht.api.Shapes.AddLine(100, 125, 400, 125) #Second line segment
shp2.Line.Weight=8 #Line width
shp2.Line.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0)) #Red
shp2.Line.Transparency=0.7 #Transparency 0.7
#bk.save()
#bk.close()
#app.kill()

How To Set Line Properties Using xlwings? – Arrows
Method
You can set the line’s color using the `ForeColor` property of the `LineFormat` object. The color can be set using various methods, including RGB, theme colors, and color schemes.
You can set the line style using the `DashStyle` property of the `LineFormat` object. The available line styles are:
|
Name |
Value |
Description |
|
msoLineDash |
4 |
Dashed line |
|
msoLineDashDot |
5 |
Dash-dot line |
|
msoLineDashDotDot |
6 |
Dash-dot-dot line |
|
msoLineDashStyleMixed |
-2 |
Not supported |
|
msoLineLongDash |
7 |
Long dashed line |
|
msoLineLongDashDot |
8 |
Long dash-dot line |
|
msoLineRoundDot |
3 |
Round dotted line |
|
msoLineSolid |
1 |
Solid line |
|
msoLineSquareDot |
2 |
Square dotted line |
You can set the line width using the `Weight` property. This is a single value that determines the thickness of the line.
shp=sht.api.Shapes.AddLine(80, 50, 200, 300) #Create a line segment Shape object
lf=shp.Line #Get the line shape object
lf.Weight=2 #Line width
lf.BeginArrowheadLength=1 #Arrow length at the start point
lf.BeginArrowheadStyle=6 #Arrow style at the start point
lf.BeginArrowheadWidth=1 #Arrow width at the start point
lf.EndArrowheadLength=3 #Arrow length at the endpoint
lf.EndArrowheadStyle=2 #Arrow style at the endpoint
lf.EndArrowheadWidth=3 #Arrow width at the endpoint
Sample Code
#Line properties - Arrow
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
shp=sht.api.Shapes.AddLine(80, 50, 200, 300) #Create a line segment Shape object
lf=shp.Line #Get the line shape object
lf.Weight=2 #Line width
lf.BeginArrowheadLength=1 #Arrow length at the start point
lf.BeginArrowheadStyle=6 #Arrow style at the start point
lf.BeginArrowheadWidth=1 #Arrow width at the start point
lf.EndArrowheadLength=3 #Arrow length at the endpoint
lf.EndArrowheadStyle=2 #Arrow style at the endpoint
lf.EndArrowheadWidth=3 #Arrow width at the endpoint
#bk.save()
#bk.close()
#app.kill()

How To Set Line Properties Using xlwings? – Color, Line Style, and Line Width
Method
In Excel, line objects are represented by the `LineFormat` object. The `Line` property of a `Shape` object returns a `LineFormat` object. For example, a straight line, the borders of a rectangle, and a circular region are all represented as `LineFormat` objects.
Once you have the `LineFormat` object, you can use its properties and methods to set various attributes like color, line style, line width, arrows, transparency, and pattern fill.
shp=sht.api.Shapes.AddLine(20, 20, 100, 120)
lf=shp.Line
lf.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0)) #Red
lf.DashStyle=5 #Line style, dotted line
lf.Weight=3 #Line width
shp2=sht.api.Shapes.AddShape(9, 200, 30, 120, 80)
#Line shape in elliptical area, i.e., the boundary of the area
lf2=shp2.Line
lf2.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0)) #Red
lf2.DashStyle=3 #Line style, dotted line with circles
lf2.Weight=4 #Line width
Sample Code
#Line properties
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
shp=sht.api.Shapes.AddLine(20, 20, 100, 120)
lf=shp.Line
lf.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0)) #Red
lf.DashStyle=5 #Line style, dotted line
lf.Weight=3 #Line width
shp2=sht.api.Shapes.AddShape(9, 200, 30, 120, 80)
#Line shape in elliptical area, i.e., the boundary of the area
lf2=shp2.Line
lf2.ForeColor.RGB=xw.utils.rgb_to_int((255,0,0)) #Red
lf2.DashStyle=3 #Line style, dotted line with circles
lf2.Weight=4 #Line width
#bk.save()
#bk.close()
#app.kill()

How To Set Color Using xlwings? – Scheme Colors
Method
Excel provides a set of colors from its color scheme that can be used to fill graphical objects. For graphical objects, the `ForeColor` and `BackColor` properties return a `ColorFormat` object, which includes a `SchemeColor` property. Each color in the color scheme has an index number, which can be assigned to the `SchemeColor` property.
shp=sht.api.Shapes.AddShape(9, 50, 50, 100, 100)
shp.Fill.ForeColor.SchemeColor=3
shp.Line.ForeColor.SchemeColor=4
Sample Code
#Color - Scheme color
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
shp=sht.api.Shapes.AddShape(9, 50, 50, 100, 100)
shp.Fill.ForeColor.SchemeColor=3
shp.Line.ForeColor.SchemeColor=4
#bk.save()
#bk.close()
#app.kill()

How To Set Color Using xlwings? – Theme Colors
Method
|
Name |
Value |
Description |
|
xlThemeColorAccent1 |
5 |
Accent1 |
|
xlThemeColorAccent2 |
6 |
Accent2 |
|
xlThemeColorAccent3 |
7 |
Accent3 |
|
xlThemeColorAccent4 |
8 |
Accent4 |
|
xlThemeColorAccent5 |
9 |
Accent5 |
|
xlThemeColorAccent6 |
10 |
Accent6 |
|
xlThemeColorDark1 |
1 |
Dark1 |
|
xlThemeColorDark2 |
3 |
Dark2 |
|
xlThemeColorFollowedHyperlink |
12 |
Followed hyperlink |
|
xlThemeColorHyperlink |
11 |
Hyperlink |
|
xlThemeColorLight1 |
2 |
Light1 |
|
xlThemeColorLight2 |
4 |
Light2 |
shp=sht.api.Shapes.AddShape(9, 50, 50, 100, 100)
shp.Fill.ForeColor.ObjectThemeColor=10
shp.Line.ForeColor.ObjectThemeColor=3
Sample Code
#Color - Theme color
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
shp=sht.api.Shapes.AddShape(9, 50, 50, 100, 100)
shp.Fill.ForeColor.ObjectThemeColor=10
shp.Line.ForeColor.ObjectThemeColor=3
#bk.save()
#bk.close()
#app.kill()

How To Set Color Using xlwings? – Indexed Coloring
Method
Indexed coloring involves using a predefined set of colors, known as a color lookup table, where each color has a unique index number, as shown in the diagram. To apply indexed coloring, simply assign an index number to the corresponding indexed color property.
sht.api.Range(“C3”).Font.ColorIndex=3
sht.api.Range(“C3″).Value=”Hello”
Sample Code
#Color - Index color
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
sht.api.Range("C3").Font.ColorIndex=3
sht.api.Range("C3").Value="Hello"
#bk.save()
#bk.close()
#app.kill()

How To Set Color Using xlwings? – RGB
Method
RGB color shading defines a color using the red, green, and blue components. You can set RGB color shading using the `Color` property of graphical objects. If you’re accustomed to specifying RGB components to set the color, you can use the `rgb_to_int` method from the `xlwings.utils` class to convert an RGB tuple, such as (255, 0, 0), into an integer, which can then be assigned to the `Color` property.
shp=sht.api.Shapes.AddShape(9, 50, 50, 100, 100)
shp.Fill.ForeColor.RGB=xw.utils.rgb_to_int((0, 255,0))
shp.Line.ForeColor.RGB=xw.utils.rgb_to_int((0,0,255))
shp.Line.ForeColor.RGB=16711680 # or 0x0000FF
Sample Code
#Color - RGB
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
shp=sht.api.Shapes.AddShape(9, 50, 50, 100, 100)
#shp.Fill.ForeColor.RGB=xw.utils.rgb_to_int((0, 255,0))
shp.Line.ForeColor.RGB=xw.utils.rgb_to_int((0,0,255))
shp.Fill.ForeColor.RGB=0x0000FF # or 16711680
#bk.save()
#bk.close()
#app.kill()

How To Create WordArt Using xlwings?
Method
Use the `AddTextEffect` method of the `Shapes` object to create WordArt. The syntax is:
sht.api.Shapes.AddTextEffect(PresetTextEffect,Text,FontName,FontSize,FontBold,FontItalic, Left, Top)
Where `sht` is the current worksheet. The parameters are explained in the table below.
|
Name |
Required/Optional |
Data Type |
Description |
|
PresetTextEffect |
Required |
MsoPresetTextEffect |
Predefined text effect |
|
Text |
Required |
String |
Text for WordArt |
|
FontName |
Required |
String |
Font name used for WordArt |
|
FontSize |
Required |
Single |
Font size (in points) used in WordArt |
|
FontBold |
Required |
MsoTriState |
Whether the font is bold |
|
FontItalic |
Required |
MsoTriState |
Whether the font is italic |
|
Left |
Required |
Single |
Horizontal coordinate of the upper-left corner |
|
Top |
Required |
Single |
Vertical coordinate of the upper-left corner |
|
Name |
Value |
Description |
|
msoTextEffect1 |
0 |
First text effect |
|
msoTextEffect2 |
1 |
Second text effect |
|
msoTextEffect3 |
2 |
Third text effect |
|
…… |
|
|
sht.api.Shapes.AddTextEffect(9,’Learn Python’,’Arial Black’,36,False,False,10,10)
sht.api.Shapes.AddTextEffect(29,’xlwings’,’Times New Roman’,40,False,False,30,50)
Sample Code
#Drawing WordArt
import xlwings as xw #Import xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
sht.api.Shapes.AddTextEffect(9,'Learn Python','Arial Black',36,False,False,10,10)
sht.api.Shapes.AddTextEffect(29,'xlwings','Times New Roman',40,False,False,30,50)
#bk.save()
#bk.close()
#app.kill()
