Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give chart title to a chart in Python-pptx chart in Chart Area(Not the slide title)

I am trying to add text to chart title in chart area in a PPT Slide(Not the slide title).I have http://python-pptx.readthedocs.io/en/latest/dev/analysis/cht-chart-title.html this link if any text can be added to my chart but I couldn't find the solution.Here is my,

import numpy as np
import pandas as pd
import pyodbc
#import pptx as ppt 
import matplotlib.pyplot as plt
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.chart.data import XyChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches,Pt
from pptx.enum.chart import XL_LABEL_POSITION
from pptx.dml.color import RGBColor
from pptx.dml import fill
from pptx.chart.chart import ChartTitle
from pptx.chart.chart import Chart



cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                        "Server=SNAME;"
                        "Database=DNAME;"
                        "Trusted_Connection=yes;")
AvgResponseTimequery = 'EXEC [SLA].[MONTHWISEREPORT]'
df=pd.read_sql(sql=AvgResponseTimequery,con=cnxn)
#df




getprs = Presentation('D:\SLA Pyth\hubiC-06-20-2017 1_10_55\SLAPerformance.pptx')
slide = getprs.slides.add_slide(getprs.slide_layouts[5])
slide.shapes.title.text = 'Key Performance KPIs'

chart_data = ChartData()
chart_data.categories = df['Month'].values.tolist()
chart_data.add_series('Average Report Response time(Seconds)', tuple(df['Avg Response Time']))
x, y, cx, cy = Inches(0.5), Inches(2), Inches(9), Inches(3)

chart=slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
).chart

#chart.has_title = True
#Chart.chart_title = "Response Time in Seconds"  #Tried to add text here, I didnt get any error though
#ChartTitle.has_text_frame = True


chart.has_title = True
chart.chart_title = "Response Time in Seconds"
# Check the has_text_frame property of this chart's chart_title:
print(chart.has_text_frame)



plot = chart.plots[0]
plot.has_data_labels = True
data_labels = plot.data_labels

chart.series[0].format.fill.solid()
chart.series[0].format.fill.fore_color.rgb = RGBColor(46, 125, 137)

getprs.save('D:\SLA REPORT MONTH WISE\SALReport1.pptx')
like image 411
Shiva Prasad Avatar asked Jun 20 '17 17:06

Shiva Prasad


1 Answers

Possible typo, case-sensitivity. When you do Chart.chart_title you're referring to the class Chart not your chart object. Likewise, ChartTitle.has_text_frame refers to the class ChartTitle not your chart!

After installing this pptx package and debugging (I got error on chart.has_title, etc.), I think you need:

chart.chart_title.has_text_frame=True
chart.chart_title.text_frame.text='Response Time in Seconds'

NOTE: You don't need this line:

chart.chart_title.has_text_frame=True

Setting the text_frame.text will be sufficient.

Here is exact code I used to test. First, create a new presentation with only 1 slide. Remove all shapes/placeholders from that slide and insert 1 chart only. Save & close the presentation.

from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.chart.data import XyChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches,Pt
from pptx.enum.chart import XL_LABEL_POSITION
from pptx.dml.color import RGBColor
from pptx.dml import fill
from pptx.chart.chart import ChartTitle
from pptx.chart.chart import Chart

file = 'c:\debug\pres.pptx'
pres = Presentation(file)
slide = pres.slides[0]
chart = slide.shapes[0].chart
chart.chart_title.text_frame.text='my new chart title'
pres.save(file)

Further, from console, I see these types, indicating that chart.chart_title is not an instance of str object, etc:

>>> type(chart)
<class 'pptx.chart.chart.Chart'>
>>> type(chart.chart_title)
<class 'pptx.chart.chart.ChartTitle'>

Note, the documentation indicates:

Currently python-pptx requires Python 2.6, 2.7, 3.3 or 3.4.

If you are using python 3.6 then perhaps that is why it is not working as expected, it is not supported version of python.

like image 75
David Zemens Avatar answered Oct 18 '22 03:10

David Zemens