Reputation: 71
I'm attempting to generate an image from a pyplot and place that image in a specific cell on a specific worksheet in Excel using xlwings. I've successfully generated the image and placed it on a worksheet, but my intent is to place multiple images in different positions on the worksheet.
Here's my code for generating the image:
#Plotting the bell-shaped curve for Critical Care
plt.style.use('seaborn')
ccfig = plt.figure(figsize=(6, 6))
plt.plot(cc_ndist, cc_normpdf, color='black', linestyle='dashed')
plt.scatter(cc_ndist, cc_normpdf, marker='o', s=25, color='gold')
# open an existing Excel workbook with existing empty worksheet(s)
wb = xw.Book(.....Score_2022_Q1.xlsb')
ws1 = wb.sheets['Metrics']
ws2 = wb.sheets['Critical Care']
# add figures to the workbook
ws2.pictures.add(ccfig, name='CC Plot', update=True)
Is there a way to either define the cell position in the .pictures.add() or in the variable of the worksheet?
Upvotes: 2
Views: 3245
Reputation: 71
# adding left and top for the correct syntax to place the picture at cell "B2"
ws2.pictures.add(ccfig, name='CC Plot', update=True,
left=ws2.range('B5').left, top=ws2.range('B5').top)
Upvotes: 3