![6020de6e4bccd15c0ddc1e931a1f34d5.png](https://img-blog.csdnimg.cn/img_convert/6020de6e4bccd15c0ddc1e931a1f34d5.png)
![7d29b88065774f2a0f4962e53c355440.gif](https://img-blog.csdnimg.cn/img_convert/7d29b88065774f2a0f4962e53c355440.gif)
“ ❤希望通过Python编程,替代你去做耗时耗力耗心的事情。让你在烦忙的学习生活中,给自己一些和自己相处的时间,也给自己一些和家人在一起的时间❤”
![6020de6e4bccd15c0ddc1e931a1f34d5.png](https://img-blog.csdnimg.cn/img_convert/6020de6e4bccd15c0ddc1e931a1f34d5.png)
![7d29b88065774f2a0f4962e53c355440.gif](https://img-blog.csdnimg.cn/img_convert/7d29b88065774f2a0f4962e53c355440.gif)
![3001f6dbb570a9a1c6707627f08d8a5c.gif](https://img-blog.csdnimg.cn/img_convert/3001f6dbb570a9a1c6707627f08d8a5c.gif)
今天,小编要跟大家分享的,是关于使用python-docx,完成一个新员工介绍.pptx的ppt文件。主要呢,是安装一个第三方模块库,python-pptx,然后通过该模块库提供的功能指令,编码创建一个关于新员工基本情况介绍的ppt,打开首页是:欢迎新员工。然后下一页是三位新员工个人照,点击进行每个人个人信息介绍页,介绍页中包含个人信息,以及日常生活照片等。
![3001f6dbb570a9a1c6707627f08d8a5c.gif](https://img-blog.csdnimg.cn/img_convert/3001f6dbb570a9a1c6707627f08d8a5c.gif)
![6020de6e4bccd15c0ddc1e931a1f34d5.png](https://img-blog.csdnimg.cn/img_convert/6020de6e4bccd15c0ddc1e931a1f34d5.png)
![7d29b88065774f2a0f4962e53c355440.gif](https://img-blog.csdnimg.cn/img_convert/7d29b88065774f2a0f4962e53c355440.gif)
![3001f6dbb570a9a1c6707627f08d8a5c.gif](https://img-blog.csdnimg.cn/img_convert/3001f6dbb570a9a1c6707627f08d8a5c.gif)
实现该案例,基本步骤如下:1. 导入操作ppt的模块库。
2. 创建一个.pptx文件。
3. 在ppt文件中添加五页,并为每页指定空白样式。
4. 向第一页添加文本信息,设置文本样式。
5. 向第二页添加文本以及图片,设置照片格式,文本格式等。
6. 向第三页以及第五页个人介绍页添加图片,文本,设置对齐方式。
7. 保存ppt文件,查看结果。
![3001f6dbb570a9a1c6707627f08d8a5c.gif](https://img-blog.csdnimg.cn/img_convert/3001f6dbb570a9a1c6707627f08d8a5c.gif)
'''新员工介绍要求:通过pptx模块创建一个ppt,关于新员工基本情况介绍的ppt,打开首页是:欢迎新员工然后下一页是三位新员工个人照,点击进行每个人个人信息介绍页,介绍页中包含个人信息,以及生活照片等'''import pptximport pptx.utilfrom pptx.dml.color import RGBColorfrom PIL import Imagefrom pptx.enum.text import MSO_VERTICAL_ANCHOR, PP_ALIGNBG = 'bg.jpg'ppt_path = "D:\\POA2006\\unit19\\员工.pptx"#幻灯片ppt = pptx.Presentation(ppt_path)TITLE_LAYOUT = ppt.slide_layouts[0]ONLY_TITLE_LAYOUT = ppt.slide_layouts[5]EMPTY_LAYOUT = ppt.slide_layouts[6]TITLE_PICTURE_LAYOUT = ppt.slide_layouts[8]PPT_WIDTH = ppt.slide_widthPPT_HEIGHT = ppt.slide_height#创建第一张slides1 = ppt.slides.add_slide(TITLE_LAYOUT)#设置背景色为黑色s1.background.fill.solid()s1.background.fill.fore_color.rgb = RGBColor(0, 0, 0)#找到模板中预置的Title PlaceHoldertitle_ph = s1.shapes[0]text_frame = title_ph.text_framep = text_frame.add_paragraph()# p.alignment = PP_ALIGN.CENTERp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.font.size = pptx.util.Pt(60)p.text = '欢 迎 新 员 工'p.font.name = '楷体'p.font.color.rgb = RGBColor(255, 255, 255)#第二张slides2 = ppt.slides.add_slide(EMPTY_LAYOUT)#设置背景色s2.background.fill.solid()s2.background.fill.fore_color.rgb = RGBColor(0, 0, 0)#设置标题colspan = PPT_WIDTH // 10rowspan = PPT_WIDTH // 8tb = s2.shapes.add_textbox(colspan, 0, 8 * colspan, rowspan)text_frame = tb.text_framep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '新 员 工'p.font.name = '楷体'p.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(255, 255, 255)for i in range(3): # 利用pillow获得图片的宽度和高度 img = Image.open(f'unit19\emp00{i+1}.png') # 计算宽高比,让图片按比例缩放 ratio = img.height / img.width s2.shapes.add_picture( f'unit19\emp00{i+1}.png', (i * 2 + i + 1) * colspan, rowspan, width=colspan * 2, height=colspan * 2 * ratio)#第三张slides3 = ppt.slides.add_slide(TITLE_PICTURE_LAYOUT)#设置背景色s3.background.fill.solid()s3.background.fill.fore_color.rgb = RGBColor(0, 0, 0)title_ph = s3.placeholders[0]text_frame = title_ph.text_framep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '王 大 锤'p.font.name = '楷体'p.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(221, 79, 66)pic_ph = s3.placeholders[1]pic_ph.insert_picture('unit19\\emp001.png')text_ph = s3.placeholders[2]text_frame = text_ph.text_framep = text_frame.add_paragraph()p.alignment = PP_ALIGN.CENTERp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐!'p.font.name = '楷体'p.font.size = pptx.util.Pt(28)p.font.color.rgb = RGBColor(255, 255, 255)#第四张slides4 = ppt.slides.add_slide(EMPTY_LAYOUT)#设置背景色s4.background.fill.solid()s4.background.fill.fore_color.rgb = RGBColor(0, 0, 0)img = Image.open('unit19\\emp002.png')ratio = img.height / img.widthcolspan = PPT_WIDTH // 10rowspan = PPT_HEIGHT // 8s4.shapes.add_picture( 'unit19\\emp002.png', left=0, top=0, height=PPT_HEIGHT, width=PPT_HEIGHT / ratio)leftmargin = PPT_HEIGHT / ratio + colspantb = s4.shapes.add_textbox( left=leftmargin, top=rowspan, width=PPT_WIDTH - leftmargin - colspan, height=rowspan)text_frame = tb.text_framep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '王 小 花'p.font.name = '楷体'p.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(221, 79, 66)tb = s4.shapes.add_textbox( left=leftmargin, top=2 * rowspan, width=PPT_WIDTH - leftmargin - colspan, height=5 * rowspan)text_frame = tb.text_frametext_frame.word_wrap = Truep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.TOPp.text = '喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐!'p.font.name = '楷体'p.font.size = pptx.util.Pt(28)p.font.color.rgb = RGBColor(255, 255, 255)#第五张slides5 = ppt.slides.add_slide(ONLY_TITLE_LAYOUT)#设置背景色s5.background.fill.solid()s5.background.fill.fore_color.rgb = RGBColor(0, 0, 0)img = Image.open('unit19\\emp003.png')ratio = img.height / img.widthcolspan = PPT_WIDTH // 10rowspan = PPT_HEIGHT // 8title = s5.placeholders[0]p = title.text_frame.add_paragraph()p.alignment = PP_ALIGN.CENTER# p.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '王 钢 蛋'p.font.name = '楷体'p.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(221, 79, 66)topmargin = title.height + 0.5 * rowspans5.shapes.add_picture( 'unit19\\emp003.png', left=PPT_WIDTH - (PPT_HEIGHT - topmargin) / ratio - colspan // 4, top=topmargin, height=PPT_HEIGHT - topmargin, width=(PPT_HEIGHT - topmargin) / ratio)leftmargin = colspantb = s5.shapes.add_textbox( left=leftmargin, top=topmargin, width=PPT_WIDTH - (PPT_HEIGHT - topmargin) / ratio - 2 * colspan, height=PPT_HEIGHT - topmargin - rowspan)text_frame = tb.text_frametext_frame.word_wrap = Truep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.TOPp.text = '喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐!'p.font.name = '楷体'p.font.size = pptx.util.Pt(28)p.font.color.rgb = RGBColor(255, 255, 255)ppt.save(ppt_path)print("生成成功,快去看看吧....")'''新员工介绍要求:通过pptx模块创建一个ppt,关于新员工基本情况介绍的ppt,打开首页是:欢迎新员工然后下一页是三位新员工个人照,点击进行每个人个人信息介绍页,介绍页中包含个人信息,以及生活照片等'''import pptximport pptx.utilfrom pptx.dml.color import RGBColorfrom PIL import Imagefrom pptx.enum.text import MSO_VERTICAL_ANCHOR, PP_ALIGNBG = 'bg.jpg'ppt_path = "D:\\POA2006\\unit19\\员工.pptx"# 16:9的尺寸是# 960pt * 540pt pt是一种与像素无关的长度单位# 1英寸 = 2.54厘米 = 72pt# 在PPT底层使用一种emu度量单位,1pt = 12700emu# 所以,16:9的PPT尺寸也表示为12192000emu * 6858000emu#幻灯片ppt = pptx.Presentation(ppt_path)TITLE_LAYOUT = ppt.slide_layouts[0]ONLY_TITLE_LAYOUT = ppt.slide_layouts[5]EMPTY_LAYOUT = ppt.slide_layouts[6]TITLE_PICTURE_LAYOUT = ppt.slide_layouts[8]PPT_WIDTH = ppt.slide_widthPPT_HEIGHT = ppt.slide_height#创建第一张slides1 = ppt.slides.add_slide(TITLE_LAYOUT)#设置背景色为黑色s1.background.fill.solid()s1.background.fill.fore_color.rgb = RGBColor(0, 0, 0)#找到模板中预置的Title PlaceHoldertitle_ph = s1.shapes[0]text_frame = title_ph.text_framep = text_frame.add_paragraph()# p.alignment = PP_ALIGN.CENTERp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.font.size = pptx.util.Pt(60)p.text = '欢 迎 新 员 工'p.font.name = '楷体'p.font.color.rgb = RGBColor(255, 255, 255)#第二张slides2 = ppt.slides.add_slide(EMPTY_LAYOUT)#设置背景色s2.background.fill.solid()s2.background.fill.fore_color.rgb = RGBColor(0, 0, 0)#设置标题colspan = PPT_WIDTH // 10rowspan = PPT_WIDTH // 8tb = s2.shapes.add_textbox(colspan, 0, 8 * colspan, rowspan)text_frame = tb.text_framep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '新 员 工'p.font.name = '楷体'p.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(255, 255, 255)for i in range(3): # 利用pillow获得图片的宽度和高度 img = Image.open(f'unit19\emp00{i+1}.png') # 计算宽高比,让图片按比例缩放 ratio = img.height / img.width s2.shapes.add_picture( f'unit19\emp00{i+1}.png', (i * 2 + i + 1) * colspan, rowspan, width=colspan * 2, height=colspan * 2 * ratio)#第三张slides3 = ppt.slides.add_slide(TITLE_PICTURE_LAYOUT)#设置背景色s3.background.fill.solid()s3.background.fill.fore_color.rgb = RGBColor(0, 0, 0)title_ph = s3.placeholders[0]text_frame = title_ph.text_framep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '王 大 锤'p.font.name = '楷体'p.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(221, 79, 66)pic_ph = s3.placeholders[1]pic_ph.insert_picture('unit19\\emp001.png')text_ph = s3.placeholders[2]text_frame = text_ph.text_framep = text_frame.add_paragraph()p.alignment = PP_ALIGN.CENTERp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐!'p.font.name = '楷体'p.font.size = pptx.util.Pt(28)p.font.color.rgb = RGBColor(255, 255, 255)#第四张slides4 = ppt.slides.add_slide(EMPTY_LAYOUT)#设置背景色s4.background.fill.solid()s4.background.fill.fore_color.rgb = RGBColor(0, 0, 0)img = Image.open('unit19\\emp002.png')ratio = img.height / img.widthcolspan = PPT_WIDTH // 10rowspan = PPT_HEIGHT // 8s4.shapes.add_picture( 'unit19\\emp002.png', left=0, top=0, height=PPT_HEIGHT, width=PPT_HEIGHT / ratio)leftmargin = PPT_HEIGHT / ratio + colspantb = s4.shapes.add_textbox( left=leftmargin, top=rowspan, width=PPT_WIDTH - leftmargin - colspan, height=rowspan)text_frame = tb.text_framep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '王 小 花'p.font.name = '楷体'p.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(221, 79, 66)tb = s4.shapes.add_textbox( left=leftmargin, top=2 * rowspan, width=PPT_WIDTH - leftmargin - colspan, height=5 * rowspan)text_frame = tb.text_frametext_frame.word_wrap = Truep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.TOPp.text = '喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐!'p.font.name = '楷体'p.font.size = pptx.util.Pt(28)p.font.color.rgb = RGBColor(255, 255, 255)#第五张slides5 = ppt.slides.add_slide(ONLY_TITLE_LAYOUT)#设置背景色s5.background.fill.solid()s5.background.fill.fore_color.rgb = RGBColor(0, 0, 0)img = Image.open('unit19\\emp003.png')ratio = img.height / img.widthcolspan = PPT_WIDTH // 10rowspan = PPT_HEIGHT // 8title = s5.placeholders[0]p = title.text_frame.add_paragraph()p.alignment = PP_ALIGN.CENTER# p.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLEp.text = '王 钢 蛋'p.font.name = '楷体'p.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(221, 79, 66)topmargin = title.height + 0.5 * rowspans5.shapes.add_picture( 'unit19\\emp003.png', left=PPT_WIDTH - (PPT_HEIGHT - topmargin) / ratio - colspan // 4, top=topmargin, height=PPT_HEIGHT - topmargin, width=(PPT_HEIGHT - topmargin) / ratio)leftmargin = colspantb = s5.shapes.add_textbox( left=leftmargin, top=topmargin, width=PPT_WIDTH - (PPT_HEIGHT - topmargin) / ratio - 2 * colspan, height=PPT_HEIGHT - topmargin - rowspan)text_frame = tb.text_frametext_frame.word_wrap = Truep = text_frame.add_paragraph()p.alignment = PP_ALIGN.LEFTp.vertical_anchor = MSO_VERTICAL_ANCHOR.TOPp.text = '喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐,喜欢摇滚乐!'p.font.name = '楷体'p.font.size = pptx.util.Pt(28)p.font.color.rgb = RGBColor(255, 255, 255)ppt.save(ppt_path)print("生成成功,快去看看吧....")
![96e33cade29deb6372b7c5d85164b222.png](https://img-blog.csdnimg.cn/img_convert/96e33cade29deb6372b7c5d85164b222.png)
![7d29b88065774f2a0f4962e53c355440.gif](https://img-blog.csdnimg.cn/img_convert/7d29b88065774f2a0f4962e53c355440.gif)
![3001f6dbb570a9a1c6707627f08d8a5c.gif](https://img-blog.csdnimg.cn/img_convert/3001f6dbb570a9a1c6707627f08d8a5c.gif)
本案例中,涉及最多的就是图片和字体的格式设置,比如:1. 关于对齐方式问题:
leftmargin = PPT_HEIGHT / ratio + colspantb = s4.shapes.add_textbox( left=leftmargin, top=rowspan, width=PPT_WIDTH - leftmargin - colspan, height=rowspan)leftmargin = PPT_HEIGHT / ratio + colspantb = s4.shapes.add_textbox( left=leftmargin, top=rowspan, width=PPT_WIDTH - leftmargin - colspan, height=rowspan)
2. 批量处理每个人生活照时问题:
for i in range(3): # 利用pillow获得图片的宽度和高度 img = Image.open(f'unit19\emp00{i+1}.png') # 计算宽高比,让图片按比例缩放 ratio = img.height / img.width s2.shapes.add_picture( f'unit19\emp00{i+1}.png', (i * 2 + i + 1) * colspan, rowspan, width=colspan * 2, height=colspan * 2 * ratio)for i in range(3): # 利用pillow获得图片的宽度和高度 img = Image.open(f'unit19\emp00{i+1}.png') # 计算宽高比,让图片按比例缩放 ratio = img.height / img.width s2.shapes.add_picture( f'unit19\emp00{i+1}.png', (i * 2 + i + 1) * colspan, rowspan, width=colspan * 2, height=colspan * 2 * ratio)
3. 关于字体的颜色值问题:
s3.background.fill.solid()s3.background.fill.fore_color.rgb = RGBColor(0, 0, 0)title_ph = s3.placeholders[0]text_frame = title_ph.text_frames3.background.fill.solid()s3.background.fill.fore_color.rgb = RGBColor(0, 0, 0)title_ph = s3.placeholders[0]text_frame = title_ph.text_framep.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(221, 79, 66)pic_ph = s3.placeholders[1]pic_ph.insert_picture('unit19\\emp001.png')text_ph = s3.placeholders[2]text_frame = text_ph.text_framep.font.size = pptx.util.Pt(48)p.font.color.rgb = RGBColor(221, 79, 66)pic_ph = s3.placeholders[1]pic_ph.insert_picture('unit19\\emp001.png')text_ph = s3.placeholders[2]text_frame = text_ph.text_fram
![6020de6e4bccd15c0ddc1e931a1f34d5.png](https://img-blog.csdnimg.cn/img_convert/6020de6e4bccd15c0ddc1e931a1f34d5.png)
![7d29b88065774f2a0f4962e53c355440.gif](https://img-blog.csdnimg.cn/img_convert/7d29b88065774f2a0f4962e53c355440.gif)
人与计算机最大的区别是:人有思想、懂感情,是一个不断学习进化的品种,并且大部分人都希望通过自己的努力,让自己和爱的人生活的更好。在这样一个被炒的铺天盖地的智能化时代,看到时代左右的人,才不会被时代所左右。
![747b0a051f92fc4bfc7ff4b25413c379.png](https://img-blog.csdnimg.cn/img_convert/747b0a051f92fc4bfc7ff4b25413c379.png)
来自:留给沿海地带,微信号:lml108
★★★ 达内 Python办公自动化,给你的生活“加点料”!★★★点击即刻免费试听课程,无需注册登陆任何帐号。
网址:http://www.tmooc.cn/course/302822.shtml