forked from elapouya/python-docx-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
178 lines (153 loc) · 5.73 KB
/
__main__.py
File metadata and controls
178 lines (153 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from __future__ import annotations
import argparse
import json
import os
from .template import DocxTemplate, TemplateError
TEMPLATE_ARG = "template_path"
JSON_ARG = "json_path"
OUTPUT_ARG = "output_filename"
OVERWRITE_ARG = "overwrite"
QUIET_ARG = "quiet"
def make_arg_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
usage="python -m docxtpl [-h] [-o] [-q] {} {} {}".format(
TEMPLATE_ARG, JSON_ARG, OUTPUT_ARG
),
description="Make docx file from existing template docx and json data.",
)
parser.add_argument(
TEMPLATE_ARG, type=str, help="The path to the template docx file."
)
parser.add_argument(
JSON_ARG, type=str, help="The path to the json file with the data."
)
parser.add_argument(
OUTPUT_ARG, type=str, help="The filename to save the generated docx."
)
parser.add_argument(
"-" + OVERWRITE_ARG[0],
"--" + OVERWRITE_ARG,
action="store_true",
help="If output file already exists, overwrites without asking for confirmation",
)
parser.add_argument(
"-" + QUIET_ARG[0],
"--" + QUIET_ARG,
action="store_true",
help="Do not display unnecessary messages",
)
return parser
def get_args(parser) -> dict:
try:
parsed_args = vars(parser.parse_args())
return parsed_args
# Argument errors raise a SystemExit with code 2. Normal usage of the
# --help or -h flag raises a SystemExit with code 0.
except SystemExit as e:
if e.code == 0:
raise SystemExit
else:
raise RuntimeError(
"Correct usage is:\n{parser.usage}".format(parser=parser)
)
def is_argument_valid(arg_name: str, arg_value: str, overwrite: bool) -> bool | None:
# Basic checks for the arguments
if arg_name == TEMPLATE_ARG:
return os.path.isfile(arg_value) and arg_value.endswith(".docx")
elif arg_name == JSON_ARG:
return os.path.isfile(arg_value) and arg_value.endswith(".json")
elif arg_name == OUTPUT_ARG:
return arg_value.endswith(".docx") and check_exists_ask_overwrite(
arg_value, overwrite
)
elif arg_name in [OVERWRITE_ARG, QUIET_ARG]:
return arg_value in [True, False]
return None
def check_exists_ask_overwrite(arg_value: str, overwrite: bool) -> bool:
# If output file does not exist or command was run with overwrite option,
# returns True, else asks for overwrite confirmation. If overwrite is
# confirmed returns True, else raises OSError.
if os.path.exists(arg_value) and not overwrite:
try:
msg = (
"File %s already exists, would you like to overwrite the existing file? "
"(y/n)" % arg_value
)
if input(msg).lower() == "y":
return True
else:
raise OSError
except OSError:
raise RuntimeError(
"File %s already exists, please choose a different name." % arg_value
)
else:
return True
def validate_all_args(parsed_args: dict) -> None:
overwrite = parsed_args[OVERWRITE_ARG]
# Raises AssertionError if any of the arguments is not validated
try:
for arg_name, arg_value in parsed_args.items():
if not is_argument_valid(arg_name, arg_value, overwrite):
raise AssertionError
except AssertionError:
raise RuntimeError(
'The specified {arg_name} "{arg_value}" is not valid.'.format(
arg_name=arg_name, arg_value=arg_value
)
)
def get_json_data(json_path) -> dict:
with open(json_path) as file:
try:
json_data = json.load(file)
return json_data
except json.JSONDecodeError as e:
print(
"There was an error on line {e.lineno}, column {e.colno} while trying "
"to parse file {json_path}".format(e=e, json_path=json_path)
)
raise RuntimeError("Failed to get json data.")
def make_docxtemplate(template_path: str) -> DocxTemplate:
try:
return DocxTemplate(template_path)
except TemplateError:
raise RuntimeError("Could not create docx template.")
def render_docx(doc: DocxTemplate, json_data: dict) -> DocxTemplate:
try:
doc.render(json_data)
return doc
except TemplateError:
raise RuntimeError("An error ocurred while trying to render the docx")
def save_file(doc: DocxTemplate, parsed_args: dict) -> None:
try:
output_path = parsed_args[OUTPUT_ARG]
doc.save(output_path)
if not parsed_args[QUIET_ARG]:
print(
"Document successfully generated and saved at {output_path}".format(
output_path=output_path
)
)
except OSError as e:
print("{e.strerror}. Could not save file {e.filename}.".format(e=e))
raise RuntimeError("Failed to save file.")
def main() -> None:
parser = make_arg_parser()
# Everything is in a try-except block that catches a RuntimeError that is
# raised if any of the individual functions called cause an error
# themselves, terminating the main function.
parsed_args = get_args(parser)
try:
validate_all_args(parsed_args)
json_data = get_json_data(os.path.abspath(parsed_args[JSON_ARG]))
doc = make_docxtemplate(os.path.abspath(parsed_args[TEMPLATE_ARG]))
doc = render_docx(doc, json_data)
save_file(doc, parsed_args)
except RuntimeError as e:
print("Error: " + e.__str__())
return
finally:
if not parsed_args[QUIET_ARG]:
print("Exiting program!")
if __name__ == "__main__":
main()