阅读,突出显示,以编程方式保存PDF

我想写一个小脚本(它将运行在无头的Linux服务器上),读取PDF,突出显示与我传递的string数组中的任何内容匹配的文本,然后保存修改过的PDF。 我想我最终会使用像python绑定到poppler的东西,但不幸的是有接近零的文档,我有python旁边的零经验。

如果任何人可以指向我的教程,例子,或一些有用的文件,让我开始这将不胜感激!

你有没有试过看PDFMiner ? 这听起来像是你想要的。

PDFlib具有Python绑定并支持这些操作。 如果您想打开PDF,您将需要使用PDI。 http://www.pdflib.com/products/pdflib-family/pdflib-pdi/和TET。

不幸的是,这是一个商业产品。 过去我在生产中使用过这个库,效果很好。 绑定功能非常强大,而不是Python。 我已经看到一些尝试,使他们更Pythonic: https : //github.com/alexhayes/pythonic-pdflib你会想要使用:open_pdi_document()。

这听起来像你会想要做一些搜索突出显示:

http://www.pdflib.com/tet-cookbook/tet-and-pdflib/highlight-search-terms/

是的,这可能与pdfminer( pip install pdfminer.six )和PyPDF2

首先,找到坐标(例如像这样 )。 然后突出显示它:

 #!/usr/bin/env python """Create sample highlight in a PDF file.""" from PyPDF2 import PdfFileWriter, PdfFileReader from PyPDF2.generic import ( DictionaryObject, NumberObject, FloatObject, NameObject, TextStringObject, ArrayObject ) def create_highlight(x1, y1, x2, y2, meta, color=[0, 1, 0]): """ Create a highlight for a PDF. Parameters ---------- x1, y1 : float bottom left corner x2, y2 : float top right corner meta : dict keys are "author" and "contents" color : iterable Three elements, (r,g,b) """ new_highlight = DictionaryObject() new_highlight.update({ NameObject("/F"): NumberObject(4), NameObject("/Type"): NameObject("/Annot"), NameObject("/Subtype"): NameObject("/Highlight"), NameObject("/T"): TextStringObject(meta["author"]), NameObject("/Contents"): TextStringObject(meta["contents"]), NameObject("/C"): ArrayObject([FloatObject(c) for c in color]), NameObject("/Rect"): ArrayObject([ FloatObject(x1), FloatObject(y1), FloatObject(x2), FloatObject(y2) ]), NameObject("/QuadPoints"): ArrayObject([ FloatObject(x1), FloatObject(y2), FloatObject(x2), FloatObject(y2), FloatObject(x1), FloatObject(y1), FloatObject(x2), FloatObject(y1) ]), }) return new_highlight def add_highlight_to_page(highlight, page, output): """ Add a highlight to a PDF page. Parameters ---------- highlight : Highlight object page : PDF page object output : PdfFileWriter object """ highlight_ref = output._addObject(highlight) if "/Annots" in page: page[NameObject("/Annots")].append(highlight_ref) else: page[NameObject("/Annots")] = ArrayObject([highlight_ref]) def main(): pdf_input = PdfFileReader(open("samples/test3.pdf", "rb")) pdf_output = PdfFileWriter() page1 = pdf_input.getPage(0) highlight = create_highlight(89.9206, 573.1283, 376.849, 591.3563, { "author": "John Doe", "contents": "Lorem ipsum" }) add_highlight_to_page(highlight, page1, pdf_output) pdf_output.addPage(page1) output_stream = open("output.pdf", "wb") pdf_output.write(output_stream) if __name__ == '__main__': main()