Using the methods provided by the `Shape` object, you can perform translation, rotation, scaling, and reflection transformations on a given graphic. The implementation of these methods is exactly the same for images.
Use the `AddPicture` method of the `Shape` object to create an image from an existing file. This method returns a `Shape` object representing the new image. The syntax of this method is:
Here, `sht` represents the worksheet, and the parameters are as follows:
Name
Required/Optional
Data Type
Description
FileName
Required
String
The image file name
LinkToFile
Required
MsoTriState
Set to `False` to make the image an independent copy, not linked. Set to `True` to link the image to the file from which it was created
SaveWithDocument
Required
MsoTriState
Saves the image with the document. Use `False` to store only the link information, and `True` to save the linked image with the document. This parameter must be `True` if `LinkToFile` is `False`
Left
Required
Single
The position of the top-left corner of the image relative to the document’s top-left corner (in points)
Top
Required
Single
The position of the top-left corner of the image relative to the document’s top (in points)
Width
Required
Single
The width of the image in points (enter `-1` to retain the original width)
Height
Required
Single
The height of the image in points (enter `-1` to retain the original height)
Graphic flipping, also known as image reflection or symmetry transformation, is achieved using the `Flip` method of the `Shape` object. This method flips the graphic around either the horizontal or vertical axis. It has one parameter that specifies whether to flip horizontally or vertically. The corresponding values for horizontal and vertical flips are 0 and 1, respectively.
shp=sht.api.Shapes.AddShape(1, 100, 50, 200, 100) #Rectangular area
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()
Use the `IncrementRotation` method of the `Shape` object to rotate the graphic. This method rotates the graphic around the Z-axis by a specified angle. It has one parameter, representing the angle of rotation in degrees. A positive value rotates the graphic clockwise, while a negative value rotates the graphic counterclockwise.
shp=sht.api.Shapes.AddShape(1, 100, 50, 200, 100) #Rectangular area
shp.Fill.PresetTextured(5) #Preset texture: Water drop
shp.IncrementRotation(30) #Rotate clockwise by 30°°
Sample Code
#Scaling 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(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
#bk.save()
#bk.close()
#app.kill()
Use the `IncrementLeft` method of the `Shape` object to translate the graphic horizontally. This method has one parameter: when the value is greater than 0, the graphic moves to the right; when the value is less than 0, the graphic moves to the left.
Use the `IncrementTop` method of the `Shape` object to translate the graphic vertically. This method has one parameter: when the value is greater than 0, the graphic moves down; when the value is less than 0, the graphic moves up.
shp=sht.api.Shapes.AddShape(1, 100, 50, 200, 100) #Rectangular area
shp.Fill.PresetTextured(5) #Preset texture: Water drop
shp.IncrementLeft(70) #Move right by 70 units
shp.IncrementTop(50) #Move down by 50 units
Sample Code
#Rotation 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(5) #Preset texture: Water drop
shp.IncrementRotation(30) #Rotate clockwise by 30°
#bk.save()
#bk.close()
#app.kill()
In Excel, fonts are represented by the `Font` object. When setting the font, it is usually done by obtaining the `Font` object through a property, and then using the properties and methods of the object for configuration.
The main properties of the `Font` object include:
– Bold: Whether the font is bold. `True` for bold, `False` for not bold.
– Color: RGB color shading.
– ColorIndex: Index coloring. The index number of a color in the color lookup table.
– FontStyle: Font style, such as “Bold Italic”.
– Italic: Whether the font is italicized. `True` for italicized, `False` for not italicized.
– Name: Font name.
– Size: Font size.
– Strikethrough: Whether to apply a strikethrough. `True` to apply, `False` to not apply.
– Subscript: Whether the text is set as subscript. `True` to apply, `False` to not apply.
– Superscript: Whether the text is set as superscript. `True` to apply, `False` to not apply.
– ThemeColor: Theme color shading.
– ThemeFont: Theme font.
– TintAndShade: Darken or lighten the font color, with a value between -1 (darkest) to 1 (lightest).
– Underline: The type of underline.
– Value -4142: No underline
– Value 2: Single underline
– Value -4119: Bold double underline
– Value 5: Close together thin double underline
sht.api.Range(‘C3′).Value=’Font Test123’
ft=sht.api.Range(‘C3’).Font
ft.Name = ‘Times New Roman’
ft.ColorIndex = 3
ft.Size = 20
ft.Bold=True
ft.Strikethrough = False
ft.Underline = 5
ft.Italic=True
Sample Code
#Translation 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(5) #Preset texture: Water drop
shp.IncrementLeft(70) #Move right by 70 units
shp.IncrementTop(50) #Move down by 50 units
#bk.save()
#bk.close()
#app.kill()
shp1=sht.api.Shapes.AddShape(1, 100, 50, 200, 100) #Rectangular area
ff1=shp1.Fill
ff1.PresetTextured(9) #Preset texture: Green marble
Sample Code
#Area 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
shp=sht.api.Shapes.AddShape(1, 200, 50, 200, 100) #Rectangular area
shp2=sht.api.Shapes.AddShape(9, 150, 70, 200, 100) #Elliptical area
ff=shp2.Fill
ff.ForeColor.RGB= xw.utils.rgb_to_int((255,0,0)) #Set elliptical area to red
ff.Transparency=0.7 #Transparency 0.7
#bk.save()
#bk.close()
#app.kill()