[英]How to set the font color of the data labels of the doughnut chart using python-pptx?
[英]Set data labels text frame wrap to false – python-pptx
我問這個問題的副本,除了提交的答案對我不起作用。 我想通過 python-pptx 從 powerpoint UI 切換 data_labels 的“Wrap text in shape”按鈕。 鏈接的答案最終完全刪除了數據標簽。 我使用的是最新的 python-pptx 版本 (0.6.18)。
這是一個簡單的復制示例:
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm
from pptx.text.text import TextFrame
# create presentation with 1 slide ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
x = ['one','two','three', 'four']
y = [['diff',
[1,
2,
3,
4,
]],
]
specs = {
'height': Cm(7.82),
'width': Cm(14.8),
'left': Cm(2.53),
'top': Cm(5.72)}
data = ChartData()
data.categories = x
data.add_series('diff', [j for j in y[0][1]])
frame = slide.shapes.add_chart(
XL_CHART_TYPE.BAR_CLUSTERED, specs['left'], specs['top'],
specs['width'], specs['height'], data
)
plot = frame.chart.plots[0]
plot.has_data_labels = True
data_labels = plot.series[0].data_labels
dLbls = data_labels._element
# ---use its <c:txPr> child to create TextFrame object---
text_frame = TextFrame(dLbls.get_or_add_txPr(), None)
# ---turn off word-wrap in the usual way---
text_frame.wrap = False
prs.save('chart-01.pptx')
我相信倒數第二行應該是text_frame.word_wrap = False
,而不是.wrap
; 這是我在早期答案中的錯誤(現已修復)。
也改變這一行:
data_labels = plot.series[0].data_labels
到:
data_labels = plot.data_labels
我想你會得到你想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.