簡體   English   中英

如何在圖表區域的Python-pptx圖表中為圖表提供圖表標題(不是幻燈片標題)

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

我試圖在PPT幻燈片(不是幻燈片標題)的圖表區域中添加文本到圖表標題。我有http://python-pptx.readthedocs.io/en/latest/dev/analysis/cht-chart-title .html此鏈接如果有任何文本可以添加到我的圖表但我找不到解決方案。這是我的,

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')

可能的拼寫錯誤,區分大小寫。 當您執行Chart.chart_title您指的是 Chart而不是您的chart對象。 同樣, ChartTitle.has_text_frame指的是ChartTitle.has_text_frame ChartTitle不是你的chart

安裝此pptx軟件包並進行調試后(我在chart.has_title上出現錯誤等),我認為您需要:

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

注意:您不需要此行:

chart.chart_title.has_text_frame=True

設置text_frame.text就足夠了。

這是我用來測試的確切代碼。 首先,只用1張幻燈片創建一個新的演示文稿。 從該幻燈片中刪除所有形狀/占位符,僅插入1個圖表。 保存並關閉演示文稿。

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)

此外,從控制台,我看到這些類型,表明chart.chart_title不是str對象的實例,等等:

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

請注意, 文檔指出

目前python-pptx需要Python 2.6,2.7,3.3或3.4。

如果你正在使用python 3.6那么也許這就是它沒有按預期工作的原因,它不支持python版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM