2017-07-19 2 views
2

Wie ändere ich die Hyperlinks in PDF mit Python? Ich verwende derzeit ein PyPDF2, um die Seiten zu öffnen und zu durchlaufen. Wie scanne ich tatsächlich nach Hyperlinks und fahre fort, um die Hyperlinks zu ändern?Wie ändere ich Hyperlinks in PDF mit Python?

Antwort

0

So konnte ich nicht bekommen, was Sie wollen mit der pyPDF2 Bibliothek.

Ich habe jedoch etwas mit einer anderen Bibliothek arbeiten: pdfrw. Das installierte gut für mich Pip in Python 3.6 mit: Ich habe mit this example pdf fand ich online, die enthält mehrere Links für die folgende:

pip install pdfrw 

Hinweis. Ihre Laufleistung kann davon abweichen.

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 
    for annot in page.Annots or []: #Links are in Annots, but some pages 
            #don't have links so Annots returns None 
     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") 
Verwandte Themen