Add support for sheets out of a .ods

This commit is contained in:
Skye 2023-12-02 20:16:39 -05:00
parent 74a49732bd
commit cf36808e55
2 changed files with 36 additions and 3 deletions

View file

@ -25,6 +25,7 @@
devShell = with pkgs;
mkShell {
packages = [
libreoffice
typst
python3
python3Packages.black

View file

@ -6,6 +6,7 @@ import hashlib
import json
import os
import string
import subprocess
import typing
import urllib.parse
import xml.etree.ElementTree as ET
@ -149,7 +150,9 @@ def get_country_name(
parser = argparse.ArgumentParser(
prog="format", description="format postcards with latex"
)
parser.add_argument("template", help="template to use", nargs="?", type=Path, default="2card")
parser.add_argument(
"template", help="template to use", nargs="?", type=Path, default="2card"
)
parser.add_argument(
"-o", "--origin", help="origin country code", default="us", type=iso_code
)
@ -198,6 +201,13 @@ parser.add_argument(
help="Skip content, e.g. to make postcard back labels",
)
parser.add_argument(
"--sheet-name",
default="",
type=str,
help="Which sheet to use out of a .ods",
)
parser.add_argument(
"-d", "--dont-compile", action="store_true", help="Don't compile to output.pdf"
)
@ -215,6 +225,7 @@ class Args(argparse.Namespace):
content_path: Path
no_content: bool
dont_compile: bool
sheet_name: str
args = parser.parse_args(args=None, namespace=Args())
@ -223,7 +234,26 @@ cldr_root = ET.parse(
f"{os.getenv('CLDR_ROOT')}/share/unicode/cldr/common/main/{args.language}.xml"
)
csvfile = open(args.address_file)
if args.address_file.suffix == ".csv":
csv_filename = args.address_file
elif args.address_file.suffix == ".ods":
result = subprocess.run(
[
"libreoffice",
"--headless",
"--convert-to",
"csv:Text - txt - csv (StarCalc):44,34,76,1,,0,false,true,true,false,false,-1",
"--outdir",
str(cache_dir()),
args.address_file,
]
)
assert result.returncode == 0
csv_filename = cache_dir() / f"{args.address_file.stem}-{args.sheet_name}.csv"
else:
raise Exception("Unknown file type for --address-file")
csvfile = open(csv_filename)
rows = csv.DictReader(csvfile)
with open("secrets.json") as secrets_file:
@ -287,11 +317,13 @@ if args.origin == "us":
serial += 1
imb.write_current_serial(serial)
def serialize_paths(obj: object):
if isinstance(obj, Path):
return str(obj)
raise TypeError("Type not Serializable")
with (root_dir() / "options.json").open("w") as options:
json.dump(
fp=options,
@ -299,7 +331,7 @@ with (root_dir() / "options.json").open("w") as options:
"args": args.__dict__,
"cards": cards,
},
default=serialize_paths
default=serialize_paths,
)
if args.dont_compile: