Xlsxwriter 调整 header 图片大小
Xlsxwriter resize header image
我在 header.jpeg
中有一张图片
我想将该图像设置为 header 到 excel 文件。
我正在使用模块 pandas, numpy and xlsxwriter
代码如下所示
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})
它也适用于
worksheet.set_header('&C&G', {'image_center': 'header.jpeg'})
如何调整 header 中的图像大小,比如将其高度调整 80%?
根据文档我发现我可以使用
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.
谢谢
How can I resize the image in the header, let's say its height by 80%?
XlsxWriter 目前不支持该功能。您可以打开一个功能请求,我会考虑添加它。
或者,您可以使用其他应用程序或模块缩小图像。
只是想为您提供一个示例,说明如何使用 xlsxwriter 之外的另一个模块缩小图像,然后使用 xlsxwriter 将其放入 .xlsx 文件中。这个例子有一个相当大的警告:它不适用于 .jpeg,只能用于 .png。但此示例将允许您将高度更改为原始图像大小的 80%,并将生成的图像放在 header.
中
在此示例中使用 urllib.request 以便代码可重现(如果您使用的是本地文件,则不需要该模块)。它还使用 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')
要在 .xlsx 文件中查看生成的图像,请从 'view' 选项卡(excel 2013)将视图设置为 'page layout'。
我在 header.jpeg
我想将该图像设置为 header 到 excel 文件。
我正在使用模块 pandas, numpy and xlsxwriter
代码如下所示
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})
它也适用于
worksheet.set_header('&C&G', {'image_center': 'header.jpeg'})
如何调整 header 中的图像大小,比如将其高度调整 80%?
根据文档我发现我可以使用
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.
谢谢
How can I resize the image in the header, let's say its height by 80%?
XlsxWriter 目前不支持该功能。您可以打开一个功能请求,我会考虑添加它。
或者,您可以使用其他应用程序或模块缩小图像。
只是想为您提供一个示例,说明如何使用 xlsxwriter 之外的另一个模块缩小图像,然后使用 xlsxwriter 将其放入 .xlsx 文件中。这个例子有一个相当大的警告:它不适用于 .jpeg,只能用于 .png。但此示例将允许您将高度更改为原始图像大小的 80%,并将生成的图像放在 header.
中在此示例中使用 urllib.request 以便代码可重现(如果您使用的是本地文件,则不需要该模块)。它还使用 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')
要在 .xlsx 文件中查看生成的图像,请从 'view' 选项卡(excel 2013)将视图设置为 'page layout'。