基础
import pptx
from pptx.util import Cm
# 读取/创建ppt
prs = pptx.Presentation('ppt/Gucci')
# 保存ppt
prs.save('...')
# 选择幻灯片slide
slide = prs.slides[0]
# 选择幻灯片里的元素
shape = slide.shapes[0]
# 选择幻灯片里的表格
tables = []
for t in slide.shapes:
if t.has_table:
tables.append(t)
# 添加文本框
text_shape = slide.shapes.add_textbox(left=Cm(1), top=Cm(1), width=Cm(1), height=(1)) # left和top表示距离幻灯片左上角的距离,width和height表示文本框的大小
set_font_style(text_shape, ...) # 设置文本格式
# 添加图片
pic = slide.shapes.add_picture(left, top, width, height)
# 添加表格
table = slide.shapes.add_table(rows, cols, left, top, width, height)
# 选择单元格
table.cell(row, col)
# 单元格写入文本、修改单元格格式,每个单元格都可视为一个文本框来修改;目前只能一格一格地写入
set_font_style(text_shape=table.cell(row, col), text, ...)
# 合并单元格
table.cell(0, 0).merge(table.cell(0, 1))
# 设置单元格宽度和高度
table.columns[0].width = Cm(1)
table.rows[0].height = Cm(1.2)
字体
def set_font_style(text_shape, text='', font_name='微软雅黑', font_size=12, background_color=None, font_color=None,
line_spacing=None, loc='center', bold=False, italic=False, wrap = True, p=0, add_run=False):
'''
背景颜色,居中
p: 段落
add_fun: 追加文字
line_space: 行间距
loc: 位置,'center'|'left'|'right'
wrap: 自动换行
bold: 加粗
italic: 斜体
'''
if text:
if add_run:
run = text_shape.text_frame.paragraphs[p].add_run()
run.text = text
font_style = run
elif p==0:
text_shape.text = text
font_style = text_shape.text_frame.paragraphs[p]
elif p>0: # 添加段落
text_shape.text_frame.add_paragraph().text = text
font_style = text_shape.text_frame.paragraphs[p]
else:
font_style = text_shape.text_frame.paragraphs[p]
font_style.font.size = pptx.util.Pt(font_size) # 大小
font_style.font.name = font_name # 字体
font_style.font.bold = bold # 加粗
font_style.font.italic = italic # 斜体
text_shape.text_frame.word_wrap = wrap # 自动换行
if background_color:
text_shape.fill.solid()
text_shape.fill.fore_color.rgb = background_color # 背景颜色
if font_color:
font_style.font.color.rgb = font_color # 颜色
if loc=='center':
font_style.alignment = pptx.enum.text.PP_ALIGN.CENTER # 居中
elif loc=='left':
font_style.alignment = pptx.enum.text.PP_ALIGN.LEFT
elif loc=='right':
font_style.alignment = pptx.enum.text.PP_ALIGN.RIGHT
if line_spacing:
font_style.line_spacing = line_spacing # 行距
表格
参考文章:
- https://blog.csdn.net/jcjic/article/details/123491918
- https://blog.csdn.net/qq_37144341/article/details/107972829
- https://cloud.tencent.com/developer/article/1805504
def add_tables(slide, df, n, table_name, table_left=4, space=5, font_size=10, font_name='微软雅黑'):
'''
add tables to ppt
params:
slide: ppt.slide
df: fill data, each brand name in df
n: which table
table_name: # of posts | # of interactions
table_left:
table_font_size:
'''
t = slide.shapes.add_table(rows=5, cols=3, left=Cm(table_left+space*n), top=Cm(11.5),
width=Cm(5), height=Cm(4.6))
table = t.table
# 第一行
table.cell(0, 0).text = table_name
table.cell(0, 2).text = 'YOY%'
for i in range(3):
set_font_style(table.cell(0, i), font_size=font_size, bold=True, font_name=font_name)
table.cell(0, 2).text_frame.paragraphs[0].font.italic = True # 斜体
# 合并单元格
table.cell(0, 0).merge(table.cell(0, 1))
table.cell(0, 0).text_frame.paragraphs[0].alignment = pptx.enum.text.PP_ALIGN.CENTER # 居中
# 修改表格宽度
table.columns[0].width = Cm(1)
table.columns[1].width = Cm(2)
table.columns[2].width = Cm(1.7)
# 表头高度
table.rows[0].height = Cm(1.2)
for row in range(0, 5):
for col in range(0, 3):
if row==0:
table.cell(row, col).fill.solid() # init cell style
table.cell(row, col).fill.fore_color.rgb = pptx.dml.color.RGBColor(0, 0, 0) # 背景颜色
else:
if col!=0:
table.cell(row, col).text = str(df.iloc[row-1, col-1])
set_font_style(table.cell(row, col), background_color=pptx.dml.color.RGBColor(242, 242, 242), font_size=font_size, font_name=font_name)
if row!=0:
pic = slide.shapes.add_picture(f'data/pl_logo/{df.index[row-1]}.png',
left=Cm(table_left+space*n+0.1), top=Cm(11.85+0.9*row), height=Cm(0.78))