Python Forum
openpyxl insert picture problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
openpyxl insert picture problem
#1
I want to insert a picture into the Excel cell.
The image size needs to fit the cell (even the merged cell) border automatically.
I can find an example to insert a picture in Excel.
But I failed to get the proper image size from the cell border.
Reply
#2
(May-02-2023, 01:43 AM)cools0607 Wrote: failed to get the proper image size from the cell border.
What exactly does it mean to get image size from cell border?

Also, please include your code (minimal reproducible example)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(May-02-2023, 08:09 AM)buran Wrote:
(May-02-2023, 01:43 AM)cools0607 Wrote: failed to get the proper image size from the cell border.
What exactly does it mean to get image size from cell border?

Also, please include your code (minimal reproducible example)



I attached my code below
import openpyxl
from openpyxl.drawing.image import Image

fn = "SS1.xlsx"
wb = openpyxl.load_workbook(fn)
ws = wb['AA']

cell='B2'     #insert img to B2 cell

# get cell size
width =  ws.column_dimensions['2'].width
height = ws.row_dimensions[2].height


img = Image("my_img.jpg")     # build img
img.width = width          # chamge img width same as cell's width
img.height = height         # chamge img height same as cell's height
ws.add_image(img,cell)      # insert img to B2

wb.save('out.xlsx')             #save file
add attchements (py code/ image/ excel)

Attached Files

.zip   openpyxl.zip (Size: 55.86 KB / Downloads: 46)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,538 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  openpyxl insert list with formulas Irv1n 1 1,561 Sep-16-2021, 08:10 AM
Last Post: Irv1n
  Picture changing triggered by GPIO q_nerk 2 2,591 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  P3, openpyxl, csv to xlsx, cell is not number, problem with colorize genderbee 1 2,168 Sep-29-2020, 03:20 PM
Last Post: Larz60+
  How to scrolling Picture in x axis kalihotname 1 2,261 Jun-16-2020, 12:18 PM
Last Post: DeaD_EyE
  openpyxl problem Sheeper 2 7,918 Mar-14-2020, 06:54 AM
Last Post: buran
  How to get the picture from gender dataset vokoyo 1 2,462 May-03-2019, 12:20 AM
Last Post: micseydel
  cropping a picture (always square) Leon 1 2,148 Aug-13-2018, 10:04 AM
Last Post: Leon
  Why A will be printed twice in the picture Shen 3 4,096 Jul-25-2018, 01:16 PM
Last Post: stranac
  MySQL INSERT Problem gw1500se 5 3,953 Jul-13-2018, 10:27 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020