简体   繁体   中英

resize excel cell to fit an image

when I write an image to an excel file, I need to make the image fit one excel cell. I know the size(in pixel) of the image, but I have no idea how to use xlsxwriter to adjust the height and width of the excel cell so that my image fits the cell perfectly.

Providing an example of how you could scale down an image using PIL (Python Imaging Library - Link to docs ) and then place it in an .xlsx file using xlsxwriter. With this example, you can see how you can adjust the size of your image using PIL and then using worksheet.set_column() and worksheet.set_row() you can adjust the cell size to accomodate the size of your image.

So effectively, you can adjust the size of your image, adjust the size of the excel cell or do both. It might take a little testing to get the right fit.

To make this answer reproducible I used urllib module to download an image to the local directory (that module is not necessary if you're using a local file).

import urllib.request
from PIL import Image
import xlsxwriter
import os

url = 'https://upload.wikimedia.org/wikipedia/en/thumb/4/43/Ipswich_Town.svg/255px-Ipswich_Town.svg.png'

urllib.request.urlretrieve(url, "local_100_perc.png")

with Image.open("local_100_perc.png") as img:
    width_100 = img.width
    height_100 = img.height

width_30 = int(round(width_100 * 0.3, 0))
img = Image.open('local_100_perc.png')
wpercent = (width_30/float(width_100))
hsize = int((float(height_100)*float(wpercent)))
img = img.resize((width_30,hsize), Image.ANTIALIAS)
img.save('local_30_perc.png') 

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column('A:B', 10)
worksheet.set_row(1, 70)
worksheet.write('A2', 'Image:')
worksheet.insert_image('B2', 'local_30_perc.png')

workbook.close()

With expected Output:

预期的 test.xlsx

It seems like it is so much tricky to set the cell size in pixels. As it is explained in xlsxwriter's docs , and Microsoft excel's docs .

The width parameter sets the column width in the same units used by Excel which is: the number of characters in the default font. The default width is 8.43 in the default font of Calibri 11. The actual relationship between a string width and a column width in Excel is complex. See the following explanation of column widths from the Microsoft support documentation for more details.

Worst part is that the equivalent value of pixels for unit width differs from the equivalent value of pixels for unit height, means 500 units for height is 4000 pixels and 500 units for width is about 400 pixels. These ratios are also changeable. so it may differ for you. But when you get size of a row or column, xlsxwriter returns the pixel value. Maybe with a trial and error, best units of height and width can be found.

import xlsxwriter
workbook = xlsxwriter.Workbook('excel.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column(0, 0, 50)
worksheet.set_row(0, 50)
c = worksheet._size_col(0)
r = worksheet._size_row(0)
print(r, c)
workbook.close()

results are:

66 355

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM