-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Xiaoqi Zhao
committed
Dec 13, 2023
1 parent
958877c
commit d73b630
Showing
9 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+33.9 KB
part2_Use-Python-in-Office/ch08_work-with-PPT/8-8_add-charts/8-8-2.pptx
Binary file not shown.
35 changes: 35 additions & 0 deletions
35
part2_Use-Python-in-Office/ch08_work-with-PPT/8-8_add-charts/8-8-2_line-chart.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+33.7 KB
part2_Use-Python-in-Office/ch08_work-with-PPT/8-8_add-charts/8-8-3.pptx
Binary file not shown.
Binary file added
BIN
+33.9 KB
part2_Use-Python-in-Office/ch08_work-with-PPT/8-8_add-charts/8-8-3_1.pptx
Binary file not shown.
34 changes: 34 additions & 0 deletions
34
part2_Use-Python-in-Office/ch08_work-with-PPT/8-8_add-charts/8-8-3_scatter-chart.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
53 changes: 53 additions & 0 deletions
53
part2_Use-Python-in-Office/ch08_work-with-PPT/8-8_add-charts/8-8-3_scatter-chart_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |