python 拆分pdf指定页,Python按页拆分pdf
重构
我对代码进行了如下重构:import os
import PyPDF2
def split_pdf_pages(input_pdf_path, target_dir, fname_fmt=u"{num_page:04d}.pdf"):
if not os.path.exists(target_dir):
os.makedirs(target_dir)
with open(input_pdf_path, "rb") as input_stream:
input_pdf = PyPDF2.PdfFileReader(input_stream)
if input_pdf.flattenedPages is None:
# flatten the file using getNumPages()
input_pdf.getNumPages() # or call input_pdf._flatten()
for num_page, page in enumerate(input_pdf.flattenedPages):
output = PyPDF2.PdfFileWriter()
output.addPage(page)
file_name = os.path.join(target_dir, fname_fmt.format(num_page=num_page))
with open(file_name, "wb") as output_stream:
output.write(output_stream)
注意:很难做得更好
剖面
使用这个split_pdf_pages函数,您可以进行分析:import cProfile
import pstats
import io
pdf_path = "path/to/file.pdf"
directory = os.path.join(os.path.dirname(pdf_path), "pages")
pr = cProfile.Profile()
pr.enable()
split_pdf_pages(pdf_path, directory)
pr.disable()
s = io.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
ps.print_stats()
print(s.getvalue())
使用您自己的PDF文件运行分析,并分析结果
分析结果
分析结果如下:159696614 function calls (155047949 primitive calls) in 57.818 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.899 0.899 57.818 57.818 $HOME/workspace/pypdf2_demo/src/pypdf2_demo/split_pdf_pages.py:14(split_pdf_pages)
2136 0.501 -.--- 53.851 0.025 $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/pdf.py:445(write)
103229/96616 1.113 -.--- 36.924 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/generic.py:544(writeToStream)
27803 9.066 -.--- 25.381 0.001 $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/generic.py:445(writeToStream)
4185807/2136 5.054 -.--- 14.635 0.007 $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/pdf.py:541(_sweepIndirectReferences)
50245/41562 0.117 -.--- 9.028 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/pdf.py:1584(getObject)
31421489 6.898 -.--- 8.193 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/utils.py:231(b_)
56779 2.070 -.--- 7.882 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/generic.py:142(writeToStream)
8683 0.322 -.--- 7.020 0.001 $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/pdf.py:1531(_getObjectFromStream)
459978/20068 1.098 -.--- 6.490 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/generic.py:54(readObject)
26517/19902 0.484 -.--- 6.360 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/generic.py:553(readFromStream)
27803 3.893 -.--- 5.565 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/generic.py:1162(encode_pdfdocencoding)
15735379 4.173 -.--- 5.412 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/utils.py:268(chr_)
3617738 2.105 -.--- 4.956 -.--- $HOME/virtualenv/py3-pypdf2_demo/lib/site-packages/PyPDF2/generic.py:265(writeToStream)
18882076 3.856 -.--- 3.856 -.--- {method 'write' of '_io.BufferedWriter' objects}
看来:writeToStream函数被大量调用,但我不知道如何优化它。
write方法直接写入流,而不是在内存中=>;可以进行优化。
改善
在缓冲区(内存中)中序列化PDF页,然后将缓冲区写入文件:buffer = io.BytesIO()
output.write(buffer)
with open(file_name, "wb") as output_stream:
output_stream.write(buffer.getvalue())
我用35秒而不是40秒处理了2135页。
优化确实很差:-(
python 拆分pdf指定页,Python按页拆分pdf相关推荐
- python读取excel指定列-Python读取excel指定列生成指定sql脚本的方法
需求 最近公司干活,收到一个需求,说是让手动将数据库查出来的信息复制粘贴到excel中,在用excel中写好的公式将指定的两列数据用update这样的语句替换掉. 例如: 有个A库,其中有两个A.01 ...
- python使用fpdf创建pdf文件包含:页眉、页脚并嵌入logo图片、设置使用中文字体
python使用fpdf创建pdf文件包含:页眉.页脚并嵌入logo图片.设置使用中文字体 #python使用fpdf创建页眉.页脚并嵌入logo图片.设置使用中文字体 from fpdf impor ...
- python使用fpdf生成pdf文件章节(chapter),包含:页眉、页脚、章节主题、数据排版等;
python使用fpdf生成pdf文件章节(chapter),包含:页眉.页脚.章节主题.数据排版等: #仿真数据 The year 1866 was marked by a bizarre deve ...
- python使用fpdf生成pdf章节(chapter)文件包含:页眉、页脚、章节主体、章节内容等;
python使用fpdf生成pdf章节(chapter)文件包含:页眉.页脚.章节主体.章节内容等: 目录
- python 字符串转日期_我总结的130页Python与机器学习之路V1.2.pdf,都是干货!
告别枯燥,通过学习有趣的小例子,扎实而系统的入门Python,从菜鸟到大师,个人觉得这是很靠谱的一种方法.通过一个又一个的小例子,真正领悟Python之强大,之简洁,真正做到高效使用Python. 两 ...
- Python初学尝试:word批量刷格式转PDF,替换页眉页脚
Python初学尝试:word批量刷格式转PDF,替换页眉页脚 初学尝试 round 1 ronund 2 easygui round 4 PyQt5 round 4 wxpython 小结 初学尝试 ...
- python使用fpdf生成pdf文件并添加页眉和页脚
python使用fpdf生成pdf文件并添加页眉和页脚 目录 python使用fpdf生成pdf文件并添加页眉和页脚 #加入页眉header示例
- python分解word文档为多个_将一个word文档按一页或多页拆分成多个文档
工作中,有时候碰到需要将一个比较大的word按照指定的页数分割成若干个小的word文档,下面提供分割的方法供参考: 一.按照单页拆分 1.在Word里面打开那个需要分割的文档(假设它的文件名叫做&qu ...
- python 删除pdf页面_使用python从新生成的pdf文件中删除空白页
使用下面的代码,我试图将图片从目录粘贴到PDF文件中.代码已经在工作,生成我的PDF几乎如我所愿.在 唯一的问题是它总是在图片之间添加空白页,我不知道为什么. 如果我执行代码,PDF将以空白页开始,然 ...
- 使用python的PyPDF2 库删除pdf文件中的空白页
参考文章:https://blog.csdn.net/xingxtao/article/details/79056341 pdf中的第4页和第10页是空白页.为了将其删除,使用以下代码: from P ...
最新文章
- C语言之结构体以及结构体对齐访问
- 安装python 的包控制mysql的Python脚本与执行系统命令的Python脚本
- Python判断一个字符串是否包含某个指定的字符串
- SPOJ - DISUBSTR Distinct Substrings(后缀数组)
- 全志A33-ARM linux开机进入uboot命令行
- 4位16色灰度图像处理
- 【LeetCode】3月26日打卡-Day11
- return两个返回值_异步函数的两个视角
- 帝国CMS7.5仿《问答库》题库问答学习平台网站源码 带手机版
- css3禅密花园叫什么名字_CSS秘密花园: 自定义下划线
- 无代码火了,短板有哪些?
- Facebook对memcached的提升
- 开机netmeeting已删除_NetMeeting
- 多linux系统u盘制作,高手教你如何做一个U盘Linux系统
- 基于FPGA的DHT11数字温湿度传感器测试
- 刘夏真的简历中国科学院计算机所,刘夏_广西医科大学研究生导师信息
- 西南大学网络与继续教育学院课程考试试题卷
- 干货 | 深度学习之卷积神经网络(CNN)的模型结构
- 基于MATLAB的图像分割系统
- FANUC机器人4种启动方式的具体操作步骤