我想要一个python函数,它接受pdf并返回文档中注释注释的文本列表.我看过python-poppler(https://code.launchpad.net/~poppler-python/poppler-python/trunk),但我无法弄清楚如何让它给我任何有用的东西.
我找到了get_annot_mapping
方法并修改了提供的演示程序来调用它self.current_page.get_annot_mapping()
,但我不知道如何处理AnnotMapping对象.它似乎没有完全实现,只提供复制方法.
如果有任何其他库提供此功能,那也没关系.
Enn*_*per 19
以防有人正在寻找一些工作代码.这是我使用的脚本.
import poppler
import sys
import urllib
import os
def main():
input_filename = sys.argv[1]
# http://blog.hartwork.org/?p=612
document = poppler.document_new_from_file('file://%s' % \
urllib.pathname2url(os.path.abspath(input_filename)), None)
n_pages = document.get_n_pages()
all_annots = 0
for i in range(n_pages):
page = document.get_page(i)
annot_mappings = page.get_annot_mapping ()
num_annots = len(annot_mappings)
if num_annots > 0:
for annot_mapping in annot_mappings:
if annot_mapping.annot.get_annot_type().value_name != 'POPPLER_ANNOT_LINK':
all_annots += 1
print 'page: {0:3}, {1:10}, type: {2:10}, content: {3}'.format(i+1, annot_mapping.annot.get_modified(), annot_mapping.annot.get_annot_type().value_nick, annot_mapping.annot.get_contents())
if all_annots > 0:
print str(all_annots) + " annotation(s) found"
else:
print "no annotations found"
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
joe*_*lom 12
pdf -annots脚本可以从 PDF 中提取注释。它基于PDFMineer.6构建,并以 Markdown 格式为突出显示的文本及其上所做的任何注释生成输出,例如突出显示区域或弹出框上的注释。输出看起来类似于:
* Page 2 Highlight:
> Underlying text that was highlighted
Comment made on highlighted text.
* Page 3 Highlight: "Short highlighted text" -- Short comment.
* Page 4 Text: A note on the page.
Run Code Online (Sandbox Code Playgroud)
完整的命令选项如下所示。
usage: pdfannots.py [-h] [-p] [-o OUTFILE] [-n COLS] [-s [SEC [SEC ...]]] [--no-group]
[--print-filename] [-w COLS]
INFILE [INFILE ...]
Extracts annotations from a PDF file in markdown format for use in reviewing.
positional arguments:
INFILE PDF files to process
optional arguments:
-h, --help show this help message and exit
Basic options:
-p, --progress emit progress information
-o OUTFILE output file (default is stdout)
-n COLS, --cols COLS number of columns per page in the document (default: 2)
Options controlling output format:
-s [SEC [SEC ...]], --sections [SEC [SEC ...]]
sections to emit (default: highlights, comments, nits)
--no-group emit annotations in order, don't group into sections
--print-filename print the filename when it has annotations
-w COLS, --wrap COLS wrap text at this many output columns
Run Code Online (Sandbox Code Playgroud)
我还没有广泛尝试过,但到目前为止效果很好!
您应该一定要看一下PyPDF2
。这个惊人的库具有无限的潜力,您可以从PDF中提取任何内容,包括图像或注释。尝试从检查Acrobat Reader DC(阅读器)可以为您提供PDF注释开始。制作一个简单的PDF,使用Reader对其进行批注(添加一些注释),然后在右上角的注释选项卡中,单击水平的三个点,然后单击Export All To Data File...
并选择带有扩展名的格式xfdf
。这将创建一个精彩的xml文件,您可以对其进行解析。该格式非常透明且不言而喻。
但是,如果您不能依靠用户单击它,而是需要使用python以编程方式从PDF提取相同的数据,请不要绝望,这是一种解决方案。(灵感来自于在不重新采样的情况下以python格式提取PDF图片?)
先决条件:
PyPDF2(pip install PyPDF2
)
Reader在上述xfdf文件中为您提供的内容如下所示:
<?xml version="1.0" ?>
<xfdf xml:space="preserve" xmlns="http://ns.adobe.com/xfdf/">
<annots>
<caret IT="Replace" color="#0000FF" creationdate="D:20190221151519+01'00'" date="D:20190221151526+01'00'" flags="print" fringe="1.069520,1.069520,1.069520,1.069520" name="72f8d1b7-d878-4281-bd33-3a6fb4578673" page="0" rect="636.942000,476.891000,652.693000,489.725000" subject="Inserted Text" title="Admin">
<contents-richtext>
<body xfa:APIVersion="Acrobat:19.10.0" xfa:spec="2.0.2" xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<p dir="ltr">
<span dir="ltr" style="font-size:10.5pt;text-align:left;color:#000000;font-weight:normal;font-style:normal"> comment1</span>
</p>
</body>
</contents-richtext>
<popup flags="print,nozoom,norotate" open="no" page="0" rect="737.008000,374.656000,941.008000,488.656000"/>
</caret>
<highlight color="#FFD100" coords="183.867000,402.332000,220.968000,402.332000,183.867000,387.587000,220.968000,387.587000" creationdate="D:20190221151441+01'00'" date="D:20190221151448+01'00'" flags="print" name="a18c7fb0-0af3-435e-8c32-1af2af3c46ea" opacity="0.399994" page="0" rect="179.930000,387.126000,224.904000,402.793000" subject="Highlight" title="Admin">
<contents-richtext>
<body xfa:APIVersion="Acrobat:19.10.0" xfa:spec="2.0.2" xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<p dir="ltr">
<span dir="ltr" style="font-size:10.5pt;text-align:left;color:#000000;font-weight:normal;font-style:normal">comment2</span>
</p>
</body>
</contents-richtext>
<popup flags="print,nozoom,norotate" open="no" page="0" rect="737.008000,288.332000,941.008000,402.332000"/>
</highlight>
<caret color="#0000FF" creationdate="D:20190221151452+01'00'" date="D:20190221151452+01'00'" flags="print" fringe="0.828156,0.828156,0.828156,0.828156" name="6bf0226e-a3fb-49bf-bc89-05bb671e1627" page="0" rect="285.877000,372.978000,298.073000,382.916000" subject="Inserted Text" title="Admin">
<popup flags="print,nozoom,norotate" open="no" page="0" rect="737.008000,268.088000,941.008000,382.088000"/>
</caret>
<strikeout IT="StrikeOutTextEdit" color="#0000FF" coords="588.088000,497.406000,644.818000,497.406000,588.088000,477.960000,644.818000,477.960000" creationdate="D:20190221151519+01'00'" date="D:20190221151519+01'00'" flags="print" inreplyto="72f8d1b7-d878-4281-bd33-3a6fb4578673" name="6686b852-3924-4252-af21-c1b10390841f" page="0" rect="582.290000,476.745000,650.616000,498.621000" replyType="group" subject="Cross-Out" title="Admin">
<popup flags="print,nozoom,norotate" open="no" page="0" rect="737.008000,383.406000,941.008000,497.406000"/>
</strikeout>
</annots>
<f href="p1.pdf"/>
<ids modified="ABB10FA107DAAA47822FB5D311112349" original="474F087D87E7E544F6DEB9E0A93ADFB2"/>
</xfdf>
Run Code Online (Sandbox Code Playgroud)
各种类型的注释在此处以<annots>
块内的标记形式呈现。Python可以为您提供几乎相同的数据。要获得它,请看以下脚本的输出给出了什么:
import sys
import PyPDF2, traceback
try :
src = sys.argv[1]
except :
src = r'/path/to/my/file.pdf'
input1 = PyPDF2.PdfFileReader(open(src, "rb"))
nPages = input1.getNumPages()
for i in range(nPages) :
page0 = input1.getPage(i)
try :
for annot in page0['/Annots'] :
print annot.getObject() # (1)
print ''
except :
# there are no annotations on this page
pass
Run Code Online (Sandbox Code Playgroud)
与上面的xfdf文件相同的文件的输出将如下所示:
{'/Popup': IndirectObject(192, 0), '/M': u"D:20190221151448+01'00'", '/CreationDate': u"D:20190221151441+01'00'", '/NM': u'a18c7fb0-0af3-435e-8c32-1af2af3c46ea', '/F': 4, '/C': [1, 0.81961, 0], '/Rect': [179.93, 387.126, 224.904, 402.793], '/Type': '/Annot', '/T': u'Admin', '/RC': u'<?xml version="1.0"?><body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:APIVersion="Acrobat:19.10.0" xfa:spec="2.0.2" ><p dir="ltr"><span dir="ltr" style="font-size:10.5pt;text-align:left;color:#000000;font-weight:normal;font-style:normal">comment2</span></p></body>', '/P': IndirectObject(5, 0), '/Contents': u'otrasneho', '/QuadPoints': [183.867, 402.332, 220.968, 402.332, 183.867, 387.587, 220.968, 387.587], '/Subj': u'Highlight', '/CA': 0.39999, '/AP': {'/N': IndirectObject(202, 0)}, '/Subtype': '/Highlight'}
{'/Parent': IndirectObject(191, 0), '/Rect': [737.008, 288.332, 941.008, 402.332], '/Type': '/Annot', '/F': 28, '/Open': <PyPDF2.generic.BooleanObject object at 0x02A425D0>, '/Subtype': '/Popup'}
{'/Popup': IndirectObject(194, 0), '/M': u"D:20190221151452+01'00'", '/CreationDate': u"D:20190221151452+01'00'", '/NM': u'6bf0226e-a3fb-49bf-bc89-05bb671e1627', '/F': 4, '/C': [0, 0, 1], '/Subj': u'Inserted Text', '/Rect': [285.877, 372.978, 298.073, 382.916], '/Type': '/Annot', '/P': IndirectObject(5, 0), '/AP': {'/N': IndirectObject(201, 0)}, '/RD': [0.82816, 0.82816, 0.82816, 0.82816], '/T': u'Admin', '/Subtype': '/Caret'}
{'/Parent': IndirectObject(193, 0), '/Rect': [737.008, 268.088, 941.008, 382.088], '/Type': '/Annot', '/F': 28, '/Open': <PyPDF2.generic.BooleanObject object at 0x02A42830>, '/Subtype': '/Popup'}
{'/Popup': IndirectObject(196, 0), '/M': u"D:20190221151519+01'00'", '/CreationDate': u"D:20190221151519+01'00'", '/NM': u'6686b852-3924-4252-af21-c1b10390841f', '/F': 4, '/IRT': IndirectObject(197, 0), '/C': [0, 0, 1], '/Rect': [582.29, 476.745, 650.616, 498.621], '/Type': '/Annot', '/T': u'Admin', '/P': IndirectObject(5, 0), '/QuadPoints': [588.088, 497.406, 644.818, 497.406, 588.088, 477.96, 644.818, 477.96], '/Subj': u'Cross-Out', '/IT': '/StrikeOutTextEdit', '/AP': {'/N': IndirectObject(200, 0)}, '/RT': '/Group', '/Subtype': '/StrikeOut'}
{'/Parent': IndirectObject(195, 0), '/Rect': [737.008, 383.406, 941.008, 497.406], '/Type': '/Annot', '/F': 28, '/Open': <PyPDF2.generic.BooleanObject object at 0x02A42AF0>, '/Subtype': '/Popup'}
{'/Popup': IndirectObject(198, 0), '/M': u"D:20190221151526+01'00'", '/CreationDate': u"D:20190221151519+01'00'", '/NM': u'72f8d1b7-d878-4281-bd33-3a6fb4578673', '/F': 4, '/C': [0, 0, 1], '/Rect': [636.942, 476.891, 652.693, 489.725], '/Type': '/Annot', '/RD': [1.06952, 1.06952, 1.06952, 1.06952], '/T': u'Admin', '/RC': u'<?xml version="1.0"?><body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:APIVersion="Acrobat:19.10.0" xfa:spec="2.0.2" ><p dir="ltr"><span dir="ltr" style="font-size:10.5pt;text-align:left;color:#000000;font-weight:normal;font-style:normal">comment1</span></p></body>', '/P': IndirectObject(5, 0), '/Contents': u' pica', '/Subj': u'Inserted Text', '/IT': '/Replace', '/AP': {'/N': IndirectObject(212, 0)}, '/Subtype': '/Caret'}
{'/Parent': IndirectObject(197, 0), '/Rect': [737.008, 374.656, 941.008, 488.656], '/Type': '/Annot', '/F': 28, '/Open': <PyPDF2.generic.BooleanObject object at 0x02A42AB0>, '/Subtype': '/Popup'}
Run Code Online (Sandbox Code Playgroud)
如果检查输出,您将意识到输出几乎相同。xfdf文件中的每个注释在python的PyPDF2输出中都有两个对应的注释。该/C
属性是高光的颜色(以RGB为单位),缩放为<0,1>范围内的浮点数。/Rect
定义页面/跨页上的注释的边界框,以相对于页面左下角的磅数(1/72英寸)为单位,向右和向上递增值。/M
和/CreationDate
被修改,创建时间,/QuadPoints
是阵列[x1, y1, x2, y2, ..., xn, yn]
的注释周围的线的坐标,/Subject
,/Type
,/SubType
,/IT
识别的注释的类型,/T
可能是创建者,/RC
是注释文本的xhtml表示形式(如果有)。如果有墨迹注释,则将在此处以具有属性/InkList
的形式呈现,该属性的数据格式[[L1x1, L1y1, L1x2, L1y2, ..., L1xn, L1yn], [L2x1, L2y1, ..., L2xn, L2yn], ..., [Lmx1, Lmy1, ..., Lmxn, Lmyn]]
为第1行,第2行,...,第m行。
有关getObject()
在指定的Python代码(1)行中获得的各个字段的更详尽说明,请参阅https://www.adobe.com/content/dam/acom/zh/devnet/pdf/pdfs/ PDF32000_2008.pdf,尤其是从381-413页开始的12.5注释部分。
这是一个使用 python 模块popplerqt5提取注释的工作示例(从之前的答案移植) :python3 extract.py sample.pdf
提取物.py
import popplerqt5
import argparse
def extract(fn):
doc = popplerqt5.Poppler.Document.load(fn)
annotations = []
for i in range(doc.numPages()):
page = doc.page(i)
for annot in page.annotations():
contents = annot.contents()
if contents:
annotations.append(contents)
print(f'page={i + 1} {contents}')
print(f'{len(annotations)} annotation(s) found')
return annotations
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('fn')
args = parser.parse_args()
extract(args.fn)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12800 次 |
最近记录: |