python - 我如何使用 python 更改 pdf 中的超链接?

标签 python pdf hyperlink

如何使用 python 更改 pdf 中的超链接?我目前正在使用 pyPDF2 打开并循环浏览页面。我如何实际扫描超链接然后继续更改超链接?

最佳答案

所以我无法使用 pyPDF2 得到你想要的东西图书馆。

但是我确实得到了一些与另一个库一起工作的东西:pdfrw .这对我在 Python 3.6 中使用 pip 安装很好:

pip install pdfrw

注意:以下我一直在使用 this example pdf我在网上找到包含多个链接。您的里程可能会因此而有所不同。

import pdfrw

pdf = pdfrw.PdfReader("pdf.pdf")  # Load the pdf
new_pdf = pdfrw.PdfWriter()  # Create an empty pdf

for page in pdf.pages:  # Go through the pages

    # Links are in Annots, but some pages don't have links so Annots returns None
    for annot in page.Annots or []:

        old_url = annot.A.URI

        # >Here you put logic for replacing the URLs<
        
        # Use the PdfString object to do the encoding for us
        # Note the brackets around the URL here
        new_url = pdfrw.objects.pdfstring.PdfString("(http://www.google.com)")

        # Override the URL with ours
        annot.A.URI = new_url

    new_pdf.addpage(page)    

new_pdf.write("new.pdf")

关于python - 我如何使用 python 更改 pdf 中的超链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45191215/

相关文章:

python - 使用 ctypes : undefined symbol 将 C++ 函数导出到 python

python - 引发运行时错误 ('FPDF error: ' +msg) 运行时错误 : FPDF error: Unsupported image type: chapter_1_romance_dawn

ios - 我如何在 Storyboard中使用 pdf 作为图像?

javascript - window.location.assign(var String) 不起作用

python - 如何绘制跟随鼠标光标的十字?

python - 我将如何修改/添加文本到 tkinter.Label?

java - PDFBox - 获取单词位置(而不仅仅是字符')

PHP MYSQL 超链接和表单字段

javascript - 特殊字符 – 在 href 上

python - 在 EPD (Windows x64) 上安装 Theano