Xlsxwriter resize header image

Viewed 1641

I have an image in header.jpeg

I want to set that image as a header to an excel file. I am using modules pandas, numpy and xlsxwriter

the code looks like this

image_file = open('header.jpeg', 'rb')
image_data = xlsxwriter.compatibility.BytesIO(image_file.read())

worksheet.set_header('&C&G', {'image_center': 'header.jpeg','image_data_left': image_data})

It also works with just

worksheet.set_header('&C&G', {'image_center': 'header.jpeg'})

How can I resize the image in the header, let's say its height by 80%?

accoring to docs I have found that I can use

http://xlsxwriter.readthedocs.io/page_setup.html

The available options are:

margin: (float) Header margin in inches. Defaults to 0.3 inch. image_left: (string) The path to the image. Needs &G placeholder. image_center: (string) Same as above. image_right: (string) Same as above. image_data_left: (BytesIO) A byte stream of the image data. image_data_center: (BytesIO) Same as above. image_data_right: (BytesIO) Same as above. scale_with_doc: (boolean) Scale header with document. Defaults to True. align_with_margins: (boolean) Align header to margins. Defaults to True.

Thanks

2 Answers

How can I resize the image in the header, let's say its height by 80%?

That isn't currently supported in XlsxWriter. You can open a feature request and I'll look at adding it.

Alternatively, you could scale the image down using another application or module.

Just wanted to provide you with an example of how you can scale down an image using another module outside of xlsxwriter and then place it in a .xlsx file using xlsxwriter. This example comes with a rather big caveat: it will not work with a .jpeg, only with .png. But this example will allow you to change the height 80% of it's original image size and place the resulting image in the header.

It uses urllib.request in this example so that the code is reproducible (that module is not necessary if you're using a local file). It also uses PIL (Python Imaging Library).

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_80 = int(round(width_100 * 0.8, 0))

img = Image.open('local_100_perc.png')
wpercent = (width_80/float(width_100))
hsize = int((float(height_100)*float(wpercent)))
img = img.resize((width_80,hsize), Image.ANTIALIAS)
img.save('local_80_perc.png') 


workbook = xlsxwriter.Workbook('headers.xlsx')

worksheet1 = workbook.add_worksheet('Image_100_perc')
header1 = '&L&G'
worksheet1.set_margins(top=3.8)
worksheet1.set_header(header1, {'image_left': 'local_100_perc.png'})
worksheet1.write('A1', '100 percent image in Header.')

worksheet2 = workbook.add_worksheet('Image_80_perc')
header2 = '&L&G'
worksheet2.set_margins(top=3)
worksheet2.set_header(header2, {'image_left': 'local_80_perc.png'})
worksheet2.write('A1', '80 percent image in Header.')

workbook.close()

# uncomment next two lines if you want to delete both pictures from your current directory
#os.remove('local_100_perc.png')
#os.remove('local_80_perc.png')   

To view the resulting images in the .xlsx file, set view to 'page layout' from the 'view' tab (excel 2013).

Related