办公自动化--python-pptx
2021/8/17 11:06:31
本文主要是介绍办公自动化--python-pptx,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
安装
- pip install python-pptx
python操作pptx文件
from pptx import Presentation
创建ppt文件
prs = Presentation() # 创建ppt文件 prs.save('texst01_ppt.pptx') # 保存ppt文件
创建副件
prs1 = Presentation('texst01_ppt.pptx') prs1.save('texst01_ppt副件.pptx')
操作幻灯片
from pptx import Presentation prs = Presentation() slide_layout = prs.slide_layouts[1] # 选择版式 slide_01 = prs.slides.add_slide(slide_layout) # 应用版式 shapes = slide_01.shapes prs.save('test02_ppt.pptx')
形状学习和颜色
from pptx import Presentation from pptx.util import Inches,Pt from pptx.enum.shapes import MSO_SHAPE from pptx.dml.color import RGBColor from pptx.enum.dml import MSO_THEME_COLOR prs = Presentation() slide_layout = prs.slide_layouts[1] slide = prs.slides.add_slide(slide_layout) shapes = slide.shapes # shapes.add_shape?? autoshape_type_id,left,top,width,height left = top = width = height = Inches(1)
查看一英尺
length = Inches(1) print(length,length.cm,length.pt) length2 = Pt(72) print(length2)
添加圆角矩形并设置长宽和在ppt的位置
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height)
颜色填充
left = top = width = height = Inches(2) shape_02 = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height) fill = shape_02.fill # 设置填充色 fill.solid() # 实心填充 fill.fore_color.rgb = RGBColor(255,0,0)
设置形状颜色填充
left = top =width = height = Inches(4) shape_03 = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height) fill_03 = shape_03.fill fill_03.solid() fill_03.fore_color.theme_color = MSO_THEME_COLOR.ACCENT_1 # 设置主题颜色 fill_03.fore_color.brightness = -0.25 # 设置亮度
设置背景色
left = top =width = height = Inches(8) shape_04 = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height) shape_04.fill.background()
设置轮廓颜色
slide_layout_02 = prs.slide_layouts[0] slide_02 = prs.slides.add_slide(slide_layout_02) shapes_02 = slide_02.shapes
设置图片轮廓
left = top =width = height = Inches(4) shape_03 = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height)
设置边框
line = shape_02.line line.color.rgb = RGBColor(255,0,0) # 设置边框颜色 line.color.brithtness = 0.5 #设置边框的通透度 line.width = Pt(2.5) # 设置边框宽度 line.color.theme_color = MSO_THEME_COLOR.ACCENT_1 # 将边框设置为主题色 line.fill.background() # 设置边框为透明色
保存
prs.save('test03_ppt.pptx')
占位符
from pptx import Presentation prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[8]) # 输出slide中的每个占位符的索引和名字 for shape in slide.placeholders: print(shape.placeholder_format.idx,shape.name) # 知道索引后也可以这样访问名称 print(slide.palceholder[1]) for shape in slide.shapes: if shape.is_placeholder: phf = shape.placeholder_format print(phf.index,phf.type) slide_02 = prs.slides.add_slide(prs.slide_layouts[8]) place_holder = slide_02.placeholders[1] # print(place_holder.name) # print(place_holder.placeholder_format.type) picture = place_holder.insert_picture('壁纸.jpg') prs.save('test04_ppt.pptx')
文本框
from pptx import Presentation from pptx.enum.text import MSO_VERTICAL_ANCHOR,MSO_AUTO_SIZE from pptx.util import Inches,Pt from pptx.enum.dml import MSO_THEME_COLOR prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[1]) shapes = slide.shapes for shape in shapes: if not shape.has_text_frame: # 判断如果说形状没有文本框,跳出,进行下一次循环 continue text_frame = shape.text_frame print(text_frame)
访问段落
para_strs = [ 'Egg,bacon,sausage and spam', 'spam,bacon,sausage and spam', 'spam,egg,spam,spam,bacon and spam' ] for shape in shapes: if not shape.has_text_frame: continue text_frame = shape.text_frame # 因为文本框中时常有自带的段落,所以我们首先需清空文本框 text_frame.clear() # 清空文本框 para = text_frame.paragraphs[0] # 获取索引为0的段落 print(para) para.text = para_strs[0] # 将第一条数据放置段落1中 run = para.add_run() run.text = 'hello world' for para_str in para_strs[1:]: # 取出第一条下面的所有数据 p = text_frame.add_paragraph() p.text = para_str
文本框格式
slide_02 = prs.slides.add_slide(prs.slide_layouts[1]) shapes = slide_02.shapes shape = shapes[0] text_frame = shape.text_frame text_frame.text = 'hello world bababbababbababa' text_frame.margin_bottom = Inches(0.08) # 设置下边距 text_frame.vertical_anchor = MSO_VERTICAL_ANCHOR.TOP # 垂直方向顶锚点 text_frame.word_wrap = False # True 允许自动换行 False不允许自动换行 text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT # 文本框自适应字体大小 text_frame_02 = shapes[1].text_frame text_frame_02.clear() p = text_frame_02.paragraphs[0] run = p.add_run() run.text = 'how are you,I am study python'
设置颜色
font = run.font font.name = 'Calibri' # 设置字体样式 font.size = Pt(15) # 设置字体大小 font.blod = True # 设置字体石否加粗 True 加粗 False 不加粗 None 保持原有设置 font.italic = None # 字体是否倾斜 True 倾斜 False 不倾斜 None 保持原有设置 font.color.theme_color = MSO_THEME_COLOR.ACCENT_1 # 设置字体主题颜色 prs.save('text05_ppt.pptx')
操作图表
导入库
from pptx import Presentation from pptx.util import Inches,Pt from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE, XL_LABEL_POSITION
插入柱状图
prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[5]) char_data = CategoryChartData() char_data.categories = ['Apple','banana','cherry'] # x轴的分类 char_data.add_series('sales',(20,30,10)) left = Inches(2) top = Inches(2) width = Inches(6) height = Inches(5) slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, # 图表类型为簇状柱形图 x=left, y=top, # 图表区左上角在幻灯片中的坐标位置 cx=width, cy=height, # 图表区的长和宽 chart_data=char_data # 图表数据 ) prs.save('text06-chart01.pptx')
创建折线图
prs_02 = Presentation() slide_02 = prs_02.slides.add_slide(prs_02.slide_layouts[5]) chart_data02 = CategoryChartData() # 图表数据 chart_data02.categories = ['Q1 sales','Q2 sales','Q3 sales'] chart_data02.add_series('Apple',(30,25,40)) chart_data02.add_series('banana',(25,30,40)) chart_data02.add_series('cherry',(40,25,30)) chart = slide_02.shapes.add_chart( XL_CHART_TYPE.LINE,left,top,width,height,chart_data02 ) chart.has_legend = True prs_02.save('test06_chart02.pptx')
扇形图
prs_03 = Presentation() slide_03 = prs_03.slides.add_slide(prs_03.slide_layouts[5]) char_data03 = CategoryChartData() char_data03.categories = ['Q1', 'Q2', 'Q3'] char_data03.add_series('Q1 sales',[0.5,0.3,0.2]) chart_03 = slide_03.shapes.add_chart( XL_CHART_TYPE.PIE,left,top,width,height,char_data03 ) chart = chart_03.chart plot = chart.plots[0] # 设置数据标签 plot.has_data_labels = True # 显示数据标签 data_labels = plot.data_labels # 获取数据标签控制类 data_labels.show_category_name = True # 是否显示类别名称 data_labels.show_value = False # 是否显示值 data_labels.show_percentage = True # 是否显示百分比 data_labels.number_format = '0.0%' # 标签的数字格式 data_labels.position = XL_LABEL_POSITION.INSIDE_END # 标签位置 data_labels.font.name = 'Arial' data_labels.font.size = Pt(14) # 设置图表标题 chart.has_title = True # 显示标题 para = chart.chart_title.text_frame.add_paragraph() para.text = '销量占比' # 标题内容 para.font.size = Pt(16) # 字体大小 prs_03.save('test06_chart03.pptx')
添加表格
from pptx import Presentation from pptx.util import Inches prs_04 = Presentation() slide_04 = prs_04.slides.add_slide(prs_04.slide_layouts[5]) x,y,cx,cy = Inches(2),Inches(2),Inches(4),Inches(1.5) shape = slide_04.shapes.add_table(3,3,x,y,cx,cy) print(shape.has_table) # 判断是否有表格 table = shape.table cell = table.cell(0,0) cell.text = 'Apple' prs_04.save('test07_tb.pptx')
练习
导入库
from pptx import Presentation
练习01
prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[0]) title = slide.shapes.title title.text = 'Hello World' subtitle = slide.placeholders[1] subtitle.text = 'Welcome python-pptx' prs.save('test1.pptx')
练习02
slide_02 = prs.slides.add_slide(prs.slide_layouts[1]) shapes = slide_02.shapes title_shape = shapes.title body_shape = shapes.placeholders[1] title_shape.text = '添加bullet shape' tf = body_shape.text_frame tf.text = '定位到bullet 幻灯片' p = tf.add_paragraph() p.text = '使用文本框' p.level = 1 p = tf.add_paragraph() p.text = '使用文本框的二级' p.level = 2 prs.save('text1.pptx')
插入图片
from pptx import Presentation from pptx.util import Inches prs = Presentation() blank_layout = prs.slide_layouts[6] slide = prs.slides.add_slide(blank_layout) # slide.shapes.add_picture?? left = top = Inches(1) img_path = '壁纸.jpg' slide.shapes.add_picture( img_path,left,top ) prs.save('text2.pptx')
添加形状(五边形)
from pptx import Presentation from pptx.util import Inches from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[5]) shapes = slide.shapes shapes.title.text = '添加形状' left = Inches(0.93) top = Inches(3) width = Inches(1.75) height = Inches(1) # shapes.add_shape?? shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.PENTAGON,left,top,width,height) # 添加五边形 shape.text = 'step' ''' 如何在第一个五边形后边添加V型 - 添加第一个 - 通过for循环改变其内容以及位置 ''' left = left + width - Inches(0.4) width = Inches(2) for n in range(2,6): shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.CHEVRON,left,top,width,height) # 创建v型图 shape.text = 'step %d '%n # 每个V型的图片的内容填充 left = left + width - Inches(0.4) # 每个图片的位置进行累加 往右移动 prs.save('test3_shape.pptx')
读取幻灯片内容
from pptx import Presentation prs = Presentation(r'python-ppt\text1.pptx') # 打开text1.pptx文件 text_runs = [] for slide in prs.slides: for shape in slide.shapes: if not shape.has_text_frame: continue for para in shape.text_frame.paragraphs: for run in para.runs: text_runs.append(run.text) print(text_runs)
这篇关于办公自动化--python-pptx的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享