from pptx import Presentationfrom pptx.util import Inches,Cm,Ptfrom pptx.dml.color import RGBColorppt = Presentation("E:/test.pptx")slides = ppt.slideslayouts = ppt.slide_layoutsslide = slides.add_slide(layouts[6])# 创建文本框# add_textbox 需要插入位置和大小共四个参数# add_textbox(left: Length, top: Length, width: Length, height: Length)text_box = slide.shapes.add_textbox(Cm(3),Cm(3),Cm(20),Cm(10))# 创建文本容器text_frame = text_box.text_frame# 文本框自动换行text_frame.word_wrap = True# 清空空白段落text_frame.clear()# 添加第一段文字 新增一个段落p1 = text_frame.add_paragraph()# 添加文字run1 = p1.add_run()run1.text = "qwpedfdfds"# 设置段落对齐方式from pptx.enum.text import PP_ALIGNp1.alignment = PP_ALIGN.CENTER # LEFT CENTER RIGHT JUSTIFY# 设置字体属性font1 = run1.fontfont1.name = "Segoe UI Black"font1.size = Pt(24)font1.bold = Truefont1.italic = Truefont1.color.rgb = RGBColor(255,0,0)# 文本框背景填充(纯色填充)fill = text_box.fillfill.solid()fill.fore_color.rgb = RGBColor(230,230,230)# 文本框边框线条line = text_box.lineline.color.rgb = RGBColor(255,0,0)line.width = Pt(1.5)#ppt.save("E:/test.pptx")
from pptx import Presentationfrom pptx.util import Inches,Cm,Ptfrom pptx.dml.color import RGBColorppt = Presentation("E:/test.pptx")slides = ppt.slideslayouts = ppt.slide_layoutsslide = slides.add_slide(layouts[6])# 定义图片image1_path = "E:/picture1.png"# add_picture 需要传入 图片及插入位置参数 图片大小参数可忽略slide.shapes.add_picture(image1_path,Cm(3),Cm(3))#ppt.save("E:/test.pptx")
from pptx import Presentationfrom pptx.util import Inches,Cm,Ptfrom pptx.dml.color import RGBColorppt = Presentation("E:/test.pptx")slides = ppt.slideslayouts = ppt.slide_layoutsslide = slides.add_slide(layouts[6])table_shape = slide.shapes.add_table(3,3,Cm(3),Cm(3),Cm(20),Cm(3))table = table_shape.table# 填充文本# 获取某个单元格cell1 = table.cell(0,0)# 单元格内填充文本cell1.text = "AA"# 单元格填充颜色cell1.fill.solid()cell1.fill.fore_color.rgb = RGBColor(255,0,0)# 单元格文本居中 字体属性cell2 = table.cell(0,1)cell2.text = "BB"cell2_tf = cell2.text_framefrom pptx.enum.text import PP_ALIGN,MSO_ANCHORcell2.vertical_anchor = MSO_ANCHOR.MIDDLEfor para in cell2_tf.paragraphs:para.alignment = PP_ALIGN.CENTERfor run in para.runs:run.font.name = "微软雅黑"run.font.size = Pt(12)cell2.fill.solid()cell2.fill.fore_color.rgb = RGBColor(255,255,0)# 单元格填充颜色cell3 = table.cell(0,2)cell3.text = "CC"cell3.fill.solid()cell3.fill.fore_color.rgb = RGBColor(0,255,0)# 合并单元格cell4 = table.cell(2,1)table.cell(1,0).merge(cell4)ppt.save("E:/test.pptx")
💡往期文章
HyperMesh Python 二次开发 提高脚本运行速度的方法
HyperMesh Python二次开发-自动修改Component名字
CAE仿真 网格评价中的雅可比为什么是0-1?及参数单元是什么