如何使用 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/