Python 是否可以使用openpyxl更改图表中的字体大小?

Python 是否可以使用openpyxl更改图表中的字体大小?,python,excel,charts,openpyxl,Python,Excel,Charts,Openpyxl,我在Windows上使用Python 2.7版和openpyxl 2.4.0版。我需要更改图表(标题/图例)中的字体大小。可能吗?我在openpyxl文档和在线搜索了所有地方,但什么也找不到 我试着用它 from openpyxl import Workbook from openpyxl.chart import Reference, Series, LineChart, BarChart from openpyxl.chart.text import RichText from openpy

我在Windows上使用Python 2.7版和openpyxl 2.4.0版。我需要更改图表(标题/图例)中的字体大小。可能吗?我在openpyxl文档和在线搜索了所有地方,但什么也找不到

我试着用它

from openpyxl import Workbook
from openpyxl.chart import Reference, Series, LineChart, BarChart
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font

chart = BarChart()
chart.type = 'col'
chart.style = 20
chart.y_axis.title = 'Stress, MPa'
data = Reference(ws, min_col=6, min_row=2, max_row=q-1, max_col=7)
cats = Reference(ws, min_col=1, min_row=3, max_row=q-1)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)
chart.shape = 4

font_test = Font(typeface='Calibri')
cp = CharacterProperties(latin=font_test, sz=1500)
chart.y_axis.textProperties = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])

ws.add_chart(chart, "I31")
当我使用它时,我在Excel中有一个错误(“无法显示内容”)。但是我的代码通过时没有错误

是的,这是可能的

>>> from openpyxl.styles import colors
>>> from openpyxl.styles import Font, Color
>>> from openpyxl import Workbook
>>> wb = Workbook()
>>> ws = wb.active
>>>
>>> a1 = ws['A1']
>>> d4 = ws['D4']
>>> ft = Font(color=colors.RED)
>>> a1.font = ft
>>> d4.font = ft
>>>
>>> a1.font.italic = True # is not allowed 
>>>
>>> # If you want to change the color of a Font, you need to reassign it::
>>>
>>> a1.font = Font(color=colors.RED, italic=True) # the change only affects A1

来源:

这是更改字体颜色和大小的最短方法

list_rows_3 = []
main_heading = ['Test',' ', 'Group Name', 'Result Status', ' ', 'Count']
m1 = [ font_color(item,20,'000000') for item in main_heading ]
list_rows_3.append(m1)

我需要更改图表标题中的字体大小,而不是单元格中的字体大小。对不起,您可以通过CHART.title.txPr属性进行更改。他正在更改x_axis.txPr,但这适用于CHART.title.txPr。原理类似,但图表中的文本格式由drawingML覆盖,这要复杂得多。当我使用它时,我在Excel中有一个错误(“无法显示内容”)。但是我的代码通过时没有出现错误。文档中没有,但OOXML规范涵盖了它。您必须准备好使用它并使用XML源代码。