### 🎯 启动会PPT生成 # -*- coding: utf-8 -*- """ 自动生成《预防一氧化碳中毒 · 小学生安全课堂》PPT 课件 运行前安装:pip install python-pptx 运行:python make_ppt.py 输出:预防一氧化碳中毒_小学生安全课件.pptx """ from pptx import Presentation from pptx.util import Inches, Pt from pptx.dml.color import RGBColor from pptx.enum.text import PP_ALIGN, MSO_ANCHOR from pptx.enum.shapes import MSO_SHAPE from pptx.oxml.ns import qn from lxml import etree # ---------------- 主题配色 ---------------- FONT = "微软雅黑" WHITE = RGBColor(0xFF, 0xFF, 0xFF) DEEP = RGBColor(0x10, 0x3A, 0x66) # 深蓝(封面) NAVY = RGBColor(0x18, 0x4C, 0x80) # 装饰圆 BLUE = RGBColor(0x1F, 0x6F, 0xB5) # 主蓝(标题栏) SKY = RGBColor(0xE8, 0xF2, 0xFB) # 浅蓝底 SKY2 = RGBColor(0xD6, 0xE8, 0xF8) RED = RGBColor(0xD9, 0x3A, 0x35) REDBG = RGBColor(0xFC, 0xE8, 0xE7) GREEN = RGBColor(0x2E, 0x9E, 0x5B) GREENBG= RGBColor(0xE7, 0xF6, 0xEC) YELLOW = RGBColor(0xFF, 0xC0, 0x00) GRAY = RGBColor(0x3C, 0x44, 0x4E) LGRAY = RGBColor(0x8A, 0x94, 0xA0) SW, SH = 13.333, 7.5 PRS = Presentation() PRS.slide_width = Inches(SW) PRS.slide_height = Inches(SH) # ---------------- 基础工具 ---------------- def set_run(run, text, size=20, bold=False, color=GRAY, font=FONT): run.text = text f = run.font f.size = Pt(size) f.bold = bold f.color.rgb = color f.name = font # 中文(东亚)字体 rPr = run._r.get_or_add_rPr() ea = rPr.find(qn('a:ea')) if ea is None: ea = etree.SubElement(rPr, qn('a:ea')) ea.set('typeface', font) def _write(tf, lines, size, bold, color, align, anchor, spacing, space_after): tf.word_wrap = True tf.vertical_anchor = anchor if isinstance(lines, str): lines = lines.split("\n") for i, ln in enumerate(lines): p = tf.paragraphs[0] if i == 0 else tf.add_paragraph() p.alignment = align p.line_spacing = spacing p.space_after = Pt(space_after) if isinstance(ln, str): ln = [(ln, size, bold, color)] if ln else [] for (t, sz, bd, cl) in ln: set_run(p.add_run(), t, sz, bd, cl) def text_box(slide, x, y, w, h, lines, size=20, bold=False, color=GRAY, align=PP_ALIGN.LEFT, anchor=MSO_ANCHOR.TOP, spacing=1.25, space_after=6): tb = slide.shapes.add_textbox(Inches(x), Inches(y), Inches(w), Inches(h)) tf = tb.text_frame tf.margin_left = tf.margin_right = tf.margin_top = tf.margin_bottom = 0 _write(tf, lines, size, bold, color, align, anchor, spacing, space_after) return tb def panel(slide, x, y, w, h, fill=SKY, line=None, lw=1.75, radius=0.12, shape=MSO_SHAPE.ROUNDED_RECTANGLE): shp = slide.shapes.add_shape(shape, Inches(x), Inches(y), Inches(w), Inches(h)) if shape == MSO_SHAPE.ROUNDED_RECTANGLE: shp.adjustments[0] = radius shp.fill.solid() shp.fill.fore_color.rgb = fill if line is None: shp.line.fill.background() else: shp.line.color.rgb = line shp.line.width = Pt(lw) shp.shadow.inherit = False tf = shp.text_frame tf.margin_left = Inches(0.25) tf.margin_right = Inches(0.25) tf.margin_top = Inches(0.18) tf.margin_bottom = Inches(0.18) return shp def fill_panel(shp, lines, align=PP_ALIGN.LEFT, anchor=MSO_ANCHOR.TOP, spacing=1.3, space_after=8): _write(shp.text_frame, lines, 20, False, GRAY, align, anchor, spacing, space_after) def badge(slide, x, y, d, text, fill=BLUE, color=WHITE, size=20): o = slide.shapes.add_shape(MSO_SHAPE.OVAL, Inches(x), Inches(y), Inches(d), Inches(d)) o.fill.solid() o.fill.fore_color.rgb = fill o.line.fill.background() o.shadow.inherit = False tf = o.text_frame tf.word_wrap = False tf.margin_left = tf.margin_right = tf.margin_top = tf.margin_bottom = 0 _write(tf, [[(text, size, True, color)]], size, True, color, PP_ALIGN.CENTER, MSO_ANCHOR.MIDDLE, 1.0, 0) def set_bg(slide, color): fill = slide.background.fill fill.solid() fill.fore_color.rgb = color def new_slide(bg=WHITE): s = PRS.slides.add_slide(PRS.slide_layouts[6]) set_bg(s, bg) return s def header(slide, title, page=None): bar = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, 0, 0, PRS.slide_width, Inches(1.05)) bar.fill.solid() bar.fill.fore_color.rgb = BLUE bar.line.fill.background() bar.shadow.inherit = False sq = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(0.5), Inches(0.33), Inches(0.12), Inches(0.4)) sq.fill.solid() sq.fill.fore_color.rgb = YELLOW sq.line.fill.background() sq.shadow.inherit = False text_box(slide, 0.8, 0.2, 10.6, 0.65, title, 28, True, WHITE, anchor=MSO_ANCHOR.MIDDLE) if page: text_box(slide, 11.4, 0.28, 1.3, 0.5, "%02d" % page, 18, True, RGBColor(0xBF, 0xD9, 0xF0), align=PP_ALIGN.RIGHT) def footer(slide): text_box(slide, 0.9, 6.98, 9.0, 0.32, "预防一氧化碳中毒 · 小学生安全课堂", 12, False, LGRAY) # ---------------- 第 1 页:封面 ---------------- def p01_cover(): s = new_slide(DEEP) for (x, y, d) in [(9.4, -1.9, 5.8), (11.3, 3.7, 3.2)]: o = s.shapes.add_shape(MSO_SHAPE.OVAL, Inches(x), Inches(y), Inches(d), Inches(d)) o.fill.solid() o.fill.fore_color.rgb = NAVY o.line.fill.background() o.shadow.inherit = False tag = s.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Inches(0.95), Inches(1.5), Inches(3.5), Inches(0.56)) tag.adjustments[0] = 0.5 tag.fill.solid() tag.fill.fore_color.rgb = YELLOW tag.line.fill.background() tag.shadow.inherit = False tag.text_frame.margin_left = tag.text_frame.margin_right = 0 _write(tag.text_frame, [[("小学生安全小课堂", 18, True, DEEP)]], 18, True, DEEP, PP_ALIGN.CENTER, MSO_ANCHOR.MIDDLE, 1.0, 0) text_box(s, 0.95, 2.42, 11.6, 0.9, "小心身边的“隐形杀手”", 50, True, WHITE) text_box(s, 0.95, 3.42, 11.6, 0.9, "预防一氧化碳中毒", 44, True, YELLOW) ln = s.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(0.95), Inches(4.6), Inches(2.6), Pt(4)) ln.fill.solid() ln.fill.fore_color.rgb = YELLOW ln.line.fill.background() ln.shadow.inherit = False text_box(s, 0.95, 4.95, 11.6, 0.5, "看得见的提醒 · 学得会的安全", 20, False, RGBColor(0xA9, 0xC6, 0xE2)) # ---------------- 第 2 页:认识它 ---------------- def p02_what(): s = new_slide() header(s, "认识它:看不见的“坏气体”", 2) cards = [ ("看不见", "它是气体\n眼睛看不见"), ("闻不到", "它没有气味\n鼻子闻不出"), ("会伤人", "进到身体里\n让人缺氧难受"), ] w, h, gap = 3.5, 2.5, 0.4 x0 = (SW - (w * 3 + gap * 2)) / 2 for i, (big, small) in enumerate(cards): x = x0 + i * (w + gap) p = panel(s, x, 1.85, w, h, SKY2, radius=0.14) fill_panel(p, [ [(big, 32, True, BLUE)], [], [(small, 19, False, GRAY)], ], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.4, space_after=6) p = panel(s, 0.9, 4.75, 11.5, 1.75, REDBG, line=RED, radius=0.1) fill_panel(p, [ [("它藏在燃烧产生的烟里:", 22, True, RED)], [("煤炉、柴火、燃气热水器、炭火、汽车尾气……", 20, False, GRAY)], ], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.4, space_after=6) footer(s) # ---------------- 第 3 页:它从哪里来 ---------------- def p03_source(): s = new_slide() header(s, "它从哪里来?危险小清单", 3) rows = [ ("煤炉、柴火", "冬天在屋里取暖"), ("燃气热水器", "洗澡、装在卫生间里"), ("炭火、烧烤", "在房间、帐篷里烤火烧烤"), ("汽车暖风", "车停着不熄火,一直开暖风"), ] x, y = 0.9, 1.75 tbl = s.shapes.add_table(len(rows) + 1, 2, Inches(x), Inches(y), Inches(11.5), Inches(0.82 * (len(rows) + 1))).table tbl.columns[0].width = Inches(4.2) tbl.columns[1].width = Inches(7.3) for i in range(len(rows) + 1): tbl.rows[i].height = Inches(0.82) def set_cell(cell, text, size, bold, color, fill): cell.fill.solid() cell.fill.fore_color.rgb = fill cell.margin_left = Inches(0.28) cell.vertical_anchor = MSO_ANCHOR.MIDDLE p = cell.text_frame.paragraphs[0] p.alignment = PP_ALIGN.LEFT for r in list(p.runs): r._r.getparent().remove(r._r) set_run(p.add_run(), text, size, bold, color) set_cell(tbl.cell(0, 0), "来源", 22, True, WHITE, BLUE) set_cell(tbl.cell(0, 1), "常见场景", 22, True, WHITE, BLUE) for i, (a, b) in enumerate(rows, start=1): bg = WHITE if i % 2 else SKY set_cell(tbl.cell(i, 0), a, 21, True, DEEP, bg) set_cell(tbl.cell(i, 1), b, 20, False, GRAY, bg) text_box(s, 0.9, 6.15, 11.5, 0.55, "有火、有烟、又不通风的地方,就要特别小心!", 20, True, RED, align=PP_ALIGN.CENTER) footer(s) # ---------------- 第 4 页:身体警报 ---------------- def p04_alert(): s = new_slide() header(s, "身体发出的“警报”,要听!", 4) items = [("头晕、头痛", "眼前发花、脑袋沉"), ("想吐、难受", "肚子不舒服、恶心"), ("浑身没力气", "腿发软、特别犯困"), ("心慌、站不稳", "心跳快、看东西模糊")] w, h, gap = 2.9, 2.35, 0.28 x0 = (SW - (w * 4 + gap * 3)) / 2 for i, (big, small) in enumerate(items): x = x0 + i * (w + gap) p = panel(s, x, 1.8, w, h, SKY, line=BLUE, radius=0.14) fill_panel(p, [ [(big, 23, True, BLUE)], [], [(small, 17, False, GRAY)], ], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.35, space_after=6) p = panel(s, 0.9, 4.55, 11.5, 1.5, RED, radius=0.1) fill_panel(p, [ [("这不是“困了”,是中毒了!", 30, True, WHITE)], [("马上离开房间,到屋外空气新鲜的地方去!", 21, False, WHITE)], ], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.35, space_after=6) footer(s) # ---------------- 通用:注意事项页 ---------------- def advice_page(title, page, dos, donts, tip): s = new_slide() header(s, title, page) w, h = 5.6, 3.6 left = panel(s, 0.85, 1.7, w, h, GREENBG, line=GREEN, radius=0.08) fill_panel(left, [[("√ 要做", 26, True, GREEN)], []] + [[("· " + d, 19, False, GRAY)] for d in dos], anchor=MSO_ANCHOR.TOP, spacing=1.3, space_after=10) right = panel(s, 0.85 + w + 0.55, 1.7, w, h, REDBG, line=RED, radius=0.08) fill_panel(right, [[("× 不要做", 26, True, RED)], []] + [[("· " + d, 19, False, GRAY)] for d in donts], anchor=MSO_ANCHOR.TOP, spacing=1.3, space_after=10) t = panel(s, 0.85, 5.65, 11.6, 1.05, RGBColor(0xFF, 0xF6, 0xDC), line=YELLOW, radius=0.2) fill_panel(t, [[("小提醒:" + tip, 21, True, RGBColor(0x8A, 0x5A, 0x00))]], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.2, space_after=0) footer(s) def p05_stove(): advice_page( "注意事项① 煤炉、柴火取暖", 5, ["门窗留一条缝,保持通风", "炉子装好烟囱,接口要严实", "睡觉前把炉火熄灭,炉子别放卧室"], ["门窗全部关紧", "把炉子搬进卧室睡觉", "用没烧透的煤压着火过夜"], "留一条小缝,就是给自己留一条安全通道。" ) def p06_heater(): advice_page( "注意事项② 燃气热水器", 6, ["装在通风好的地方(阳台、外墙等)", "洗澡时开窗、开排气扇", "洗澡时间别太长,家里要有人"], ["把热水器装在卫生间里", "洗澡时门窗全部关死", "洗完不检查,直接睡觉"], "回家看看:你家的燃气热水器,装在哪里?" ) def p07_charcoal(): advice_page( "注意事项③ 别在屋里、帐篷里烧炭", 7, ["烧烤、烤火到室外通风的地方", "用完的炭火用水浇灭", "室内取暖用合格取暖设备"], ["在房间里烧炭烤火", "在帐篷里烧烤、点炭盆", "在密闭小屋里烧炭取暖"], "帐篷又小又闷,毒气散不出去,最危险!" ) def p08_car(): advice_page( "注意事项④ 车里开暖风要当心", 8, ["车停在空旷通风的地方再休息", "车里人多时,开一点车窗", "长途行车,定时开窗换气"], ["车停着不熄火,长时间开暖风待着", "在地下车库、车库里开暖风睡觉", "车窗全关、发动机一直转着休息"], "车窗关紧 + 发动机一直转 = 很危险。" ) # ---------------- 第 9 页:感觉不对马上做 ---------------- def p09_action(): s = new_slide() header(s, "感觉不对,马上做这三件事!", 9) steps = [("1", "马上走到屋外", "不要犹豫,先离开"), ("2", "呼吸新鲜空气", "到通风、开阔的地方"), ("3", "马上告诉大人", "爸爸妈妈、老师都行")] w, h, gap = 3.2, 2.9, 0.6 x0 = (SW - (w * 3 + gap * 2)) / 2 for i, (num, title, desc) in enumerate(steps): x = x0 + i * (w + gap) p = panel(s, x, 1.9, w, h, SKY, line=BLUE, radius=0.12) badge(s, x + w / 2 - 0.42, 2.15, 0.84, num, BLUE, WHITE, 30) text_box(s, x + 0.2, 3.25, w - 0.4, 0.55, title, 24, True, DEEP, align=PP_ALIGN.CENTER) text_box(s, x + 0.2, 3.85, w - 0.4, 0.7, desc, 17, False, GRAY, align=PP_ALIGN.CENTER) if i < 2: ar = s.shapes.add_shape(MSO_SHAPE.RIGHT_ARROW, Inches(x + w + 0.1), Inches(3.15), Inches(0.4), Inches(0.4)) ar.fill.solid() ar.fill.fore_color.rgb = YELLOW ar.line.fill.background() ar.shadow.inherit = False t = panel(s, 0.9, 5.3, 11.5, 1.15, REDBG, line=RED, radius=0.2) fill_panel(t, [[("千万不要硬撑,也不要觉得“睡一觉就好了”!", 23, True, RED)]], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.2, space_after=0) footer(s) # ---------------- 第 10 页:看到有人中毒 ---------------- def p10_rescue(): s = new_slide() header(s, "看到有人中毒,救人四步走", 10) steps = [("1", "打开门窗", "让新鲜空气进来"), ("2", "搬人出去", "移到空气新鲜处"), ("3", "赶紧喊大人", "不要一个人冲进去"), ("4", "拨打 120 / 119", "说清地点和情况")] w, h, gap = 2.8, 3.4, 0.3 x0 = (SW - (w * 4 + gap * 3)) / 2 for i, (num, title, desc) in enumerate(steps): x = x0 + i * (w + gap) panel(s, x, 1.75, w, h, SKY, line=BLUE, radius=0.12) badge(s, x + w / 2 - 0.4, 2.0, 0.8, num, BLUE, WHITE, 28) text_box(s, x + 0.15, 2.95, w - 0.3, 0.6, title, 21, True, DEEP, align=PP_ALIGN.CENTER) text_box(s, x + 0.2, 3.6, w - 0.4, 1.2, desc, 17, False, GRAY, align=PP_ALIGN.CENTER, spacing=1.3) t = panel(s, 0.9, 5.5, 11.5, 1.1, RGBColor(0xFF, 0xF6, 0xDC), line=YELLOW, radius=0.2) fill_panel(t, [[("小朋友是未成年人:先喊大人,再打电话,不要自己冒险!", 21, True, RGBColor(0x8A, 0x5A, 0x00))]], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.2, space_after=0) footer(s) # ---------------- 第 11 页:三件不能做 ---------------- def p11_never(): s = new_slide() header(s, "救命时刻,三件事千万别做", 11) items = [("不能开灯", "别碰电灯开关"), ("不能按开关", "门铃、插座都别动"), ("不能点火", "打火机、火柴都不行")] w, h, gap = 3.7, 2.7, 0.4 x0 = (SW - (w * 3 + gap * 2)) / 2 for i, (big, small) in enumerate(items): x = x0 + i * (w + gap) p = panel(s, x, 1.85, w, h, RED, radius=0.12) fill_panel(p, [ [("✕", 52, True, WHITE)], [(big, 26, True, WHITE)], [(small, 18, False, RGBColor(0xFF, 0xE6, 0xE5))], ], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.15, space_after=6) t = panel(s, 0.9, 4.95, 11.5, 1.4, RGBColor(0xFF, 0xF6, 0xDC), line=YELLOW, radius=0.15) fill_panel(t, [ [("为什么?", 24, True, RGBColor(0x8A, 0x5A, 0x00))], [("一点点火花,就可能引起爆炸!", 22, False, GRAY)], ], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.3, space_after=4) footer(s) # ---------------- 第 12 页:求助电话 ---------------- def p12_phone(): s = new_slide() header(s, "求助电话要记牢", 12) cards = [("120", "急救电话", "有人晕倒、叫不醒时打", BLUE), ("119", "消防电话", "有爆炸危险、需要救援时打", RED)] w, h, gap = 5.4, 2.9, 0.7 x0 = (SW - (w * 2 + gap)) / 2 for i, (num, name, desc, col) in enumerate(cards): x = x0 + i * (w + gap) p = panel(s, x, 1.75, w, h, col, radius=0.1) fill_panel(p, [ [(num, 60, True, WHITE)], [(name, 24, True, WHITE)], [(desc, 18, False, RGBColor(0xEC, 0xF3, 0xFA))], ], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.15, space_after=6) t = panel(s, 0.9, 5.05, 11.5, 1.35, SKY, line=BLUE, radius=0.15) fill_panel(t, [ [("打电话要说清楚:", 22, True, BLUE)], [("在哪里 · 发生了什么 · 有几个人", 22, False, GRAY)], ], align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.3, space_after=4) footer(s) # ---------------- 第 13 页:报警器 ---------------- def p13_alarm(): s = new_slide() header(s, "家里的“安全小哨兵”", 13) items = [ ("是什么", "一氧化碳报警器:浓度一高就会“滴滴”报警"), ("装在哪", "卧室附近、取暖房间的墙上或天花板上"), ("怎么用", "定期按一下测试键,看它还会不会响"), ("响了咋办", "开窗 → 离开 → 告诉大人,别犹豫"), ] y = 1.75 for i, (k, v) in enumerate(items): p = panel(s, 0.9, y, 11.5, 0.98, SKY if i % 2 == 0 else WHITE, line=SKY2 if i % 2 else BLUE, radius=0.2) fill_panel(p, [[(k + ":", 21, True, BLUE), (v, 20, False, GRAY)]], anchor=MSO_ANCHOR.MIDDLE, spacing=1.15, space_after=0) y += 1.15 text_box(s, 0.9, 6.3, 11.5, 0.5, "建议爸爸妈妈装一个:小小的设备,守护全家一整个冬天。", 20, True, RED, align=PP_ALIGN.CENTER) footer(s) # ---------------- 第 14 页:安全口诀 ---------------- def p14_rhyme(): s = new_slide() header(s, "安全口诀,一起大声读!", 14) p = panel(s, 0.9, 1.6, 11.5, 4.5, SKY2, line=BLUE, radius=0.06) lines = [ [("煤炉取暖门留缝,", 28, True, DEEP)], [("热水器不进卫生间;", 28, True, DEEP)], [("屋里帐篷不烧炭,", 28, True, DEEP)], [("车里暖风别久待;", 28, True, DEEP)], [("头晕想吐快出门,", 28, True, RED)], [("120、119 记心间!", 28, True, RED)], ] fill_panel(p, lines, align=PP_ALIGN.CENTER, anchor=MSO_ANCHOR.MIDDLE, spacing=1.45, space_after=2) text_box(s, 0.9, 6.35, 11.5, 0.55, "安全记心里,冬天更温暖!", 24, True, BLUE, align=PP_ALIGN.CENTER) footer(s) # ---------------- 第 15 页:互动小测 ---------------- def p15_quiz(): s = new_slide() header(s, "课堂小测:你记住了吗?", 15) quiz = [ ("1. 冬天用煤炉取暖,门窗应该怎么开?", "A. 全部关紧 B. 留一条缝通风", "B"), ("2. 燃气热水器可以装在卫生间里吗?", "A. 可以 B. 不可以", "B"), ("3. 发现有人中毒,第一件事做什么?", "A. 先开灯看看 B. 打开门窗、把人搬到外面", "B"), ] y = 1.7 for q, opts, ans in quiz: p = panel(s, 0.9, y, 11.5, 1.3, SKY, line=BLUE, radius=0.15) fill_panel(p, [ [(q, 21, True, DEEP)], [(opts, 19, False, GRAY)], ], anchor=MSO_ANCHOR.MIDDLE, spacing=1.3, space_after=4) y += 1.5 text_box(s, 0.9, 6.35, 11.5, 0.5, "答案:1 - B 2 - B 3 - B", 22, True, RED, align=PP_ALIGN.CENTER) footer(s) # ---------------- 生成 ---------------- def main(): p01_cover() p02_what() p03_source() p04_alert() p05_stove() p06_heater() p07_charcoal() p08_car() p09_action() p10_rescue() p11_never() p12_phone() p13_alarm() p14_rhyme() p15_quiz() out = "预防一氧化碳中毒_小学生安全课件.pptx" PRS.save(out) print("已生成:", out) if __name__ == "__main__": main()