搜索
首页后端开发Python教程利用python将pdf输出为txt的实例

利用python将pdf输出为txt的实例

Apr 23, 2018 pm 03:16 PM
python实例输出

下面为大家分享一篇利用python将pdf输出为txt的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧

一个礼拜前一个同学问我这个事情,由于之前在参加华为的比赛,所以赛后看了一下,据说需要用到pdfminer这个包。于是安装了一下,安装过程很简单:

sudo pip install pdfminer;

中间也没有任何的报错。至于如何调用,本人也没有很好的研究过pdfminer这个库,于是开始了百度……

官方文档:http://www.unixuser.org/~euske/python/pdfminer/index.html

完全使用python编写。 (适用于2.4或更新版本)

解析,分析,并转换成PDF文档。

PDF-1.7规范的支持。 (几乎)

中日韩语言和垂直书写脚本支持。

各种字体类型(Type1、TrueType、Type3,和CID)的支持。

基本加密(RC4)的支持。

PDF与HTML转换。

纲要(TOC)的提取。

标签内容提取。

通过分组文本块重建原始的布局。

一些基本的类

PDFParser:从一个文件中获取数据

PDFDocument:保存获取的数据,和PDFParser是相互关联的

PDFPageInterpreter处理页面内容

PDFDevice将其翻译成你需要的格式

PDFResourceManager用于存储共享资源,如字体或图像。

简单的实现

读取test.pdf输出为output.txt:

# -*- coding: utf-8 -*-  
from pdfminer.pdfparser import PDFParser 
from pdfminer.pdfdocument import PDFDocument 
from pdfminer.pdfpage import PDFPage 
from pdfminer.pdfpage import PDFTextExtractionNotAllowed 
from pdfminer.pdfinterp import PDFResourceManager 
from pdfminer.pdfinterp import PDFPageInterpreter 
from pdfminer.pdfdevice import PDFDevice 
from pdfminer.layout import * 
from pdfminer.converter import PDFPageAggregator 
import os 
fp = open('test.pdf', 'rb') 
#来创建一个pdf文档分析器 
parser = PDFParser(fp) 
#创建一个PDF文档对象存储文档结构 
document = PDFDocument(parser) 
# 检查文件是否允许文本提取 
if not document.is_extractable: 
 raise PDFTextExtractionNotAllowed 
else: 
 # 创建一个PDF资源管理器对象来存储共赏资源 
 rsrcmgr=PDFResourceManager() 
 # 设定参数进行分析 
 laparams=LAParams() 
 # 创建一个PDF设备对象 
 # device=PDFDevice(rsrcmgr) 
 device=PDFPageAggregator(rsrcmgr,laparams=laparams) 
 # 创建一个PDF解释器对象 
 interpreter=PDFPageInterpreter(rsrcmgr,device) 
 # 处理每一页 
 for page in PDFPage.create_pages(document): 
  interpreter.process_page(page) 
  # 接受该页面的LTPage对象 
  layout=device.get_result() 
  for x in layout: 
   if(isinstance(x,LTTextBoxHorizontal)): 
    with open('output.txt','a') as f: 
     f.write(x.get_text().encode('utf-8')+'\n')

相关推荐:

Python 将pdf转成图片的方法


以上是利用python将pdf输出为txt的实例的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解释吗?揭穿神话Python:它是真正的解释吗?揭穿神话May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

您可以使用Python中的循环加入列表吗?您可以使用Python中的循环加入列表吗?May 10, 2025 am 12:14 AM

是的,YouCanconCatenatElistsusingAloopInpyThon.1)使用eparateLoopsForeachListToAppendIteMstoaresultList.2)useanestedlooptoiterateOverMultipliplipliplipliplipliplipliplipliplipliplistforamoreConciseApprace.3)

condenate列表python:使用,扩展()等condenate列表python:使用,扩展()等May 10, 2025 am 12:12 AM

ThemostefficientmethodsforconcatenatinglistsinPythonare:1)theextend()methodforin-placemodification,2)itertools.chain()formemoryefficiencywithlargedatasets.Theextend()methodmodifiestheoriginallist,makingitmemory-efficientbutrequirescautionifpreserving

Python循环:示例和最佳实践Python循环:示例和最佳实践May 10, 2025 am 12:05 AM

pythonboopsincludeforandwhileloops,with forloopsidealforequencessand and whileloopsforcondition repetition.bestpracticesinvolve:1)使用listComprehensionsforshensionsforsimpletranspletransformations,2)obseringEnumerateForIndex-valuepairs,3)optingftingftingfortermornemoremoremoremore

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境