- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
因此,我试图从一些 PDF 中获取特定的文本位,并且我将 Python 与 PDFMiner 一起使用,但由于 API 发生了一些变化,在 November 2013 中发生了一些问题。 .基本上,要从 PDF 中获取我想要的文本部分,我目前必须将 整个 文件转换为文本,然后使用字符串函数来获取我想要的部分。我想要做的是遍历 PDF 的每一页,并将每一页一页一页地转换为文本。然后,一旦找到我想要的部分,我就会阻止它阅读该 PDF。
我将发布我的文本编辑器 atm 中的代码,但它不是工作版本,它更像是半途而废的有效解决方案版本:P
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
from pdfminer.converter import LTChar, TextConverter
from pdfminer.layout import LAParams
from subprocess import call
from cStringIO import StringIO
import re
import sys
import os
argNum = len(sys.argv)
pdfLoc = str(sys.argv[1]) #CLI arguments
def convert_pdf_to_txt(path): #converts pdf to raw text (not my function)
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = file(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos=set()
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
interpreter.process_page(page)
fp.close()
device.close()
str = retstr.getvalue()
retstr.close()
return str
if (pdfLoc[-4:] == ".pdf"):
contents = ""
try: # Get the outlines (contents) of the document
fp = open(pdfLoc, 'rb') #open a pdf document for reading
parser = PDFParser(fp)
document = PDFDocument(parser)
outlines = document.get_outlines()
for (level,title,dest,a,se) in outlines:
title = re.sub(r".*\s", "", title) #get raw titles, stripped of formatting
contents += title + "\n"
except: #if pdfMiner can't get contents then manually get contents from text conversion
#contents = convert_pdf_to_txt(pdfLoc)
#startToCpos = contents.find("TABLE OF CONTENTS")
#endToCpos = contents.rfind(". . .")
#contents = contents[startToCpos:endToCpos+8]
fp = open(pdfLoc, 'rb') #open a pdf document for reading
parser = PDFParser(fp)
document = PDFDocument(parser)
pages = PDFPage(document, 3, {'Resources':'thing', 'MediaBox':'Thing'}) #God knows what's going on here
for pageNumber, page in enumerate(pages.get_pages(PDFDocument, fp)): #The hell is the first argument?
if pageNumber == 42:
print "Hello"
#for line in s:
# print line
# if (re.search("(\.\s){2,}", line) and not re.search("NOTES|SCOPE", line)):
# line = re.sub("(\.\s){2,}", "", line)
# line = re.sub("(\s?)*[0-9]*\n", "\n", line)
# line = re.sub("^\s", "", line)
# print line,
#contents = contents.lower()
#contents = re.sub("“", "\"", contents)
#contents = re.sub("”", "\"", contents)
#contents = re.sub("fi", "f", contents)
#contents = re.sub(r"(TABLE OF CONTENTS|LIST OF TABLES|SCOPE|REFERENCED DOCUMENTS|Identification|System (o|O)verview|Document (o|O)verview|Title|Page|Table|Tab)(\n)?|\.\s?|Section|[0-9]", "", contents)
#contents = re.sub(r"This document contains proprietary information and may not be reproduced in any form whatsoever, nor may be used by or its contents divulged to third\nparties without written permission from the ownerAll rights reservedNumber: STP SMEDate: -Jul-Issue: A of CMC STPNHIndustriesCLASSIFICATION\nNATO UNCLASSIFIED AGUSTAEUROCOPTEREUROCOPTER DEUTSCHLAND FOKKER", "", contents)
#contents = re.sub(r"(\r?\n){2,}", "", contents)
#contents = contents.lstrip()
#contents = contents.rstrip()
#print contents
else:
print "Not a valid PDF file"
This is the old way of doing it (或者至少知道旧方法是如何做到的,这个线程对我来说不是很有用 tbh)。但现在我必须使用 PDFPage.get_pages 而不是 PDFDocument.get_pages 并且方法和参数完全不同。
目前,我正在尝试弄清楚我传递给 PDFPage 的 get_pages 方法的“Klass”变量到底是什么? .
如果有人可以阐明 API 的这一部分,甚至提供一个工作示例,我将非常感激。
最佳答案
尝试使用 PyPDF2 .它使用起来要简单得多,而且不像 PDFMiner 那样具有不必要的功能(这对您来说很好)。这就是您想要的,而且实现起来 super 简单。
from PyPDF2 import PdfFileReader
PDF = PdfFileReader(file(pdf_fp, 'rb'))
if PDF.isEncrypted:
decrypt = PDF.decrypt('')
if decrypt == 0:
print "Password Protected PDF: " + pdf_fp
raise Exception("Nope")
elif decrypt == 1 or decrypt == 2:
print "Successfully Decrypted PDF"
for page in PDF.pages:
print page.extractText()
'''page.extractText() is the unicode string of the contents of the page
And I am assuming you know how to play with a string and use regex
If you find what you want just break like so:
if some_condition == True:
break'''
关于python - PDFMiner - 遍历页面并将它们转换为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21113773/
假设我有父页面。 当我单击“打印”时,会打开一个新选项卡(页面)以进行打印预览。 我想要的是,当我单击“打印”时,父选项卡(页面)上应显示覆盖层,以停止进一步的工作,直到子选项卡关闭。 Because
我知道这是不可能的,但我想我还是会问这个问题。 我有一个 HTTPS 页面,并且正在动态创建一个表单。我想将表单发布到 HTTP 页面。在浏览器不弹出警告的情况下这可能吗?当我在 IE8 上执行此操作
在我的应用程序中,我实现了ajax 4.0客户端模板 目前我的模板位于同一个.aspx页面上。 (例如 Main.aspx) 但我想将它们外部化。(即所有 HTML 都会放在另一个页面上) 为此,我使
我目前正在构建自己的网络应用程序。在此应用程序的标题中有一个导航栏。 我将相同的导航栏添加到我的所有 html 页面,以便在页面之间导航时保持可见,并将导航元素设置为事件到我现在所在的页面。 我的
我们有一个在集成管道下运行的 IIS7 Intranet 站点,该管道主要是 ASP.Net,带有一些传统的经典 ASP 页面。该站点允许匿名访问大多数区域,但使用 Windows 身份验证来保护某些
有一个要求,我需要根据 div 标签的高度超过 700 像素的条件将大型 HTML 页面拆分为多个页面。 我还可以根据字数拆分页面。 下面的示例根据 HTML 标签分割页面,而我需要根据 div 高度
有两个 html 页面,如果在第一个页面中单击特定图像,它将转到第二个 html 页面。如果在第二个 html 页面上的任何地方单击它应该返回到第一页并将第二页完全显示在第一页的一个 div 中 1s
我有一个简单的 hello world Servant 应用程序。我需要向它添加一些静态或动态的 html 页面。我怎样才能做到这一点?在文档中没有提到它。注意我不想在 Haskell 代码中创建 h
我有两个文件 index.html 和 index2.html。这两个文件都在本地机器上的同一目录中(无法访问 PHP 等)。 我正在努力 Page Title 使用 jQuery 从 index.h
假设我有一个 JSP 页面 index.jsp User id:
我面临着一个对我来说非常陌生的情况。我会尽力让事情变得简单来解释它一步一步来。 1)在页面 A(用户表单)中,我从字段中获取一些值并将其作为 jQuery AJAX POST 发送到页面 B。这是主要
我正在尝试弄清楚如何从 buttons.html 获取用户按钮点击动态切换第二个列的可见性 userinput.html 。我正在使用 DataTables userinput.html表并认为我有
我有asp。净 mvc4 项目。 Angularjs 已集成。 我已经按照以前的要求构建了 HTML 页面和 WEB API 2。 现在出于某种原因,我必须使用 CSHTML 页面。以前我只有 web
我的网站包含大约 18 个不同的页面。以管理员身份登录后,我单击“页面”。 我看到了以下内容 All (20) | Mine (19) | Published (19) | Draft (1) No
Haskell 中每个函数都有手册页吗?换句话说,我可以在某处输入 man fst 并获取有关该函数的手册或帮助页面吗?我发现大量的功能势不可挡! 我正在使用 GHC 和 GHCi。 最佳答案 我不知
我一直在用github pages很长一段时间以来,并使用自定义域重定向功能来重定向我的 user website来自 prahladyeri.github.io到我自己的域名 https://www
我对 Github 有一个基本的了解:我知道如何创建、添加、提交、推送和克隆存储库。我也开始探索 Github 页面来托管我的项目。我的最新项目于 3 月开始并将其推送到 gh-page。从那以后,我
在尝试查看日志时,Airflow 会抛出一个带有以下消息的 oops 页面: File "/Users/user/.pyenv/versions/3.5.2/lib/python3.5/locale.
我已经按照此处列出的步骤 (https://pages.github.com/) 为我正在处理的网站设置了一个 GitHub 页面 我设置的站点当前托管在 IIS 下,并使用 URL Rewrite
我想将我的 domain.com 重定向到 openshift 页面 (openshift-app.rhcloud.com)。我已经按照描述完成了 here它有效。唯一的问题是,在任何网站上,我都不会
我是一名优秀的程序员,十分优秀!