简体   繁体   English

如何使用python的pptx模块调整圆环图的大小

[英]How can adjust the size of doughnut chart using python's pptx module

I want to have multiple doughnut charts (max 3) using python's pptx module.我想使用 python 的 pptx 模块有多个圆环图(最多 3 个)。 As of now I'm able to add only one chart, how can I reduce the size of the doughnuts accordingly so that I can adjust 2 or 3 charts in one slide.截至目前,我只能添加一个图表,如何相应地减小甜甜圈的大小,以便我可以在一张幻灯片中调整 2 或 3 个图表。 Also, how can I auto adjust the size of doughnuts depending on number of graphs with different set of values.此外,如何根据具有不同值集的图表数量自动调整甜甜圈的大小。

python code: python 代码:

from pptx import Presentation
from pptx.util import Pt, Cm, Inches
from pptx.chart.data import CategoryChartData, ChartData
from pptx.enum.chart import XL_CHART_TYPE


def create_ppt():
    
    pr = Presentation()
    slide1_register = pr.slide_layouts[0]

    #add initial slide
    slide1 = pr.slides.add_slide(slide1_register)

    #top placeholder
    title1 = slide1.shapes.title
    #subtitle
    sub_title = slide1.placeholders[1]

    # insert text
    title1.text = "Data"

    title_para = title1.text_frame.paragraphs[0]
    title_para.font.name = "Arial (Headings)"
    title_para.font.size = Pt(34)
    title1.top = Cm(1)
    title1.left = Cm(0)
    title1.width = Cm(15)
    title1.height = Cm(3)

    chart_data = ChartData()
    chart_data.categories = ['X1', 'X2', 'X3', 'X4']
    chart_data.add_series('Data 1', (75, 10, 5, 4))
    x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
    chart = slide1.shapes.add_chart(
        XL_CHART_TYPE.DOUGHNUT, x, y, cx, cy, chart_data
    ).chart
    chart.has_legend = True
    chart.legend.include_in_layout = False
    apply_data_labels(chart)

    pr.save("test222222222.pptx")
    
def apply_data_labels(chart):
    plot = chart.plots[0]
    plot.has_data_labels = True
    for series in plot.series:
        values = series.values
        counter = 0
        for point in series.points:
            data_label = point.data_label
            data_label.has_text_frame = True
            data_label.text_frame.text = str(values[counter])
            counter = counter + 1
    
    
create_ppt()

current slide:当前幻灯片: 幻灯片1

expected output:预计 output: 预期回应

As you insert the chart, you can specify its position and size, as you already do:插入图表时,您可以指定其 position 和大小,就像您已经做的那样:

x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide1.shapes.add_chart(XL_CHART_TYPE.DOUGHNUT, x, y, cx, cy, chart_data)

In the code above x and y are the coordinates of the top left corner of the chart, while cx and cy are the width and height, respectively.在上面的代码中, xy是图表左上角的坐标,而cxcy分别是宽度和高度。

You can repeat the add_chart call as many times as many chart you want to insert with proper position and size values.您可以使用适当的 position 和大小值重复调用add_chart多次,次数与要插入的图表一样多。

See the documentation for shapes.add_chart function here .请在此处查看shapes.add_chart function 的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM