简体   繁体   English

如何使用 xlsxwriter 和 python 将图像添加到 xlsx 文件的标题中?

[英]How can i add an image to a header of a xlsx file using xlsxwriter and python?

I want to add an image to the header of the xlsx but it's showing nothing on the generated file (our application picks a .csv, then converts to .xlsx using .py file with xlsxwriter, and then to .pdf using a libreoffice command)我想在 xlsx 的标题中添加一个图像,但它在生成的文件中没有显示任何内容(我们的应用程序选择一个 .csv,然后使用带有 xlsxwriter 的 .py 文件转换为 .xlsx,然后使用 libreoffice 命令转换为 .pdf)

We've already tried with different image formats and sizes but it made no difference.我们已经尝试过不同的图像格式和大小,但没有任何区别。

Also tried with the examples from the library ( https://xlsxwriter.readthedocs.io/example_headers_footers.html?highlight=set_header ) with no luck.还尝试了库中的示例( https://xlsxwriter.readthedocs.io/example_headers_footers.html?highlight=set_header ),但没有成功。

We used the worksheet.insert_image() , it adds the image but not in the header.我们使用了worksheet.insert_image() ,它添加了图像但不在标题中。 This is our current result: https://ibb.co/QNXv8bM这是我们目前的结果: https : //ibb.co/QNXv8bM

We want to add the image directly on the header (maybe using the set_header() ) but so far our tries with this method hasn't produced any results.我们想直接在标题上添加图像(可能使用set_header() ),但到目前为止我们尝试使用此方法还没有产生任何结果。 When we use the set_header() to place the image it shows nothing on the header.当我们使用set_header()放置图像时,它在标题上不显示任何内容。

Here is a piece of the python file that we are using:这是我们正在使用的 Python 文件的一部分:

def create_worksheet(workbook, data, image, p_header_text):
    '''
    Creates and formats worksheet
    :param workbook: Main workbook
    :type workbook: xlsxwriter.Workbook
    :param data: dict with data to use in the worksheet
    :type data: dict
    data example:
    data = {'headings': [head1, head2, ..., headn], 'rows': [[data1, ..., datan], ...]}
    :return: Nothing
    '''
    worksheet = workbook.add_worksheet()
    ### Page Setup
    worksheet.set_margins(top=1.4)
    worksheet.set_landscape()
    worksheet.hide_gridlines(2)
    #worksheet.set_paper(9)  # 9 = A4
    worksheet.fit_to_pages(1, 0)
    ### Header and footer
    header_text = p_header_text
    #worksheet.set_header('&C&16&"Calibri,Bold"{}'.format(header_text))
    worksheet.set_header('&L&G', {'image_left': '/home/reports/LTA-logo.jpg'})


    worksheet.set_footer('&L&D&RPage &P of &N')
    #worksheet.insert_image('A1', '/home/reports/LTA-logo.jpg', {'x_offset': 0, 'y_offset': 0})
    #worksheet.set_header('&C&G', {'image_left': '/home/reports/LTA-logo.jpg'})

    ### Create table
    create_table(worksheet, data)

Note : The worksheet.set_header('&C&16&"Calibri,Bold"{}'.format(header_text)) works fine, it shows the text on the header.注意worksheet.set_header('&C&16&"Calibri,Bold"{}'.format(header_text))工作正常,它在标题上显示文本。 The problem is when we try to put the image...问题是当我们尝试将图像...

The expected result is to make the image appear in the header, left aligned with the title as shown on this picture: https://ibb.co/vQTytK2预期的结果是使图像出现在标题中,与标题左对齐,如图所示: https : //ibb.co/vQTytK2

Note 2 : For business reasons (company) i cannot show the data on the print screens注 2 :出于商业原因(公司),我无法在打印屏幕上显示数据

It should work with XlsxWriter.它应该与 XlsxWriter 一起使用。 You just need to build the format string in the right way with the &L left part and the &C centre part.您只需要使用&L左部分和&C中心部分以正确的方式构建格式字符串。

For example:例如:

import xlsxwriter

workbook = xlsxwriter.Workbook('headers_footers.xlsx')
worksheet = workbook.add_worksheet('Image')

# Adjust the page top margin to allow space for the header image.
worksheet.set_margins(top=1.3)

worksheet.set_header('&L&[Picture]&C&16&"Calibri,Bold"Revenue Report',
                     {'image_left': 'python-200x80.png'})

workbook.close()

Note, I use the more explicit &[Picture] in the example but &G works as well.请注意,我在示例中使用了更明确的&[Picture] ,但&G也可以使用。

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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