xlsxwrter:图表标题中的富文本格式 [英] xlsxwrter: rich text format in chart title
问题描述
是否可以使用 xlsxwriter
或任何其他 python-excel 模块(例如 openpyxl
)在图表标题和标签中使用富文本格式和特殊字符?
is it possible to use rich text format and special characters in chart titles and labels with xlsxwriter
, or with any other python-excel module (e.g. openpyxl
)?
import xlsxwriter
workbook = xlsxwriter.Workbook('rich_text_in_chart_title.xlsx')
worksheet = workbook.add_worksheet()
subscript = workbook.add_format({'font_script': 2})
worksheet.write_rich_string('A1',
'H',
subscript, '2',
'O'
)
# Add the worksheet data to be plotted
data = [10, 40, 50, 20, 10, 50]
worksheet.write_column('A3', data)
# Create a new chart object
chart = workbook.add_chart({'type': 'line'})
# Add a series to the chart
chart.add_series({'values': '=Sheet1!$A$3:$A$8'})
chart.set_title({'name': 'alpha: H2O'})
# Insert the chart into the worksheet.
worksheet.insert_chart('C1', chart)
workbook.close()
我想用希腊字母替换图表标题中的 alpha
,用下标 2 替换 H20
,就像它在单元格 A1 中一样.
I want to replace the alpha
in the chart title with a greek letter and the H20
with a subscripted 2 as it is in cell A1.
推荐答案
xlsxwriter 是否可以在图表标题和标签中使用富文本格式和特殊字符
is it possible to use rich text format and special characters in chart titles and labels with xlsxwriter
没有.这不受支持.
但是,您可以使用 UTF-8 Unicode 创建所需的标题,例如 Python3 示例:
However you could create the caption you need using UTF-8 Unicode, like this Python3 example:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_column.xlsx')
worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})
worksheet.write_column('A2', [2, 3, 4, 5, 6, 7],)
chart.add_series({'values': '=Sheet1!$A$1:$A$6'})
chart.set_title ({'name': 'α: H₂O'})
worksheet.insert_chart('C2', chart)
workbook.close()
输出:
这篇关于xlsxwrter:图表标题中的富文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!