简体   繁体   English

为什么我的 header 图像从我的模板中消失了?

[英]Why does my header image disappear from my template?

I made a template document in Excel which is mostly empty, except for the header which contains the logo of our department (below).我在 Excel 中制作了一个模板文档,其中大部分是空的,除了包含我们部门徽标的 header (下)。

模板文档

I then read this template document into python using openpyxl and populate it with a data frame.然后,我使用 openpyxl 将此模板文档读入openpyxl并用数据框填充它。 However, when I save the workbook and open the Excel doc, everything is intact except my image disappears.但是,当我保存工作簿并打开 Excel 文档时,除了我的图像消失之外,一切都完好无损。

How can I ensure my logo stays intact in the report?如何确保我的徽标在报告中保持完整? I know I can't insert an image into the header in openpyxl , so I was hoping by including it in my template (and never touching the header) it would stay but it isn't.我知道我无法将图像插入到 openpyxl 中的openpyxl中,所以我希望通过将它包含在我的模板中(并且从不触及标题)它会保留,但事实并非如此。

Code in Python Python 中的代码

from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows

wb = load_workbook('./template.xlsx')
ws = wb["Sheet1"]

for r in dataframe_to_rows(df, index=False, header=False):
    ws.append(r)

wb.save("{}".format(fn))

Unfortunately openpyxl does not support images at this time, see official documentation: https://openpyxl.readthedocs.io/en/stable/usage.html#read-an-existing-workbook可惜openpyxl目前不支持图片,请看官方文档: https://openpyxl.readthedocs.io/en/stable/usage.html#read-an-existing-workbook

openpyxl does currently not read all possible items in an Excel file so images and charts will be lost from existing files if they are opened and saved with the same name. openpyxl 目前不会读取 Excel 文件中的所有可能项目,因此如果以相同的名称打开和保存现有文件中的图像和图表,它们将会丢失。

So when you open the template file, the images are not read, thus not saved in the new file.因此,当您打开模板文件时,图像不会被读取,因此不会保存在新文件中。 Therefore, you may have to switch to using xlsxwriter.因此,您可能不得不改用 xlsxwriter。

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

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