python 生成 word、ppt 操作源码分享
使用 python 生成 word
# coding=utf-8
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn
# 打开文档
document = Document()
# 加入不同等级的标题
document.add_heading(u'MS WORD写入测试', 0)
document.add_heading(u'一级标题', 1)
document.add_heading(u'二级标题', 2)
# 添加文本
paragraph = document.add_paragraph(u'我们在做文本测试!')
# 设置字号
run = paragraph.add_run(u'设置字号、')
run.font.size = Pt(24)
# 设置字体
run = paragraph.add_run('Set Font,')
run.font.name = 'Consolas'
# 设置中文字体
run = paragraph.add_run(u'设置中文字体、')
run.font.name = u'宋体'
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
# 设置斜体
run = paragraph.add_run(u'斜体、')
run.italic = True
# 设置粗体
run = paragraph.add_run(u'粗体').bold = True
# 增加引用
document.add_paragraph('Intense quote', style='Intense Quote')
# 增加无序列表
document.add_paragraph(
u'无序列表元素1', style='List Bullet'
)
document.add_paragraph(
u'无序列表元素2', style='List Bullet'
)
# 增加有序列表
document.add_paragraph(
u'有序列表元素1', style='List Number'
)
document.add_paragraph(
u'有序列表元素2', style='List Number'
)
# 增加图像(此处用到图像image.bmp,请自行添加脚本所在目录中)
document.add_picture('hz.png', width=Inches(1.25))
# 增加表格
table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Name'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
# 再增加3行表格元素
for i in range(3):
row_cells = table.add_row().cells
row_cells[0].text = 'test' + str(i)
row_cells[1].text = str(i)
row_cells[2].text = 'desc' + str(i)
# 增加分页
document.add_page_break()
# 保存文件
document.save(u'测试.docx')
使用 python 生成 ppt
简单生成单页面 ppt
from pptx import Presentation
# 创建幻灯片 ------
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
# 设置标题和副标题
title.text = "Hello, World!"
subtitle.text = "pip install python-pptx"
prs.save("test.pptx")
图表输出到 ppt
效果图
代码
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
# 创建幻灯片 ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
# 幻灯片标题 ------
title = slide.shapes.title
title.text = "ppt表格"
# 定义图表数据 ---------------------
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
# 将图表添加到幻灯片 --------------------
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
prs.save('chart-01.pptx')
使用 ppt 模板来生成 ppt
准备 ppt 模板(网络下载或自定义幻灯片母版)
加载 ppt 模板,并使用指定幻灯片样式
添加数据并生成新 ppt
效果图
代码
from pptx import Presentation
from pptx.util import Inches
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm # Inches
from pptx.enum.chart import XL_LEGEND_POSITION
if __name__ == '__main__':
# 创建幻灯片 ------
prs = Presentation('template.pptx')
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = '报告'
# 定义表格数据 ------
name_objects = ["object1", "object2", "object3"]
name_AIs = ["AI1", "AI2", "AI3"]
val_AI1 = (19.2, 21.4, 16.7)
val_AI2 = (22.3, 28.6, 15.2)
val_AI3 = (20.4, 26.3, 14.2)
val_AIs = [val_AI1, val_AI2, val_AI3]
# 表格样式 --------------------
rows = 4
cols = 4
top = Cm(12.5)
left = Cm(3.5) # Inches(2.0)
width = Cm(24) # Inches(6.0)
height = Cm(6) # Inches(0.8)
# 添加表格到幻灯片 --------------------
table = shapes.add_table(rows, cols, left, top, width, height).table
# 设置单元格宽度
table.columns[0].width = Cm(6) # Inches(2.0)
table.columns[1].width = Cm(6)
table.columns[2].width = Cm(6)
table.columns[3].width = Cm(6)
# 设置标题行
table.cell(0, 1).text = name_objects[0]
table.cell(0, 2).text = name_objects[1]
table.cell(0, 3).text = name_objects[2]
# 填充数据
table.cell(1, 0).text = name_AIs[0]
table.cell(1, 1).text = str(val_AI1[0])
table.cell(1, 2).text = str(val_AI1[1])
table.cell(1, 3).text = str(val_AI1[2])
table.cell(2, 0).text = name_AIs[1]
table.cell(2, 1).text = str(val_AI2[0])
table.cell(2, 2).text = str(val_AI2[1])
table.cell(2, 3).text = str(val_AI2[2])
table.cell(3, 0).text = name_AIs[2]
table.cell(3, 1).text = str(val_AI3[0])
table.cell(3, 2).text = str(val_AI3[1])
table.cell(3, 3).text = str(val_AI3[2])
# 定义图表数据 ---------------------
chart_data = ChartData()
chart_data.categories = name_objects
chart_data.add_series(name_AIs[0], val_AI1)
chart_data.add_series(name_AIs[1], val_AI2)
chart_data.add_series(name_AIs[2], val_AI3)
# 添加图表到幻灯片 --------------------
x, y, cx, cy = Cm(3.5), Cm(4.2), Cm(24), Cm(8)
graphic_frame = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
chart = graphic_frame.chart
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.TOP
chart.legend.include_in_layout = False
value_axis = chart.value_axis
value_axis.maximum_scale = 100.0
value_axis.has_title = True
value_axis.axis_title.has_text_frame = True
value_axis.axis_title.text_frame.text = "False positive"
# value_axis.axis_title.text_frame.auto_size = 10
prs.save('test_template.pptx')
回帖内容已被屏蔽。
我自己也是为了收藏一下,哈哈哈
这个可以收藏了,太棒了