You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
946 B
28 lines
946 B
import sys |
|
import subprocess |
|
import re |
|
|
|
extract_bookmarks_from = sys.argv[1] |
|
source_pdf_filename = sys.argv[2] |
|
output_filename = sys.argv[3] |
|
|
|
bookmarks_filename = extract_bookmarks_from.replace('.pdf', '.txt') |
|
assert(bookmarks_filename != extract_bookmarks_from) |
|
|
|
# extract PDF metadata into a text file |
|
subprocess.run(['pdftk', extract_bookmarks_from, 'dump_data', 'output', bookmarks_filename]) |
|
|
|
with open(bookmarks_filename) as bookmarks_file: |
|
metadata = bookmarks_file.read() |
|
|
|
# Offset page numbers |
|
def replaceBookmarkPageNumber(match): |
|
initial_page = int(match.group(1)) |
|
return 'BookmarkPageNumber: ' + str(initial_page + 2) |
|
metadata = re.sub('BookmarkPageNumber:\s+([0-9]+)', replaceBookmarkPageNumber, metadata) |
|
|
|
with open(bookmarks_filename, 'w') as bookmarks_file: |
|
bookmarks_file.write(metadata) |
|
|
|
# generate the output PDF |
|
subprocess.run(['pdftk', source_pdf_filename, 'update_info', bookmarks_filename, 'output', output_filename])
|
|
|