python合并多个pdf文件

假设您有个无聊的工作,将几十个PDF文档合并成一个PDF文件。 他们每个都有封面页作为第一页,但你不希望在最终结果中重复覆盖表。 即使有有很多免费的程序来组合PDF,其中许多只是合并整个文件在一起。 让我们编写一个Python程序来自定义哪些页面你想要的是组合PDF。从高层次来看,这是程序将要做的事情:

  • 查找当前工作目录中的所有PDF文件。
  • 对文件名进行排序,以便按顺序添加PDF。
  • 将每个PDF的每个页面(不包括第一页)写入输出文件。
    在实现方面,您的代码需要执行以下操作:
  • 调用 os.listdir() 来查找工作目录中的所有文件,删除所有非PDF文件。
  • 调用Python的sort()列表方法来按字母顺序排列文件名。
  • 为输出PDF创建PdfFileWriter对象。
  • 遍历每个PDF文件,为其创建PdfFileReader对象。
  • 在每个PDF文件中循环遍历每个页面(第一页除外)。
  • 将页面添加到输出PDF。
  • 将输出PDF写入名为allminutes.pdf的文件。
    对于此项目,请打开一个新的文件编辑器窗口并将其另存为 “combinePdfs.py”

Step 1:找到所有的PDF文件

首先,您的程序需要获取所有扩展名为.pdf的文件的列表
当前的工作目录并对它们进行排序。 让你的代码看起来像
以下:

在这里插入代码片

在shebang线和关于什么的描述性评论之后程序没有,这段代码导入了os和PyPDF2模块。该
os.listdir(’.’) 调用将返回当前工作中的每个文件的列表目录。 代码循环遍历此列表,并仅添加带有.pdf扩展的那些文件pdfFiles。之后,此列表按字母顺序排序,使用key = str.lower关键字参数对sort() 进行排序。创建PdfFileWriter对象以保存组合的PDF页面。最后,一些评论概述了该计划的其余部分。

#! /usr/bin/python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.import PyPDF2, os# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('.'):if filename.endswith('.pdf'):pdfFiles.append(filename)
pdfFiles.sort(key = str.lower)pdfWriter = PyPDF2.PdfFileWriter()# TODO: Loop through all the PDF files.# TODO: Loop through all the pages (except the first) and add them.# TODO: Save the resulting PDF to a file.

第二步:打开每一个 PDF 文件

现在程序必须读取pdfFiles中的每个PDF文件。 添加以下内容:

#! /usr/bin/python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.import PyPDF2, os# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('.'):if filename.endswith('.pdf'):pdfFiles.append(filename)
pdfFiles.sort(key = str.lower)pdfWriter = PyPDF2.PdfFileWriter()# Loop through all the PDF files.
for filename in pdfFiles:pdfFileObj = open(filename, 'rb')pdfReader = PyPDF2.PdfFileReader(pdfFileObj)# TODO: Loop through all the pages (except the first) and add them.# TODO: Save the resulting PDF to a file.

对于每个PDF,循环通过以读二进制模式(以’rb’作为第二个参数)调用open() 。 open()调用返回一个File对象,它被传递给PyPDF2.PdfFileReader() 。

第三步: 添加每一页

对于每个PDF,您都希望遍历除第一个页面之外的每个页面。 加上这个代码到你的程序:

#! /usr/bin/python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.import PyPDF2, os# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('.'):if filename.endswith('.pdf'):pdfFiles.append(filename)
pdfFiles.sort(key = str.lower)pdfWriter = PyPDF2.PdfFileWriter()# Loop through all the PDF files.
for filename in pdfFiles:pdfFileObj = open(filename, 'rb')pdfReader = PyPDF2.PdfFileReader(pdfFileObj)# Loop through all the pages (except the first) and add them.for pageNum in range(1, pdfReader.numPages):pageObj = pdfReader.getPage(pageNum)pdfWriter.addPage(pageObj)# TODO: Save the resulting PDF to a file.

for循环中的代码将每个Page对象分别复制到PdfFileWriter对象。 请记住,您想跳过第一页。 以来
PyPDF2认为0是第一页,你的循环应该从1 开始,然后转到但不包括pdfReader.numPages中的整数。

第四步: 保存结果

在这些嵌套的for循环完成循环之后,pdfWriter变量将会循环包含PdfFileWriter对象,其中包含所有PDF的页面。最后一步是将此内容写入硬盘驱动器上的文件。 将此代码添加到你程序中:

#!/usr/bin/python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.import PyPDF2, os# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('/home/hux/books/python'):if filename.endswith('.pdf'):pdfFiles.append('/home/hux/books/python/'+filename)
pdfFiles.sort(key = str.lower)pdfWriter = PyPDF2.PdfFileWriter()# Loop through all the PDF files.
for filename in pdfFiles:pdfFileObj = open(filename, 'rb')pdfReader = PyPDF2.PdfFileReader(pdfFileObj, strict=False)for pageNum in range(1, pdfReader.numPages):pageObj = pdfReader.getPage(pageNum)pdfWriter.addPage(pageObj)pdfOutput = open('allminutes.pdf', 'wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()

python合并多个pdf文件相关推荐

  1. 使用python合并与切割pdf文件

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- import codecs import os import sys import PyPDF2# 文件名 ...

  2. Python实现按序合并多个pdf文件

    技术交流QQ群:1027579432,欢迎你的加入! 欢迎关注我的微信公众号:CurryCoder的程序人生 1.整体实现步骤 在日常办公中,我们可能会有一个需求,需要将多个pdf文件合并成一个文件. ...

  3. python合并多个pdf_python合并多个pdf文件

    假设您有个无聊的工作,将几十个PDF文档合并成一个PDF文件. 他们每个都有封面页作为第一页,但你不希望在最终结果中重复覆盖表. 即使有有很多免费的程序来组合PDF,其中许多只是合并整个文件在一起. ...

  4. python多图片合并pdf_Python结合ImageMagick实现多张图片合并为一个pdf文件的方法

    本文实例讲述了Python结合ImageMagick实现多张图片合并为一个pdf文件的方法.分享给大家供大家参考,具体如下: 前段时间买了不少书,现在手头的书籍积累的越来越多,北京这边租住的小屋子空间 ...

  5. python学习之路:合并多个excel文件、合并多个pdf文件

    **关键点: python中的os模块 python中的pandas中的concat python中的PyPDF2模块** 1.合并多个Excel文件 import os import pandas ...

  6. Python自动化办公【PDF文件自动化】

    PDF 文件可以分为可编辑型PDF 文件与扫描型PDF 文件,内容可以复制,是可编辑型PDF文件,反之则是扫描型PDF 文件.简单理解扫描型PDF文件是由一张张图像构建而成. 读取PDF文件内容 1. ...

  7. 多个pdf文件合并为一个pdf文件

    简单代码搞定将多个pdf文件合并为同一个pdf文件,有python你还有啥担心的呢?在日常生活中需要用到的将多个pdf文件合并为同一个pdf文件的请看过来,不要再去网上下载这啊那的转换软件了.话不多说 ...

  8. python使用fpdf创建pdf文件包含:页眉、页脚并嵌入logo图片、设置使用中文字体

    python使用fpdf创建pdf文件包含:页眉.页脚并嵌入logo图片.设置使用中文字体 #python使用fpdf创建页眉.页脚并嵌入logo图片.设置使用中文字体 from fpdf impor ...

  9. python使用fpdf生成pdf文件章节(chapter),包含:页眉、页脚、章节主题、数据排版等;

    python使用fpdf生成pdf文件章节(chapter),包含:页眉.页脚.章节主题.数据排版等: #仿真数据 The year 1866 was marked by a bizarre deve ...

  10. python使用fpdf生成pdf文件:配置多种语言字体写入多种文字

    python使用fpdf生成pdf文件:配置多种语言字体写入多种文字 目录

最新文章

  1. DeepMind大放送:开放4个多物体表征学习数据集,还有一篇智能体新研究
  2. 无界限设计加持!魅族手环正式亮相
  3. windows命令行无法启动redis_Win10 3分钟简单、快速安装Redis
  4. ubuntu19.10锁屏时间调节
  5. mysql读写分离 存储过程_基于maxscale的读写分离部署笔记
  6. system函数_自学C++基础教程【函数】
  7. redis查看某一个key的大小_Redis笔记
  8. 蓝桥杯 ADV-112 算法提高 c++_ch02_01
  9. Linux系统故障排查和修复技巧
  10. android simpliadapter的两种用法
  11. ajax 详解(GET,POST方式传输以其封装)
  12. sdp ddp内存怎么分_小鑫课堂 | 多余的内存不要浪费,拿来做硬盘吧
  13. ubuntu svn命令
  14. 模拟电子技术 项目课 多种波形发生器(方波、三角波、正弦波)
  15. 关于Dubbo的面试题,建议收藏
  16. 如何理解新技术带来的新资产类别?
  17. iOS 数据库升级策略
  18. py实现高斯列选主元消元法
  19. 海外服务器跟国内服务器对比分别有哪些优势呢
  20. HTML基础——table标签

热门文章

  1. 计算机无线网络计算机文件共享,两台电脑怎么用无线网络共享文件?
  2. 指付通盗刷信用卡维权连载--9月4日维权纪实
  3. python3.7的idle打不开解决办法_python3.4idle为什么打不开
  4. 打印机脱机了怎么恢复打印
  5. 怎么建立局域网_用ZeroTier搭建属于自己的虚拟局域网(VLAN)
  6. 英语面试自我介绍范文(二)
  7. CarPlay iAP2附件协议
  8. 遭遇Excel的宏病毒
  9. 如何给word文档添加注释
  10. JavaScript 3D球形标签云代码