当前位置: 首页 > news >正文

PDF分页处理:技术与实践

引言

在数字化办公和学习中,PDF文件因其便携性和格式稳定性而广受欢迎。然而,处理大型PDF文件时,我们经常需要将其拆分成单独的页面,以便于管理和分享。本文将探讨如何使用Python编程语言和一些流行的库来实现PDF文件的分页处理。

PDF分页的技术背景

PDF(Portable Document Format,便携式文档格式)是一种由Adobe系统公司开发的文件格式,用于表示文档的布局、文本、图形和其他元素。PDF文件广泛用于电子文档交换,因为它们可以在不同的操作系统和设备上保持一致的显示效果。

使用Python进行PDF分页

Python是一种灵活且功能强大的编程语言,拥有丰富的库支持,非常适合进行PDF文件的处理。以下是一些用于处理PDF的流行Python库:

PyPDF2

PyPDF2是一个强大的库,可以用于读取PDF文件、拆分页面、合并页面等。但是,从版本3.0.0开始,PyPDF2不再维护,推荐使用PdfReader替代PdfFileReader。

fitz (PyMuPDF)

fitz是另一个流行的库,它是MuPDF的Python绑定,提供了丰富的PDF处理功能。使用fitz,我们可以轻松地打开、解析和修改PDF文件。

实战:使用Python分页PDF

PDF上下分页

import os
import pandas as pd
from PyPDF2 import PdfFileReader, PdfFileWriterdef split_pdf(infile):split_pdf_file = []split_pdf_file_name = []if '/' in infile:in_File = infile.split('/')[2][:-4]else:in_File = infile[:-4]new_filepath = os.path.join('%s/%s') % ('./resluts', in_File)if not os.path.exists(new_filepath):os.makedirs(new_filepath)with open(infile, 'rb') as infile:reader = PdfFileReader(infile, strict=False)number_of_pages = reader.getNumPages()print("共{}页".format(number_of_pages))for i in range(number_of_pages):writer = PdfFileWriter()writer.addPage(reader.getPage(i))out_new_file = new_filepath + '/' + str(i + 1)if not os.path.exists(out_new_file):os.makedirs(out_new_file)out_file_name = out_new_file + '/' + str(i + 1) + '.pdf'with open(out_file_name, 'wb') as outfile:writer.write(outfile)split_pdf_file.append(out_file_name)split_pdf_file_name.append(out_new_file)return split_pdf_file, split_pdf_file_name

结果如下:
在这里插入图片描述

PDF左右分页


import pdfplumber
from PyPDF2 import PdfFileReader, PdfFileWriter
import os
def split_pdf(infile, out_path):if not os.path.exists(out_path):os.makedirs(out_path)with open(infile, 'rb') as infile:reader = PdfFileReader(infile)number_of_pages = reader.getNumPages()print("共{}页".format(number_of_pages))for i in range(number_of_pages):writer = PdfFileWriter()writer.addPage(reader.getPage(i))out_file_name = out_path + str(i + 1) + '.pdf'with open(out_file_name, 'wb', ) as outfile:writer.write(outfile)def PdfSplitpath(new_filepath):isExists = os.path.exists(new_filepath)if not isExists:os.makedirs(new_filepath)print("----------目录创建成功--------")else:print("---------目录已经存在----------")def SplitPDFLeft(inpath, outpath):inpath_new = os.listdir(inpath)for j in inpath_new:inpath1 = inpath + jwith open(inpath1, "rb") as in_f:input1 = PdfFileReader(in_f)output = PdfFileWriter()numPages = input1.getNumPages()for i in range(numPages):page = input1.getPage(i)page.cropBox.lowerLeft = (10, 45) page.cropBox.upperRight = (600, 841.89)output.addPage(page)with open(('%s/%s.pdf' % (outpath, j[:len(j) - 4] + '_lift')), "wb") as out_f:print("已写入第{}个pdf_lift".format(j[:len(j) - 4]))output.write(out_f)def SplitPDFRight(inpath, outpath):inpath_new = os.listdir(inpath)for j in inpath_new:inpath1 = inpath + jwith open(inpath1, "rb") as in_f:input1 = PdfFileReader(in_f)output = PdfFileWriter()numPages = input1.getNumPages()for i in range(numPages):page = input1.getPage(i)page.height = (791.89)page.width = (562.2)page.cropBox.upperRight = (600, 841.89) page.cropBox.lowerLeft = (1162.2, 50) output.addPage(page)with open(('%s/%s.pdf' % (outpath, j[:len(j) - 4] + '_right')), "wb") as out_f:print("已写入第{}个pdf_right".format(j[:len(j) - 4]))output.write(out_f)if __name__ == '__main__':in_File = './data/越南协会组织与NGO组织目录.pdf'out_Path = './data/单页/'  # 生成输出文件夹split_pdf(in_File, out_Path)new_filepath = './data/分页'PdfSplitpath(new_filepath)inpath_new = os.listdir(out_Path)print(inpath_new)print(out_Path + inpath_new[3])print((inpath_new[3])[:len(inpath_new[3]) - 4])SplitPDFRight(out_Path, new_filepath)SplitPDFLeft(out_Path, new_filepath)

结果如下

单页PDF

在这里插入图片描述

左单页PDF

在这里插入图片描述

右单页PDF

在这里插入图片描述

相关文章:

  • 千益畅行,共享旅游卡,满足您多样化的同行出行需求
  • Web考试前端等级:深度剖析与实战攻略
  • 搭建python虚拟环境,并在VSCode中使用
  • 让你的TypeScript代码更优雅,这10个特性你需要了解下
  • htb-linux-9-sense
  • Web安全:Web体系架构存在的安全问题和解决方案
  • Debian13将正式切换到基于内存的临时文件系统
  • 中电金信:产教联合共育人才 AFAC2024金融智能创新大赛启动
  • k8s之deployments相关操作
  • 【图 - 遍历(BFS DFS)】深度优先搜索算法(Depth First Search), 广度优先搜索算法(Breadth First Search)
  • (佳作)两轮平衡小车(原理图、PCB、程序源码、BOM等)
  • 安装ps提示缺少dll文件是怎么回事,哪种解决方法更推荐
  • 【计算机视觉】数字图像处理基础:以像素为单位的图像基本运算(点运算、代数运算、逻辑运算、几何运算、插值)
  • Spring Boot整合WebSocket和Redis实现直播间在线人数统计功能
  • 检测五个数是否一样的算法
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • [笔记] php常见简单功能及函数
  • CEF与代理
  • GraphQL学习过程应该是这样的
  • java8 Stream Pipelines 浅析
  • js对象的深浅拷贝
  • Map集合、散列表、红黑树介绍
  • Mocha测试初探
  • overflow: hidden IE7无效
  • Redis 懒删除(lazy free)简史
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • 对JS继承的一点思考
  • 关于Flux,Vuex,Redux的思考
  • 前端存储 - localStorage
  • 试着探索高并发下的系统架构面貌
  • 探索 JS 中的模块化
  • 微服务核心架构梳理
  • ionic异常记录
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • 数据库巡检项
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​LeetCode解法汇总518. 零钱兑换 II
  • #git 撤消对文件的更改
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (13):Silverlight 2 数据与通信之WebRequest
  • (31)对象的克隆
  • (MATLAB)第五章-矩阵运算
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (含笔试题)深度解析数据在内存中的存储
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • (转)shell调试方法
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .form文件_一篇文章学会文件上传
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈