How To Implement Graphic Scaling Using xlwings?

Method

Graphic scaling, also known as affine transformation, is the process of enlarging or reducing a given graphic by a certain ratio. Use the `ScaleWidth` and `ScaleHeight` methods of the `Shape` object to specify the scaling ratios for the horizontal and vertical directions, respectively, to achieve the scaling operation.

 

Both `ScaleWidth` and `ScaleHeight` methods have three parameters, as shown in the table.

 

Name

Required/Optional

Data Type

Description

Factor

Required

Single

Specifies the ratio of the adjusted width to the current or original width

RelativeToOriginalSize

Required

MsoTriState

When `False`, scales relative to the current size. This parameter can only be set to `True` when the graphic is a picture or OLE object.

Scale

Optional

Variant

One of the constants from MsoScaleFrom type, specifying which part of the graphic remains at the original position when scaling.

 

The `Scale` parameter takes constants from the `MsoScaleFrom` enumeration, indicating which part of the graphic remains at the original position after scaling. The possible values are as follows:

 

Name

Value

Description

msoScaleFromBottomRight

2

The bottom-right corner of the graphic remains at the original position

msoScaleFromMiddle

1

The midpoint of the graphic remains at the original position

msoScaleFromTopLeft

0

The top-left corner of the graphic remains at the original position

 

shp=sht.api.Shapes.AddShape(9, 100, 50, 200, 100)    #Elliptical area

ff=shp.Fill

ff.PresetTextured(12)    #Preset texture: Granite

shp.ScaleWidth(0.75,False)    #Width × 0.75

shp.ScaleHeight(1.75,False)    #Height × 1.75

Sample Code

#Flip transformation

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(1, 100, 50, 200, 100)    #Rectangular area
shp.Fill.PresetTextured(22)    #Preset texture: Walnut wood
shp.Flip(0)    #Horizontal flip
shp.Flip(1)    #Vertical flip

#bk.save()
#bk.close()
#app.kill()
March 1, 2026 (0)


Leave a Reply

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