diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2021-11-02 12:19:18 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2021-11-02 12:19:18 +0100 |
commit | 384ea87914e8073667f67d27b47c6799515f4644 (patch) | |
tree | 1922982ef4e9555784354b881e4cff09b6291444 | |
parent | 55a7966d493e9972e07a60cc7ecbfba9a93a2eee (diff) |
Weekly planner: convert to pdf and join pages in a single file
-rwxr-xr-x | weekly_planner_generator.py | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/weekly_planner_generator.py b/weekly_planner_generator.py index d5bd90c..52bc091 100755 --- a/weekly_planner_generator.py +++ b/weekly_planner_generator.py @@ -2,7 +2,10 @@ import argparse import calendar +import logging import os +import subprocess +import sys import jinja2 @@ -29,20 +32,60 @@ def main(): continue last_monday = week[0] - with open("build/week_on_two_pages-A6-{year}-{page:02}.svg".format( + with open("build/week_on_two_pages-A6-{year}-{page:03}.svg".format( year=year, page=page, ), 'w') as fp: fp.write(template_verso.render(week=week)) page += 1 - with open("build/week_on_two_pages-A6-{year}-{page:02}.svg".format( + with open("build/week_on_two_pages-A6-{year}-{page:03}.svg".format( year=year, page=page, ), 'w') as fp: fp.write(template_recto.render(week=week)) page += 1 + inkscape_commands = ";\n".join([ + ( + "file-open:build/{svg};" + + " export-type: pdf;" + + " export-filename:build/{pdf};" + + " export-text-to-path;" + + " export-do" + ).format( + svg = s, + pdf = os.path.splitext(s)[0] + ".pdf", + ) + for s in os.listdir("build") + ]) + try: + subprocess.run( + ["inkscape", "--shell" ], + input=inkscape_commands, + text=True, + ) + except FileNotFoundError: + logging.warning("Inkscape is not installed, can't convert to pdf") + logging.warning("Stopping here, you can use the svgs as you like") + sys.exit(1) + + pdf_pages = sorted([ + os.path.join("build", p) + for p in os.listdir("build") + if p.endswith(".pdf") + ]) + try: + subprocess.run([ + "pdfjam", + "--outfile", "weekly_planner_A6.pdf", + *pdf_pages + ]) + except FileNotFoundError: + logging.warning("pdfjam is not installed") + logging.warning("you will have to join the pdf pages yourself") + sys.exit(1) + if __name__ == '__main__': main() |