Is it possible to set two value axis in python-pptx (one left, one right)

非 Y 不嫁゛ 提交于 2019-12-13 20:24:16

问题


I frequently use python-pptx, with current version of 0.6.6....

Now I want to set up a chart with two value axis, one on the left, one on the right, just like this:

In the doc of python-pptx, value_axis is accessed by chart.value_axis, but I have not found any description about how to set two value axis (with its own maximum_scale/minimum_scale/major_unit).

Any clue on that?

Picture with two value axis

Following Scanny's answer, I dig a little bit into the lxml, but I found it do not work(with code attached below, just want to test whether the axPos changed to r will take effect or not). just like this post:

Python PPTX Bar Chart negative values

and I have the same problem, when I set invert_if_negative option, something may happen in the lxml layer, and make the manipulation of lxml values do no effect on ppt?

if 'import':
    from pptx import Presentation
    from pptx.util import *
    from pptx.chart.data import ChartData
    from pptx.enum.chart import *
    import pandas as pd

if __name__ == '__main__':

    prs = Presentation()    
    slide = prs.slides.add_slide(prs.slide_layouts[6])

    df = pd.DataFrame({'Term': ('1Y', '2Y', '3Y', '4Y', '5Y'), 'A': (2.3, 2.6, 2.7, 2.8, 3.0), 'B': (6, 7.1, 7.5, 6.9, 8)})    

    chart_data = ChartData()
    chart_data.categories = df.Term
    seriesA = chart_data.add_series('A', df.A)
    seriesB = chart_data.add_series('B',df.B)
#    seriesA.invert_if_negative = False
#    seriesB.invert_if_negative = False

    x, y, cx, cy = Inches(0.5), Inches(2), Inches(8.5), Inches(4.5)
    chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data).chart    

    cvaxis = chart.value_axis    
    axPos = cvaxis._element.find('{http://schemas.openxmlformats.org/drawingml/2006/chart}axPos')
    print (axPos.values())
    axPos.set('val','r')
    print (axPos.values())

    prs.save('test123.pptx')

回答1:


Secondary axes have not yet been implemented in python-pptx. You would have to develop some code that manipulates the XML using lxml calls. You can find more information on that by searching on "python-pptx workaround function".

The basic gist is that you use python-pptx to get as close as you can to the XML element(s) you need, and then use the lxml API "underlying" python-pptx to get the rest of the job done.



来源:https://stackoverflow.com/questions/45047694/is-it-possible-to-set-two-value-axis-in-python-pptx-one-left-one-right

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