Python Forum
automate new PDF creation with Bookmarks Based up Regex
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
automate new PDF creation with Bookmarks Based up Regex
#1
I am seeking to create some functionality to recasting a PDF file of medical records. The date of treatment or visit is very important and I will like to bookmark based upon treatment date. So for this particular pdf file, the treatment date appears as "Visit date: 01/01/2010" format. The below code does successfully create the new pdf but does not create any bookmarks. I am a newbie at this - any help would be appreciated:

import PyPDF4
import re

# Open the PDF file for reading
pdf_file = open(r"C:\Users\StanleyDenman\Documents\NewMedical.pdf", 'rb')
pdf_reader = PyPDF4.PdfFileReader(pdf_file)
pdf_writer = PyPDF4.PdfFileWriter()


# Define the regular expression for finding the bookmark locations
regex = re.compile("Visit date: '\b\d{2}/\d{2}/\d{4}\b'")

# Iterate through the pages of the PDF
for i in range(len(pdf_reader.pages)):
    page = pdf_reader.getPage(i)
    text = page.extractText()
    matches = re.findall(regex, text)
    pdf_writer.addPage(page)
    

for match in matches:
   #  Create a bookmark for each match
       bookmark = PyPDF4.pdf.Destination()
       bookmark.page = pdf_writer.addpage()
       bookmark.title = match.group(1)
    

# Write the new PDF file with bookmarks
output_file = open('NewPDF2.pdf', 'wb')
pdf_writer.write(output_file)
output_file.close()
pdf_file.close()
Output:
No error. but like I said, "newPDF2.pdf" has no bookmarks. In fact, the file is identifical to C:\Users\StanleyDenman\Documents\NewMedical.pdf
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split pdf in pypdf based upon file regex standenman 1 2,073 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  Creating new list based on exact regex match in original list interjectdirector 1 2,279 Mar-08-2020, 09:30 PM
Last Post: deanhystad
  Regex Issue from Automate the Boring Stuff book robgraves 2 2,814 Jun-16-2019, 12:41 AM
Last Post: robgraves
  Automate the boring stuff: regex not matching DJ_Qu 7 3,772 Apr-27-2019, 07:03 AM
Last Post: micseydel
  delete pages in pdf according to bookmarks vagser 0 1,897 Sep-29-2018, 11:33 AM
Last Post: vagser
  1. How can I automate this batch creation of documents? 2. How can I automate posting SamLearnsPython 2 3,423 Jul-02-2018, 11:36 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020