在 Python 中编辑 PDF 格式

本机和高性能 PDF 文档敏感密文信息使用服务器端 Aspose.PDF for Python API,无需使用微软或 Adobe PDF 等任何软件。

如何使用 Python 库编辑 PDF 文件

为了编辑 PDF 文件,我们将使用 Aspose.PDF for .NET API,这是一款功能丰富、功能强大且易于使用的适用于 python-net 平台的文档处理 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

通过 Python 编辑 PDF 文档


你需要 Aspose.PDF for .NET 在你的环境中试用代码。

1.使用 “文档” 实例加载 PDF。 1.使用搜索词作为参数创建 TextFragmentAbsorber 对象。 1.设置搜索选项。 1.循环浏览每个片段收集以进行编辑。 1.保存 PDF 文件。

编辑 PDF 文件-Python。


    doc = new Document(dataDir + "test.pdf")

    textFragmentAbsorber = TextFragmentAbsorber(searchTerm)
    textSearchOptions = TextSearchOptions(True)
    textFragmentAbsorber.TextSearchOptions = textSearchOptions

    doc.Pages.Accept(textFragmentAbsorber)
    textFragmentCollection = textFragmentAbsorber.TextFragments
    for textFragment in textFragmentCollection:
        page = textFragment.Page
        annotationRectangle = textFragment.Rectangle
        RedactionAnnotation annot = Annotations.RedactionAnnotation(page, annotationRectangle)
        annot.FillColor = Color.Black
        doc.Pages[textFragment.Page.Number].Annotations.Add(annot, true)
        annot.Redact()

        doc.Save(dataDir + "output.pdf")