【问题标题】:Delete text from pdf using PyMUPDF使用 PyMUPDF 从 pdf 中删除文本
【发布时间】:2022-09-26 16:26:25
【问题描述】:

我需要使用 Python 从 pdf 文档中删除文本 \"DRAFT\"。我可以找到包含文本的文本框,但找不到如何使用 pymupdf 编辑 pdf 文本元素的示例。

在下面的示例中,草稿对象包含 DRAFT 文本元素的坐标和文本。

import fitz

fname = r\"original.pdf\"
doc = fitz.open(fname)
page = doc.load_page(0)

draft = page.search_for(\"DRAFT\")

# insert code here to delete the DRAFT text or replace it with an empty string

out_fname = r\"final.pdf\"
doc.save(out_fname)

添加于 2022 年 4 月 28 日 我找到了一种删除文本的方法,但不幸的是,它也会删除 DRAFT 周围框下方的任何重叠文本。我真的只想删除草稿字母而不修改底层

# insert code here to delete the DRAFT text or replace it with an empty string
rl = page.search_for(\"DRAFT\", quads = True)
page.add_redact_annot(rl[0])

page.apply_redactions()

  • 在这种情况下,从 ArcGIS Pro 导出的地图,草稿只是覆盖在其他文本上的水平文本元素。我不确定 anylyser 是什么

标签: python pymupdf


【解决方案1】:

你可以试试这个。

import fitz

doc = fitz.open("xxxx")

for page in doc:
    for xref in page.get_contents():
        stream = doc.xref_stream(xref).replace(b'The string to delete', b'')
        doc.update_stream(xref, stream)

【讨论】:

    猜你喜欢
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多