热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在图表区域的Python-pptx图表中为图表赋予图表标题(不是幻灯片标题)

如何解决《如何在图表区域的Python-pptx图表中为图表赋予图表标题(不是幻灯片标题)》经验,为你挑选了1个好方法。

我正在尝试向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')

David Zemens.. 5

可能的错别字,区分大小写。当您执行操作时,Chart.chart_title您指的是 Chart而不是您的chart对象。同样,ChartTitle.has_text_frame指的不是您的ChartTitlechart

安装此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个图表。保存并关闭演示文稿。

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)

>>> type(chart.chart_title)

注意,文档指出:

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

如果您使用的是python 3.6,那么也许这就是为什么它无法按预期运行的原因,因此不支持python版本。



1> David Zemens..:

可能的错别字,区分大小写。当您执行操作时,Chart.chart_title您指的是 Chart而不是您的chart对象。同样,ChartTitle.has_text_frame指的不是您的ChartTitlechart

安装此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个图表。保存并关闭演示文稿。

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)

>>> type(chart.chart_title)

注意,文档指出:

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

如果您使用的是python 3.6,那么也许这就是为什么它无法按预期运行的原因,因此不支持python版本。


推荐阅读
author-avatar
D之phper
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有