英文:
Python XlsxWriter ChartSheet watermark does not work
问题
我使用Python的xlsxwriter库来创建PDF中的表格和图表。有一个选项可以通过设置页眉来添加水印图片。
这在工作表中可以正常工作,但对于图表工作表则不起作用。页眉文本在工作表和图表工作表上都显示。但页眉图片仅在工作表上显示。
有人知道如何在图表工作表上添加水印吗?
以下是代码示例:
import xlsxwriter
workbook = xlsxwriter.Workbook("chartsheet.xlsx")
# 添加一个工作表来保存数据。
worksheet = workbook.add_worksheet()
worksheet.set_header('&C&G<EXTISVISIBLE', {'image_center': 'watermark_draft.png'})
# 添加一个图表工作表。这是仅包含图表的工作表。
chartsheet = workbook.add_chartsheet()
chartsheet.set_header('&C&G<EXTISVISIBLE', {'image_center': 'watermark_draft.png'})
# 为标题添加格式。
bold = workbook.add_format({"bold": 1})
# 添加图表所需的工作表数据。
headings = ["Number", "Batch 1", "Batch 2"]
data = [
[2, 3, 4, 5, 6, 7],
[10, 40, 50, 20, 10, 50],
[30, 60, 70, 50, 40, 30],
]
worksheet.write_row("A1", headings, bold)
worksheet.write_column("A2", data[0])
worksheet.write_column("B2", data[1])
worksheet.write_column("C2", data[2])
# 创建一个新的柱状图。
chart1 = workbook.add_chart({"type": "bar"})
# 配置第一个系列。
chart1.add_series(
{
"name": "=Sheet1!$B$1",
"categories": "=Sheet1!$A$2:$A$7",
"values": "=Sheet1!$B$2:$B$7",
}
)
# 配置第二个系列。请注意,这里使用了替代语法来定义范围。
chart1.add_series(
{
"name": ["Sheet1", 0, 2],
"categories": ["Sheet1", 1, 0, 6, 0],
"values": ["Sheet1", 1, 2, 6, 2],
}
)
# 添加图表标题和一些轴标签。
chart1.set_title({"name": "Results of sample analysis"})
chart1.set_x_axis({"name": "Test number"})
chart1.set_y_axis({"name": "Sample length (mm)"})
# 设置Excel图表样式。
chart1.set_style(11)
# 将图表添加到图表工作表。
chartsheet.set_chart(chart1)
# 打开工作簿时,将图表工作表设为活动工作表。
chartsheet.activate()
workbook.close()
英文:
I use python xlsxwriter to create PDF tables and graphs. There is a option to put watermark pictures by using the header.
This works fine for worksheets but it does not work for chartsheets. The header text is shown on both worksheets and chartsheets. The header image is only shown on the worksheet.
Anyone knows how to get a watermark on a chartsheet?
Here is an code example for this
#######################################################################
#
# An example of creating an Excel chart in a chartsheet with Python
# and XlsxWriter.
#
# SPDX-License-Identifier: BSD-2-Clause
# Copyright 2013-2023, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter
workbook = xlsxwriter.Workbook("chartsheet.xlsx")
# Add a worksheet to hold the data.
worksheet = workbook.add_worksheet()
worksheet.set_header('&C&G&LTEXTISVISIBLE', {'image_center': 'watermark_draft.png'})
# Add a chartsheet. A worksheet that only holds a chart.
chartsheet = workbook.add_chartsheet()
chartsheet.set_header('&C&G&LTEXTISVISIBLE', {'image_center': 'watermark_draft.png'})
# Add a format for the headings.
bold = workbook.add_format({"bold": 1})
# Add the worksheet data that the charts will refer to.
headings = ["Number", "Batch 1", "Batch 2"]
data = [
[2, 3, 4, 5, 6, 7],
[10, 40, 50, 20, 10, 50],
[30, 60, 70, 50, 40, 30],
]
worksheet.write_row("A1", headings, bold)
worksheet.write_column("A2", data[0])
worksheet.write_column("B2", data[1])
worksheet.write_column("C2", data[2])
# Create a new bar chart.
chart1 = workbook.add_chart({"type": "bar"})
# Configure the first series.
chart1.add_series(
{
"name": "=Sheet1!$B$1",
"categories": "=Sheet1!$A$2:$A$7",
"values": "=Sheet1!$B$2:$B$7",
}
)
# Configure a second series. Note use of alternative syntax to define ranges.
chart1.add_series(
{
"name": ["Sheet1", 0, 2],
"categories": ["Sheet1", 1, 0, 6, 0],
"values": ["Sheet1", 1, 2, 6, 2],
}
)
# Add a chart title and some axis labels.
chart1.set_title({"name": "Results of sample analysis"})
chart1.set_x_axis({"name": "Test number"})
chart1.set_y_axis({"name": "Sample length (mm)"})
# Set an Excel chart style.
chart1.set_style(11)
# Add the chart to the chartsheet.
chartsheet.set_chart(chart1)
# Display the chartsheet as the active sheet when the workbook is opened.
chartsheet.activate()
workbook.close()
答案1
得分: 2
XlsxWriter中有一个bug,导致无法将页眉图像完全添加到图表工作表中。这个问题现在已经在GitHub主页上修复了:https://github.com/jmcnamara/XlsxWriter
英文:
It turns out there was a bug in XlsxWriter that prevented header image from being added (fully) to chartsheets. That is fixed now on the GitHub main: https://github.com/jmcnamara/XlsxWriter
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论