Python解析txt文件、使用pdfminer解析pdf文件
来源:互联网 发布:种族歧视 知乎 编辑:程序博客网 时间:2024/05/17 22:06
一、读取TXT文档
urlopen()
一个简单的例子:读取https://en.wikipedia.org/robots.txt的内容
from urllib.request import urlopenhtml=urlopen("https://en.wikipedia.org/robots.txt")print(html.read().decode("utf-8"))
二、读取PDF文档
pdfminer3k
1.安装pdfminer模块
pip install pdfminer3k
2.在IDE中进行编码
一个简单的例子:读取本地PDF文件simple1.pdf。这个例子中,simple1.pdf实现加入了项目文件夹下。如果读取网络上的PDF文档,那么注释掉原来的fp=open..改为fp=urlopen...并且 from urllib.request import urlopen
from pdfminer.converter import PDFPageAggregatorfrom pdfminer.layout import LAParamsfrom pdfminer.pdfparser import PDFParser, PDFDocumentfrom pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreterfrom pdfminer.pdfdevice import PDFDevicefrom urllib.request import urlopen#Open a PDF file.#rb是指以二进制读的形式打开#fp=open("simple1.pdf","rb")fp=urlopen("http://www.tencent.com/zh-cn/articles/8003251479983154.pdf")#Create a PDF parser object associated with the file object.parser=PDFParser(fp)#Create a PDF document object that stores the document structuredoc=PDFDocument()#Connect the parser annd document objects.parser.set_document(doc)doc.set_parser(parser)# Supply the password for initialization.# (If no password is set, give an empty string.)doc.initialize("")#创建PDF资源管理器resource=PDFResourceManager()#参数分析器laparam=LAParams()#创建一个聚合器device=PDFPageAggregator(resource,laparams=laparam)#创建PDF页面解释器interpreter=PDFPageInterpreter(resource,device)#使用文档对象从页面读取内容for page in doc.get_pages(): #使用页面解释器来读取 interpreter.process_page(page) #使用聚合器来获取内容 layout=device.get_result() for out in layout: if hasattr(out,"get_text"): print(out.get_text())
阅读全文
0 0
- Python解析txt文件、使用pdfminer解析pdf文件
- Python 3.6 中使用pdfminer解析pdf文件
- Python PDFMiner 解析pdf 文本
- PDFMiner 操作 PDF 文件
- python解析pdf文件
- 利用python库中的pdfminer解析paper pdf文档
- PDF解析模块-PDFMiner开发手册[翻译]
- 使用PDFBox解析pdf文件
- pdf转换成txt文件的操作步骤解析
- java解析txt文件
- 解析TXT文件
- 解析TXT文件2
- 解析txt文件
- java解析txt文件
- 解析.txt文件入库
- Java解析txt文件
- 解析txt文件数据
- python中解析和生成pdf文件
- Android夜间模式的实现方案
- Intellij Idea中运行tomcat 报內存溢出 PermGen space 解决方案
- designing a non-blocking server
- 编译原理过程简述及中间代码优化
- mapping.txt还原混淆代码
- Python解析txt文件、使用pdfminer解析pdf文件
- ConcurrentHashMap 详细分析
- 随想
- HDU 6114 Chess(组合数学)
- C#学习笔记16-结构体和类
- 为eclipse添加背景色
- 面向对象 接口
- java实现有界队列
- iOS 3D Touch超详细入门(附带demo示例代码)