Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys, os, subprocess
- def bash(cmd, read=False):
- if read:
- try:
- x = subprocess.check_output(cmd, shell=True).decode("utf-8")
- except:
- x = False
- return x
- else:
- os.system(cmd)
- return
- def addBookmark(title, level, pageNumber):
- ret = f"BookmarkBegin\nBookmarkTitle: {title}\nBookmarkLevel: {level}\nBookmarkPageNumber: {pageNumber}\n"
- return ret
- def extractBookmarks(filename):
- return bash(f"pdftk '{filename}' data_dump", read=True)
- def merge(first, second):
- # add a bookmark to the second.pdf and create second.bookmarked.pdf
- secondBookmarkedData = addBookmark(second.split("/")[-1][:-4], 1, 1) + extractBookmarks(second)
- with open("addBookmark.bookmarks", "w") as bookmarks:
- bookmarks.write(secondBookmarkedData)
- bash(f"pdftk '{second}' update_info 'addBookmark.bookmarks' output '{second}.bookmark.pdf'")
- # merge pdf1 and pdf2 into final.pdf
- bash(f"pdftk '{first}' '{second}.bookmark.pdf' cat output '{first}.2.pdf'")
- bash(f"mv '{first}' ~/.local/share/Trash/ && mv '{first}.2.pdf' '{first}' && mv '{second}.bookmark.pdf' ~/.local/share/Trash/")
- bash(f"mv 'addBookmark.bookmarks' ~/.local/share/Trash/ ")
- def main():
- first = sys.argv[1]
- second = sys.argv[2]
- merge(first, second)
- if(len(sys.argv) > 3):
- for arg in sys.argv[3:]:
- merge(first, arg)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement