要将PDF文件转换为PPT,我实在python3.9 环境下转成功的,python3.11不行。
需要
pip install PyMuPDF
代码说话
# -*- coding: utf-8 -*-
"""
@author: 赫凯
@software: PyCharm
@file: xxx.py
@time: 2023/12/21 11:20
"""
import fitz # PyMuPDF
from pptx import Presentation
from pptx.util import Inches
# 设置ppt的长和宽
width_X = Inches(13.33)
height_Y = Inches(7.5)
def pdf_to_ppt(input_pdf, output_ppt):
# 打开PDF
pdf_document = fitz.open(input_pdf)
# 创建一个空的PPT文档
ppt = Presentation()
# 设置幻灯片尺寸为16:9
ppt.slide_width = width_X
ppt.slide_height = height_Y
for page_number in range(pdf_document.page_count):
# 创建一个新的PPT幻灯片
slide = ppt.slides.add_slide(ppt.slide_layouts[0])
# 添加PDF页面内容到PPT幻灯片中
pdf_page = pdf_document.load_page(page_number)
image_name = f'pdf_image_{page_number}.png'
pdf_page.get_pixmap(matrix=fitz.Matrix(2, 2)).save(image_name)
left = top = Inches(0)
slide.shapes.add_picture(image_name, left, top, width=width_X, height=height_Y)
# 保存PPT文件
ppt.save(output_ppt)
if __name__ == '__main__':
# 调用函数进行转换
pdf_to_ppt(r'aa.pdf', 'output.pptx')