Skip to content

Instantly share code, notes, and snippets.

@jtoman

jtoman/split_toc.py

Last active Nov 13, 2018
Embed
What would you like to do?
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import subprocess, sys, os, tempfile
import atexit, shutil
prefix = "BookmarkPageNumber: "
split_range = int(sys.argv[2])
info = subprocess.check_output(["pdftk", sys.argv[1], "dump_data"])
appendix = []
main_paper = []
curr_bookmark = None
for i in info.split("\n"):
if i == "BookmarkBegin":
if curr_bookmark is not None:
(page, bk) = curr_bookmark
if page > split_range:
appendix += bk
else:
main_paper += bk
curr_bookmark = (None, [])
if i.startswith(prefix):
num = int(i[len(prefix):])
curr_bookmark = (num, curr_bookmark[1])
if num > split_range:
i = prefix + str(num - 29)
if curr_bookmark is not None:
curr_bookmark[1].append(i)
tmp = tempfile.mkdtemp()
atexit.register(lambda: shutil.rmtree(tmp))
def update_info(tmp_file, info, output_name):
new_info = subprocess.check_output(["pdftk", tmp + "/" + tmp_file + ".pdf", "dump_data"])
with open(os.path.join(tmp, tmp_file + ".info"), "w") as f:
print >> f, new_info
print >> f, "\n".join(info)
subprocess.check_call(["pdftk", tmp + "/" + tmp_file + ".pdf", "update_info", os.path.join(tmp, tmp_file + ".info"), "output", output_name])
subprocess.check_call(["pdftk", sys.argv[1], "cat", "1-%d" % split_range, "output", tmp + "/tmp_main.pdf"])
subprocess.check_call(["pdftk", sys.argv[1], "cat", "%d-end" % (split_range + 1), "output", tmp + "/tmp_appendix.pdf"])
update_info("tmp_main", main_paper, sys.argv[1])
update_info("tmp_appendix", appendix, sys.argv[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment