scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.46k stars 529 forks source link

How to set the Value Axis of a chart to No line #669

Closed vba34520 closed 3 years ago

vba34520 commented 3 years ago

image

I want to set the Value Axis of a chart to No line. But I can't find the related information from documents of python-pptx and PowerPoint VBA .

Here is my code.

from pptx.util import Inches
from pptx import Presentation
from pptx.enum.chart import XL_CHART_TYPE
from pptx.chart.data import CategoryChartData

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

chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
prs.save('test.pptx')

I want it looks like this.

image

Looking forward to your reply. Best Wishes!

vba34520 commented 3 years ago
from pptx.util import Inches
from pptx import Presentation
from docx.oxml.shared import OxmlElement
from pptx.enum.chart import XL_CHART_TYPE
from pptx.chart.data import CategoryChartData

def set_chart_value_axis_no_fill(chart):
    element = chart.element
    valAx = element.valAx_lst[0]
    node = OxmlElement('c:spPr')
    node1 = OxmlElement('a:ln')
    node2 = OxmlElement('a:noFill')
    node1.append(node2)
    node.append(node1)
    valAx.append(node)

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data)
chart = chart.chart
set_chart_value_axis_no_fill(chart)
prs.save('test.pptx')
scanny commented 3 years ago
chart.value_axis.format.line.fill.background()

Should also do the trick.