Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Script to add bookmarks to a PDF (Need review and suggestions)

Hello guys,

So i made this little script that adds bookmarks to PDFs using Py2PDF module. It's not really a full automation of the process of adding bookmarks since you still have to manually add the parent bookmark and add the bookmarks (Chapters + their respectives pages) into a dictionnary. Once the dictionnary is made, the bookmarks are added with a for loop that uses the addBookmark method in a new output PDF file.

from PyPDF2 import PdfFileWriter, PdfFileReader

pdf_object = open("pdf_without_bookmarks.pdf","rb") #rb stands for read binary
output = PdfFileWriter()
input = PdfFileReader(pdf_object)

input_numpages = input.getNumPages()

#basically just copy the input file
for i in range(input_numpages):
output.addPage(input.getPage(i)) #insert page in the output file

parent_1 = output.addBookmark('Parent 2', page of parent 2) # add parent bookmark
bookmarks_dic_1 = {
"Chapter 1": page of Chapter 1,
"Chapter 2": page of Chapter 2,
"Chapter n": page of Chapter n,
}



for k, v in bookmarks_dic_1.items():

output.addBookmark(k ,v +1,parent_1 ) #add child bookmarks


parent_2 = output.addBookmark('Parent 2', page of parent 2)
bookmarks_dic_2 = {
"Chapter 1": page of Chapter 1,
"Chapter 2": page of Chapter 2,
"Chapter n": page of Chapter n,
}

for k, v in bookmarks_dic_2.items():

output.addBookmark(k ,v +1,parent_2 ) #add child bookmarks

outputstream = open('pdf_with_bookmarks.pdf','wb') #creating result
output.write(outputstream) #writing to result pdf
outputstream.close() #closing result


Initially, when i started working on this, i wanted to scrape Chapter and Page information from PDF filesummary, but i realized it was too ambitious and that it requires to know how to use regex (Am i right?).

I also want to know if there's a way to put all this in one loop, instead of having to create a new loop for each parent. But i don't know how. Like is there such thing as a dictionnary with more than 1 value? The Parent will be the key and the values Name of chapter and Page of chapter. A kind of a double dictionnary. Something like this :

summary_dictionnary = {"Parent 1":{"Chapter 1": Page, "Chapter n": Page}, "Parent n": {"Chapter 1": Page,"Chapter n":Page}}

Is that even possible?
I'm also posting here to have a general review on my code, what do you guys would change?

Big thanks in advance

/r/Python
https://redd.it/7tih40