Add bookmarks to the given PDF file in batch. You can also export bookmarks from the given PDF file.
You can clone the repository by git:
git clone https://github.com/RunfengTsui/AddPDFBookmarks.git
You can also download zip file and then unzip it.
Use Poetry to manage dependencies. Run poetry install --no-root command to create virtual environment and install dependencies.
Of course, you can use pip install the packages:
pip3 install pikepdf, chardet
Create file main.py and export the bookmarks from the given file.
from src.pdfhandler import PDFHandler
pdf = PDFHandler("filename.pdf")
pdf.export_bookmarks("bookmarks.txt")Then, you can modify the bookmarks.txt file and add it to the PDF file.
from src.pdfhandler import PDFHandler
pdf = PDFHandler("filename.pdf")
pdf.add_bookmarks_from_file("bookmarks.txt", "filename_bookmarks.pdf", page_offset=0)In the function read_bookmarks_from_file(), we handled the UnicodeDecodeError. We add the function identify_encoding() which can identify possible encoding of the given file by package chardet.
Now, you can use the method export_bookmarks() to export the bookmarks from the given file. At the same time, move the function read_bookmarks_from_file() from the class PDFHandler to file src/tools.py.