python实现批量生成会议台卡(pyQt5)

`# 导入所需库
from pptx import Presentation
from pptx.enum.text import PP_ALIGN
from pptx.util import Pt
import pandas as pd # 新增导入pandas库
from PyQt5 import QtWidgets # 新增导入PyQt5库

创建应用程序

app = QtWidgets.QApplication([])

输入数字

num_copies = 1 # 直接设置为1,去掉输入对话框

读取模板PPT和Excel文件

template_ppt = Presentation(QtWidgets.QFileDialog.getOpenFileName(None, "选择模板PPT", "", "PPTX Files (.pptx)")[0]) # 选择模板PPT
excel_file = QtWidgets.QFileDialog.getOpenFileName(None, "选择Excel文件", "", "Excel Files (
.xlsx)")[0] # 选择Excel文件
excel_data = pd.read_excel(excel_file) # 读取Excel文件

复制第一页

first_slide = template_ppt.slides[0]
num_rows = len(excel_data) # 获取Excel数据的行数
for i in range(min(int(num_copies), num_rows)): # 确保不超过行数
# 创建新幻灯片并复制内容
new_slide = template_ppt.slides.add_slide(first_slide.slide_layout)
for shape in first_slide.shapes:
if shape.has_text_frame:
new_shape = new_slide.shapes.add_textbox(shape.left, shape.top, shape.width, shape.height)
new_shape.text = str(excel_data.iloc[i, 0]) # 使用Excel中每个单元格的文本

        # 设置文本框文字居中,字体为微软雅黑,大小为120
        text_frame = new_shape.text_frame
        for paragraph in text_frame.paragraphs:
            paragraph.alignment = PP_ALIGN.CENTER  # 文本居中
        for run in text_frame.paragraphs[0].runs:
            run.font.name = '微软雅黑'  # 设置字体
            run.font.size = Pt(120)  # 设置字体大小
            run.font.bold = True  # 设置字体加粗

# {{ edit_1 }} 旋转第一个文本框180度
if new_slide.shapes:  # 确保新幻灯片有形状
    new_slide.shapes[1].rotation = 180  # 旋转第一个文本框180度

保存新的PPT

output_file, _ = QtWidgets.QFileDialog.getSaveFileName(None, "另存为", "output.pptx", "PPTX Files (*.pptx)") # 选择保存路径
if output_file: # 确保用户选择了文件名
template_ppt.save(output_file) # 保存为用户指定的文件名
`

posted @ 2024-09-19 00:41  龙岩龙  阅读(53)  评论(0)    收藏  举报