Method
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:
sht.api.Shapes.AddPicture(FileName,LinkToFile,SaveWithDocument,Left,Top,Width,Height)
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) |
sht.api.Shapes.AddPicture(root+”/picpy.jpg”, True, True, 100, 50, 100, 100)
Sample Code
#Add Picture
import xlwings as xw #Import xlwings package
import os
root = os.getcwd()
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
sht.api.Shapes.AddPicture(r"D:\picpy.jpg", True, True, 100, 50, 100, 100)
#bk.save()
#bk.close()
#app.kill()

Leave a Reply