Python 3.x 如何在python pptx中向图表添加边框

Python 3.x 如何在python pptx中向图表添加边框,python-3.x,python-pptx,Python 3.x,Python Pptx,我有一个堆叠条形图,我想在图表周围加一个边框,但图表没有“线”属性: from pptx.chart.data import ChartData from pptx.enum.chart import XL_CHART_TYPE, XL_TICK_MARK, XL_LABEL_POSITION, XL_LEGEND_POSITION from pptx.dml.color import RGBColor from pptx.util import Inches, Pt # define

我有一个堆叠条形图,我想在图表周围加一个边框,但图表没有“线”属性:

    from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_TICK_MARK, XL_LABEL_POSITION, XL_LEGEND_POSITION
from pptx.dml.color import RGBColor
from pptx.util import Inches, Pt

# define chart data 
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
chart_data.add_series('Series 2', (10.2, 11.4, 6.7))
chart_data.add_series('Series 3', (2.2, 8.4, 1.7))



# add chart to slide
x, y, cx, cy = Inches(1), Inches(1), Inches(5), Inches(3.5)
ThisChart = ThisSlide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_STACKED, x, y, cx, cy, chart_data
).chart # the '.chart' is getting the chart object from the chart creation


#Put a border around the chart *This is the part that not working *
ThisBorder = ThisChart.line # <--- there is no line attribute
ThisBorder.color.rgb = RGBColor(0xFF, 0x00, 0x00) # hex
ThisBorder.width = inches(0.1)

#Give the chart a title
ThisChart.chart_title.has_text_frame = True
ThisChart.chart_title.text_frame.text = "My Chart Title"

# Give the chart a legend
ThisChart.has_legend = True
ThisChart.legend.position = XL_LEGEND_POSITION.RIGHT
ThisChart.legend.include_in_layout = False
ThisChart.legend.font.size = Pt(11)

#add data labels
plot = ThisChart.plots[0] # The first Series
plot.has_data_labels = True # display the value on the bar
plot.overlap = 100 # make the bars stacked
data_labels = plot.data_labels
data_labels.font.size = Pt(11)
data_labels.font.color.rgb = RGBColor(0,0,0)
data_labels.number_format = "#,##0"


ThisChart.value_axis.has_minor_gridlines = False
ThisChart.value_axis.has_major_gridlines = False
ThisChart.value_axis.tick_labels.font.size = Pt(12)
ThisChart.value_axis.tick_labels.number_format = "#,##0"
ThisChart.category_axis.tick_labels.font.size = Pt(12)

# Colour the bars, I'm not going to really use these colours, just getting things working :)
ThisChart.series[0].fill.solid()
ThisChart.series[0].fill.fore_color.rgb = RGBColor(244,66,152) # Pink hex: F44298, RGB: 244,66,152
ThisChart.series[1].fill.solid()
ThisChart.series[1].fill.fore_color.rgb = RGBColor(66,134,244) # Blue hex: 4286F4, RGB: 66,134,244
ThisChart.series[2].fill.solid()
ThisChart.series[2].fill.fore_color.rgb = RGBColor(92,244,66) # Green hex: 5CF442, RGB: 92,244,66
从pptx.chart.data导入ChartData
从pptx.enum.chart导入XL\图表\类型、XL\勾号\标记、XL\标签\位置、XL\图例\位置
从pptx.dml.color导入RGBColor
从pptx.util导入英寸,Pt
#定义图表数据
图表数据=图表数据()
图表_data.categories=[‘东部’、‘西部’、‘中西部’]
图表数据。添加数据系列(‘系列1’,(19.2、21.4、16.7))
图表数据。添加数据系列(‘系列2’,(10.2、11.4、6.7))
图表数据。添加数据系列(‘系列3’,(2.2、8.4、1.7))
#将图表添加到幻灯片
x、 y,cx,cy=英寸(1),英寸(1),英寸(5),英寸(3.5)
ThisChart=ThisSlide.shapes.add\u图表(
XL\图表\类型。列\堆叠、x、y、cx、cy、图表\数据
).chart#“.chart”正在从图表创建中获取图表对象
#在图表周围加上边框*这是不起作用的部分*

ThisBorder=ThisChart.line#此功能尚未在
python pptx
中实现。如果是,它将是
Chart.plot\u area.format.line
,以防您想在GitHub repo上为它添加功能请求

同时,您有两个选择,如果包括“不做就做”选项,则有三个选择:

第一种方法是在“开始”或“模板”演示文稿中包含一个按您希望的方式格式化的图表,并使用
chart.replace_data()
方法仅更改它所描述的数据。这给了你真正广泛的控制,但它需要你提前知道你想要多少图表和在哪里


第二种方法是使用
lxml
level调用来操作底层XML,以添加所需的元素并适当地设置属性。如果搜索“python pptx变通功能”,您可以找到有关这种方法的更多信息。

是的,非常正确,我已经解决了这个问题。这两本书都是我写的,有时会像我孩子的名字一样混淆:)