Python PPTX workaround function for rotating chart data labels

China☆狼群 提交于 2019-12-07 23:18:48

问题


I intend to create the following chart using Python PPTX.

Below code achieve the color setting, font size and number format. However, I am not yet able to rotate the data label, as I believe this API is not yet available in python-pptx 0.6.5

lbl = plot.data_labels
lbl.font.size = config["DATA_LABEL_FONT_SIZE"]
lbl.font.color.rgb = config["DATA_LABEL_FONT_COLOR"]
lbl.number_format = config["DATA_LABEL_NUMBER_FORMAT"]
lbl.position = config["DATA_LABEL_POSITION"]

To get started, I have created two minimal slides before and after rotating, and use opc-diag tool to find the diff.

<a:bodyPr rot="-5400000" spcFirstLastPara="1" vertOverflow="ellipsis"
vert="horz" wrap="square" lIns="38100" tIns="19050" rIns="38100" 
bIns="19050" anchor="ctr" anchorCtr="1">\n                 
<a:spAutoFit/>\n </a:bodyPr>\n

I believe I need to add rot="-5400000" XML element to lbl (plot.data_labels), but not clear on how to achieve this. I have used dir(), ._element and .xml on the chart and its children but not able to find <a:bodyPr> tag.


回答1:


I tried below and it works.

if config["DATA_LABEL_VERTICAL"]:
    txPr = lbl._element.get_or_add_txPr()
    txPr.bodyPr.set('rot','-5400000')


来源:https://stackoverflow.com/questions/44544701/python-pptx-workaround-function-for-rotating-chart-data-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!