【Python】pdf转ppt
代码环境基于python3
引用:
https://www.cnblogs.com/loveprogramme/p/11247037.html
https://blog.csdn.net/weixin_46426157/article/details/108110894
https://github.com/phasedOut/pdf2pptx
https://github.com/Derekchen147/pdf2ppt
直接上代码
下载需要的包
# vim requirements.txt
fitz
os
time
tqdm
datetime
Image
Presentation
Inches
# pip3 install -r requirements.txt -i https://pypi.douban.com/simple
如果没有pip3命令,就用pip命令。
安装fitz报错
running installrunning buildrunning build_pyrunning build_extbuilding 'fitz._fitz' extensionswigging fitz/fitz.i to fitz/fitz_wrap.cswig -python -o fitz/fitz_wrap.c fitz/fitz.iunable to execute 'swig': No such file or directoryerror: command 'swig' failed with exit status 1----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-ijzpcyaz/PyMuPDF/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-atf0rquy/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-ijzpcyaz/PyMuPDF/------
# 安装swig 然后重试
yum install swig
或
apt install swig
------Failed building wheel for scipyRunning setup.py clean for scipyComplete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-pykkl320/scipy/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" clean --all:`setup.py clean` is not supported, use one of the following instead:- `git clean -xdf` (cleans all files)- `git clean -Xdf` (cleans all versioned files, doesn't touchfiles that aren't checked into the git repo)Add `--force` to your command to use it anyway if you must (unsupported).# 升级pip wheel setuptools
pip3 install --upgrade pip setuptools wheel -i https://pypi.douban.com/simple
安装完后,重试安装panda报错ERROR: Cannot unpack file /tmp/pip-unpack-xq_hmo_x/simple.htm (downloaded from /tmp/pip-req-build-df6dsm0s, content-type: text/html); cannot detect archive format
ERROR: Cannot determine archive format of /tmp/pip-req-build-df6dsm0s
解决方法:
指定 --trusted-host pypi.tuna.tsinghua.edu.cn 然后重试
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn 模块名
安装完成后,执行下面的py程序
import fitz
import time
from tqdm import tqdm
import os
from PIL import Image
from pptx import Presentation
from pptx.util import Inchesbase_path = input("请输入要转换的文件路径:")
filenames = os.listdir(base_path)
#for filename in os.listdir('source_files/'):
for filename in filenames:# 将pdf一张张切割成jpgtimestamp = time.time()print('切割pdf为jpg...')# full_path = "./source_files/%s" % filename # 拼接,得到PDF文件的绝对路径full_path = os.path.join(base_path, filename)print(full_path)doc = fitz.open(full_path)rotate = int(0) # 设置图片的旋转角度,如果图片方向不对的话改这个zoom_x = 2.0 # 设置图片相对于PDF文件在X轴上的缩放比例zoom_y = 2.0 # 设置图片相对于PDF文件在Y轴上的缩放比例trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate) #旋转图片,如果图片方向不对的话改前面rotate的旋转角度print("%s开始转换..." % filename)if doc.page_count > 1: # 获取PDF的页数for pg in tqdm(range(doc.page_count)):page = doc[pg] # 获得第pg页pm = page.get_pixmap(matrix=trans, alpha=False) # 将其转化为光栅文件(位数)new_full_name = filename.split(".")[0] # 保证输出的文件名不变if not os.path.exists('%s/%s' % (base_path, new_full_name)):os.mkdir('%s/%s' % (base_path, new_full_name))pm.save("%s/%s/%s-%s.jpg" % (base_path, new_full_name, new_full_name, pg)) # 将其输入为相应的图片格式,可以为位图,也可以为矢量图# 我本来想输出为jpg文件,但是在网页中都是png格式(即调用writePNG),再转换成别的图像文件前,最好查一下是否支持else:page = doc[0]pm = page.get_pixmap(matrix=trans, alpha=False)new_full_name = full_path.split(".")[0]if not os.path.exists('/%s/%s' % (base_path, new_full_name)):os.mkdir('%s/%s' % (base_path, new_full_name))pm.save("%s/%s/%s-%s.jpg" % (base_path, new_full_name, new_full_name, pg))# pm.save("%s.jpg" % new_full_name)print("%s转换jpg完成!" % filename)print('耗时:', time.time() - timestamp, 's')print('\n\n')# 将分割好的jpg图片整合到ppttimestamp = time.time()print('整合jpg为ppt...')# 将filename改成不带格式名字的str,如file.pdf则filename = filefilename = filename.split(".")[0]# jpg保存的地址jpg_path = '%s/%s' % (base_path, filename)pages = os.listdir(jpg_path)prs = Presentation()# 在这里修改ppt长宽格式。默认是16*9prs.slide_width = Inches(16)prs.slide_height = Inches(9)for index, page in enumerate(tqdm(pages)):# 得到单张jpg图片的地址jpg_file = "%s/%s/%s-%d.jpg" % (base_path, filename, filename, index)# 获取jpg文件的长宽image = Image.open(jpg_file)height = image.heightwidth = image.width# #Rotate 270 degrees if horizontal# if height > width:# adjusted = image.rotate(270, expand=True)# adjusted.save(jpg_file)# 设置ppt的slidetitle_slide_layout = prs.slide_layouts[0]slide = prs.slides.add_slide(title_slide_layout)# 将图片插入slide中。# left和top表示图片与slide边框的距离,默认为0# height=prs.slide_height, width=prs.slide_width调整图片大小,默认塞满整个slideleft = top = 0slide.shapes.add_picture(jpg_file, left, top, height=prs.slide_height, width=prs.slide_width)prs.save('%s/%s.pptx' % (base_path, filename))print("成功保存ppt文件 %s.pptx", filename)print('耗时:', time.time() - timestamp, 's')print('\n\n')
** 使用方法**
输入的方式为请输入要转换的文件路径:/home/zclinux/Desktop/pdf2ppt
如果路径最后多添加一条/
则会报错。
【Python】pdf转ppt相关推荐
- python 根据word生成ppt_未明学院:利用Python将Wordamp;PPT批量转成PDF
Python中的许多库,像一个个具有不同功能的"工具",能帮助我们解决工作中的不同问题,提高工作效率! 本期将继续为大家放松python中的实用技能,本期的主题是:利用Python ...
- Python从Word/PPT/PDF中抽取图片
Python从Word/PPT/PDF中抽取图片 PS 1:也是从网上各个帖子中学习的代码,因此代码的格式以及内容有粘贴网上其他大神的代码,如有侵权请告知删除 2:本次设计意在用pyinstaller ...
- 使用Python实现将ppt文件批量转化为pdf
使用pywin32模块完成主要功能,程序整体实现思路 1. 初始化PowerPoint 端口应用程序 2. 使用应用程序对象打开ppt文件 3. 将ppt文件直接转存为PDF文件 特点:在后台运行,不 ...
- [Python]PDF文件与图片转换(PyMuPDF)及转PPT
文章目录 安装与简介 MuPDF PyMuPDF PyMuPDF使用 元数据 页面Page 代码示例 PDF转图片 图片转PDF PDF转PPT PyMuPDF提供了PDF及流行图片处理接口. 安装与 ...
- 利用python将PDF转为PPT(课件专用)
利用python将PDF转为PPT(课件专用) 前言:课程中老师经常会将课件作为PDF发放而非PPT,而现有的PDF阅读器一般不支持添加修改等操作,所以显得十分麻烦,考虑将PDF转换为PPT格式,方便 ...
- 好用到爆的Python自动化办公教程pdf,Python × Excel × Word × PPT 一次搞定
在这个自动化时代,我们有很多重复无聊的工作要做.想想这些你不再需要一次又一次地做的无聊的事情,让它自动化,让你的生活更轻松.那么在本文中,我将向您介绍一个好用到爆的Python自动化办公教程pdf,P ...
- 利用python做一个可批量将PDF转PPT文件的exe简单小程序
前言:在这里并不是将PDF中的文字和图片转为相对应的PPT,只是简单的把PDF作为图片插入到PPT中! 最终呈现的效果如下图: 话不多说,直接开干! 第一步:先写出PDF转PPT的代码. import ...
- 用Python玩转PPT!
作者 | 陈熹 来源 | 早起Python 今天本文将基于第三方库pptx,详细讲解如何使用Python操作Office全家桶最后一位--PPT. 安装 pptx是一个非标准库,需要在命令行中安装 p ...
- 带你用Python玩转PPT
作者 | 陈熹 来源 | 早起Python(ID:zaoqi-python) 头图 | CSDN 下载自东方IC 导读 大家好,今天依旧是Python办公自动化基础系列,在之前我们分别详细讲解了 ? ...
- 关于python的ppt_用Python玩转PPT
作者 | 陈熹 来源 | 早起Python 今天本文将基于第三方库pptx,详细讲解如何使用Python操作Office全家桶最后一位--PPT. 安装 pptx是一个非标准库,需要在命令行中安装 p ...
最新文章
- 人生的抉择-创业纪录片(二)-起步期
- 一、flask的基本使用-flask
- 经典C语言程序100例之四五
- 第三次学JAVA再学不好就吃翔(part88)--ArrayList嵌套ArrayList
- Windows Phone 7 开发 31 日谈——第22日:应用?还是 游戏?
- HTML的SEO(搜索引擎优化)标准
- python写一个完整的小程序_写一个python小程序
- 来,我们一起学Hibernate
- Jdk1.6.0+Tomcat6.0环境变量配置
- CAD无吊顶画弱电点位图总结
- python工业机器人_工业机器人编程语言汇总!
- c语言如何生成csv文件格式,生成 csv 文件
- unity tags的坑
- 漏洞分析丨HEVD-0x6.UninitializedStackVariable[win7x86]
- YOLOv5桌面应用开发,手把手教学实操(上)——附源代码
- font-size:字号大小
- 吉度盘点机PDA盘点生成的条码和数量导入库存Excel表
- ·穷途末路之举·解决VirtualBox启动问题-Error relaunching VirtualBox VM process 5/terminated with exit code1(0x1)
- 身份证正则 身份证正则表达式
- 怎么将pdf压缩?pdf文件如何压缩?