So, I want to hyperlink a text present in a PDF using Python.
I am able to extract the content of the PDF & make the particular text Hyperlink but I am unable to rewrite the contents of the PDF
Any help would be appreciated
from PyPDF2 import PdfFileReader
from PyPDF2 import PdfFileWriter
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader("a.pdf")
pdf_page = pdf_reader.getPage(0)
e = pdf_page.extractText()
text = "places"
def Convert(string):
li = list(string.split(" "))
return li
lis = Convert(e)
print(lis)
hyperlink_format = '<a href="https://stackoverflow.com/questions/13452410/creating-hyperlinks-in-python">places</a>'
for i in range(0, len(lis)):
if lis[i] == text:
del lis[i]
lis.insert(i, hyperlink_format)
print(lis)
lis = ' '.join(lis)
print(lis)
page = pdf_writer.addBlankPage(width=72, height=72)
from pathlib import Path
with Path("blank.pdf").open(mode="wb") as output_file:
pdf_writer.write()