前段时间买了一个kindle 电子书阅读器、我想用它来读的pdf文档、当然最主要是用来读python标准库&mysql的官方文档。

问题就来了、这两个都是大头书、之前用mac看还好、用kindle就真的不方便了;主要是kindle对pdf的支持不太好、不能

目录导航;于是我就想把大的pdf文件按章节分解成小的pdf文件

一、安装PyPDF2这个python包

pip3 install PyPDF2

二、从源pdf文件中抽取页面

#/usr/local/python/bin/python3

from PyPDF2 import PdfFileReader,PdfFileWriter
"""
抽取pdf页面
""" if __name__=="__main__":
reader=PdfFileReader('/Users/jianglexing/Documents/linux/python/python-3.6/library.pdf')
writer=PdfFileWriter()
#开始的页面号
start=108
#结束的页面号
stop=126
with open('/Users/jianglexing/Documents/python-std-re.pdf','wb') as wstream:
for page in range(start,stop):
temp=reader.getPage(page)
writer.addPage(temp)
writer.write(wstream)
print("对抽取完成了")

三、功能我们已经实现了、但是还太友好、下面对代码进行改进

#/usr/local/python/bin/python3

from PyPDF2 import PdfFileReader,PdfFileWriter
import argparse """
抽取pdf页面
""" if __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument('--source-file',default=r'/Users/jianglexing/Documents/linux/python/python-3.6/library.pdf',help='源文件全路径')
parser.add_argument('--target-file',default=r'/tmp/target.pdf',help='目标路径的全路径')
parser.add_argument('--start-page',default=,type=int,help='开始的页号')
parser.add_argument('--stop-page',default=,type=int,help='结束的页号')
args=parser.parse_args()
reader=PdfFileReader(args.source_file)
writer=PdfFileWriter()
with open(args.target_file,'wb') as wstream:
for page in range(args.start_page,args.stop_page):
temp=reader.getPage(page)
writer.addPage(temp)
writer.write(wstream)
print("对抽取完成了")

四、还有一些没有解决的问题、如果源文件太大的话会报错、由于还没有看PyPDF2的源码、所以目前还不知道怎么解决

JianglexingdeMacBook-Pro:Desktop jianglexing$ python3 splitpdf.py --source-file='/Users/jianglexing/Desktop/refman-5.7.18-en.a4.pdf' --target-file=/Users/jianglexing/Desktop/temp.pdf --start-page= --stop-page=
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/generic.py", line , in __new__
return decimal.Decimal.__new__(cls, utils.str_(value), context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/utils.py", line , in str_
if sys.version_info[] < :
RecursionError: maximum recursion depth exceeded in comparison During handling of the above exception, another exception occurred: Traceback (most recent call last):
File "splitpdf.py", line , in <module>
writer.write(wstream)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/pdf.py", line , in write
self._sweepIndirectReferences(externalReferenceMap, self._root)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/pdf.py", line , in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/pdf.py", line , in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/pdf.py", line , in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/pdf.py", line , in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/pdf.py", line , in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, data[i])
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PyPDF2/pdf.py", line , in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)

----

学习交流

从pdf 文件中抽取特定的页面的更多相关文章

  1. 怎样在PDF文件中查找某个特定的词?

    不得不说中国的修饰词太多了例如:“滚”可以这样说,请你以一种圆润的方式离开:上次小编在路上听到某男子打电话,好像是给女孩子,那口才,是真的牛,夸人不带重复的.要不是我男孩子,我都想以身相许了.人们常常 ...

  2. 另类爬虫:从PDF文件中爬取表格数据

    简介   本文将展示一个稍微不一样点的爬虫.   以往我们的爬虫都是从网络上爬取数据,因为网页一般用HTML,CSS,JavaScript代码写成,因此,有大量成熟的技术来爬取网页中的各种数据.这次, ...

  3. 怎样把PDF文件中的一页提取出来

    现在随着网络科技的发展在网上找资源找文件就像家常便饭一样,但是有很多文件下载完成之后只有几页是需要的这时候就很困惑了,这么多怎么才能看完啊.这样为了不浪费时间可以将有用的一页提取出来,进行使用,那怎样 ...

  4. java 用PDFBox 删除 PDF文件中的某一页

    依赖: <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-app ...

  5. 使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载

    使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载 2018年06月07日 10:42:26 守望dfdfdf 阅读数:235 标签: jav ...

  6. 读取同一文件夹下多个txt文件中的特定内容并做统计

    读取同一文件夹下多个txt文件中的特定内容并做统计 有网友在问,C#读取同一文件夹下多个txt文件中的特定内容,并把各个文本的数据做统计. 昨晚Insus.NET抽上些少时间,来实现此问题,加强自身的 ...

  7. 借助python工具从word文件中抽取相关表的定义,最后组装建表语句-非常好

    借助python工具从word文件中抽取表的定义,最后组装建表语句-非常好 --如有转载请以超链接的方式注明原文章出处,谢谢大家.请尊重每一位乐于分享的原创者 1.python脚本 ## -*- co ...

  8. 怎样批量删除PDF文件中的注释

    日常我们在阅读一些PDF文章时候,我们会发现有些PDF文章带有非常多的注释,显得非常不美观,影响了阅读体验.那么PDF文章里的批注应该怎么进行删除呢?怎样批量删除PDF文件中的注释?   操作教程: ...

  9. 使用Python从PDF文件中提取数据

    前言 数据是数据科学中任何分析的关键,大多数分析中最常用的数据集类型是存储在逗号分隔值(csv)表中的干净数据.然而,由于可移植文档格式(pdf)文件是最常用的文件格式之一,因此每个数据科学家都应该了 ...

随机推荐

  1. android开发:退出程序(对话框、两次返回键退出)

    private void exitDialog() { AlertDialog.Builder aa=new AlertDialog.Builder(this); aa.setTitle(" ...

  2. .NET:用T4消除代码重复,对了,也错了

    背景 我需要为int.long.float等这些数值类型写一些扩展方法,但是我发现他们不是一个继承体系,我的第一个思维就是需要为每个类型重复写一遍扩展方法,这让我觉得非常不爽,但是我还是不情愿的写了, ...

  3. OpenCV特征点检测

    特征点检测 目标 在本教程中,我们将涉及: 使用 FeatureDetector 接口来发现感兴趣点.特别地: 使用 SurfFeatureDetector 以及它的函数 detect 来实现检测过程 ...

  4. js轮播功能 标签自动切换 同页面多轮播js

    需要加入jquery 1.43及以上版本 下面还有个简单版,一个页面只支持一个轮播 同页面多轮播js <div> <div class="yt_content"& ...

  5. SQL IN

    here are some additional clause in the SQL language that can be used to simplify queries by decrease ...

  6. http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html

    http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html http://www.linuxeden.com/html/softuse ...

  7. NotePad++ 显示字符

    转: http://shouce.jb51.net/notepad_book/npp_func_show_special_char.html

  8. android AIDL 入门讲解非常好的文章(网页代码着色给力)

    转自: http://tech.cnnetsec.com/585.html

  9. [AngularJS] AngularJS 1.3 $scope.$watchGroup

    $watchGroup can monitor an array or expression. We watch both email and password by using the same c ...

  10. 如何使用angularjs实现抓取页面内容

    <html ng-app="myApp"> <head> <title>angularjs-ajax</title> <scr ...