Skip to content

Commit

Permalink
add 8-8 (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoqi Zhao committed Dec 13, 2023
1 parent 958877c commit d73b630
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 5 deletions.
Binary file modified Python-with-Office-Video_Covers.pptx
Binary file not shown.
6 changes: 4 additions & 2 deletions Python_in_Office.mm
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,8 @@
<arrowlink DESTINATION="ID_1963511707"/>
</node>
<node TEXT="2. Picture" ID="ID_734161837" CREATED="1702347586920" MODIFIED="1702411462993">
<arrowlink DESTINATION="ID_1749082627"/>
<arrowlink DESTINATION="ID_1963511707"/>
<arrowlink DESTINATION="ID_1749082627"/>
</node>
<node TEXT="3. Graphic Frame" ID="ID_758421842" CREATED="1702347606737" MODIFIED="1702411468502">
<arrowlink DESTINATION="ID_1963511707"/>
Expand Down Expand Up @@ -913,7 +913,9 @@
</node>
</node>
<node TEXT="8.8 Add Chart 添加图表&#xa;shape.add_chart()" POSITION="bottom_or_right" ID="ID_1724032330" CREATED="1697648680854" MODIFIED="1702430781080">
<node TEXT="8.8.1 Column Chart 柱状图" ID="ID_117954374" CREATED="1702306461528" MODIFIED="1702310172912"/>
<node TEXT="8.8.1 Column Chart 柱状图" ID="ID_117954374" CREATED="1702306461528" MODIFIED="1702310172912">
<node TEXT="add data table" ID="ID_1467510577" CREATED="1702504559956" MODIFIED="1702504569072" LINK="https://github.com/scanny/python-pptx/issues/373"/>
</node>
<node TEXT="8.8.2 Line Chart 折线图" ID="ID_1505810850" CREATED="1702306479400" MODIFIED="1702310180005"/>
<node TEXT="8.8.3 X Y (Scatter) Chart 散点图" ID="ID_1551411475" CREATED="1702306500047" MODIFIED="1702310192314"/>
<node TEXT="8.8.4 Pie Chart 饼图" ID="ID_1204693924" CREATED="1702306511208" MODIFIED="1702310199057"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@

chart.legend.include_in_layout = False

data_labels = chart.data_labels

data_labels.position = XL_DATA_LABEL_POSITION.ABOVE
# currently there's no support for adding data table

ppt.save("./8-8-1_4.pptx")
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION
from pptx.util import Cm

ppt = Presentation()

chart_data = CategoryChartData()

chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']

chart_data.add_series('Digital', (36.6, 21.1, 15.9, 20.4))
chart_data.add_series('Life', (65.5, 52.1, 22.3, 35.3))
chart_data.add_series('Study', (10.0, 3.1, 9.8, 3.2))

slide = ppt.slides.add_slide(ppt.slide_layouts[6])

x = y = Cm(3)
width = Cm(20); height = Cm(10)

graphic_frame = slide.shapes.add_chart(
XL_CHART_TYPE.LINE,
x,
y,
width,
height,
chart_data
)

chart = graphic_frame.chart
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.TOP
chart.legend.include_in_layout = False

ppt.save("./8-8-2.pptx")
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pptx import Presentation
from pptx.chart.data import XyChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm

ppt = Presentation()

chart_data = XyChartData()

series_1 = chart_data.add_series("series one")
series_1.add_data_point(0.7, 2.7)
series_1.add_data_point(1.8, 3.2)
series_1.add_data_point(2.6, 0.8)

series_2 = chart_data.add_series("series two")
series_2.add_data_point(1.3, 3.7)
series_2.add_data_point(2.7, 2.3)
series_2.add_data_point(1.6, 1.8)

slide = ppt.slides.add_slide(ppt.slide_layouts[6])

x = y = Cm(3)
width = Cm(20); height = Cm(10)

slide.shapes.add_chart(
XL_CHART_TYPE.XY_SCATTER,
x,
y,
width,
height,
chart_data
)

ppt.save("./8-8-3.pptx")
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from pptx import Presentation
from pptx.chart.data import XyChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm

ppt = Presentation()

chart_data = XyChartData()

sale_info = [
{
"category": "digital",
"sale_volume": [36.6, 21.1, 15.9, 20.4]
},
{
"category": "life",
"sale_volume": [65.5, 52.1, 22.3, 35.3]
},
{
"category": "study",
"sale_volume": [10.0, 3.1, 9.8, 3.2]
},
]

for info in sale_info:
series = chart_data.add_series(info.get("category"))
for quarter, volume in enumerate(info.get("sale_volume")):
series.add_data_point(quarter + 1, volume, number_format='"Quarter "0')

slide = ppt.slides.add_slide(ppt.slide_layouts[6])

x = y = Cm(3)
width = Cm(20); height = Cm(10)

chart_frame = slide.shapes.add_chart(
XL_CHART_TYPE.XY_SCATTER,
x,
y,
width,
height,
chart_data
)

chart = chart_frame.chart
x_axis = chart.category_axis
x_axis.has_title = True
x_axis.axis_title.text_frame.text = "Quarter"
y_axis = chart.value_axis
y_axis_has_title = True
y_axis.axis_title.text_frame.text = "Sales Volume"
chart.has_legend = True # by default is True already

ppt.save("./8-8-3_1.pptx")

0 comments on commit d73b630

Please sign in to comment.