簡體   English   中英

是否可以使用 openpyxl 更改圖表中的字體大小?

[英]Is it possible to change size font in a chart using 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 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 中出現錯誤(“無法顯示內容”)。 但是我的代碼通過沒有錯誤

這是更改字體顏色和大小的最短方法

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)

對的,這是可能的

>>> 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

來源: openpyxl 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM