简体   繁体   English

将数据标签文本框换行设置为false – python-pptx

[英]Set data labels text frame wrap to false – python-pptx

I am asking a duplicate of this question , except that the answer submitted does not work for me.我问这个问题的副本,除了提交的答案对我不起作用。 I would like to toggle the data_labels' "Wrap text in shape" button from the powerpoint UI via python-pptx.我想通过 python-pptx 从 powerpoint UI 切换 data_labels 的“Wrap text in shape”按钮。 The linked answer ends up removing the data labels altogether instead.链接的答案最终完全删除了数据标签。 I am using the latest python-pptx version (0.6.18).我使用的是最新的 python-pptx 版本 (0.6.18)。

Here is a simple example to replicate:这是一个简单的复制示例:

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

I believe the second to last line should be text_frame.word_wrap = False , not .wrap ;我相信倒数第二行应该是text_frame.word_wrap = False ,而不是.wrap that's my mistake on the earlier answer (now fixed).这是我在早期答案中的错误(现已修复)。

Also change this line:也改变这一行:

data_labels = plot.series[0].data_labels

to:到:

data_labels = plot.data_labels

And I think you'll get what you're looking for.我想你会得到你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM