scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.37k stars 513 forks source link

How to plot add vertical in line chart with labels and titles in power point using python-pptx module? #661

Open pratikchhapolika opened 3 years ago

pratikchhapolika commented 3 years ago

I have a dataset which looks like this:

 A       B       C       D
AR    20201     1     200
AR    20202     1     300
AR    20203     1     1000
AR    20204     1     3000
AR    20205     1     700
AR    20206     1     800
AR    20207     1     900

AR    20201     2    1000
AR    20202     2    2000
AR    20203     2    3000
AR    20204     2    4000
AR    20205     2    5676
AR    20206     2    6000
AR    20207     2    1000

AR    20201     3   4500
AR    20202     3   4567
AR    20203     3   1000
AR    20204     3   900
AR    20205     3   600
AR    20206     3   200
AR    20207     3  100

==========================
BR    20201     1
BR    20202     1
BR    20203     1
BR    20204     1
BR    20205     1
BR    20206     1
BR    20207     1

BR    20201     2

I am using loop to create multile line charts in ppt. For country ( Column A)=AR I am plotting line charts for different C=1,2 and 3 on same plot. In the Y axis I will have column D and on X axis I will have column B

Similary for different country.

Question 1: I am not able to choose particular line graph available in ppt which also shows Y-DATA LABELS. Also my X-axis is not properly formatted. How to align x labels 45 degree slanting.

Here is the code for one country:

chart_data = ChartData()
cat=['202001','202002','202003','202004','202005','202006','202007']
chart_data.categories = cat

for i in list(data["C"].unique()):

    chart_data.add_series(i,tuple(data[data['C']==i]['D']))
    x, y, cx, cy = Inches(1), Inches(1), Inches(10.5), Inches(6)
    chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data).chart

    chart.has_legend = True
    chart.legend.include_in_layout = False
    chart.series[0].smooth = True

Here is the output that I get:

eLtBv

Expected Output is

covid

Question 2: How can I add a dotted vertical line, move the legend to the top and change font size of legend, and finally to add xlabel, ylabel and titles using the above code?

Please help.

iPinaki commented 7 months ago

Looks like this is still open. Appreciate if anyone can give some idea how to do it!