Add quiet option, update doc, modify test
This commit is contained in:
parent
e4737ddfbb
commit
1f3ef9b236
@ -1,3 +1,9 @@
|
||||
0.14.0 (2021-09-30)
|
||||
-------------------
|
||||
- One can now use python -m docxtpl on command line
|
||||
to generate a docx from a template and a json file as a context
|
||||
Thanks to Lcrs123@github
|
||||
|
||||
0.12.0 (2021-08-15)
|
||||
-------------------
|
||||
- Code has be split into many files for better readability
|
||||
|
||||
@ -387,6 +387,30 @@ Then in your template, you will be able to use::
|
||||
|
||||
Euros price : {{ price_dollars|multiply_by(0.88) }}
|
||||
|
||||
|
||||
Command-line execution
|
||||
----------------------
|
||||
|
||||
One can use `docxtpl` module directly on command line to generate a docx from a template and a json file as a context::
|
||||
|
||||
usage: python -m docxtpl [-h] [-o] [-q] template_path json_path output_filename
|
||||
|
||||
Make docx file from existing template docx and json data.
|
||||
|
||||
positional arguments:
|
||||
template_path The path to the template docx file.
|
||||
json_path The path to the json file with the data.
|
||||
output_filename The filename to save the generated docx.
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
-o, --overwrite If output file already exists, overwrites without asking
|
||||
for confirmation
|
||||
-q, --quiet Do not display unnecessary messages
|
||||
|
||||
|
||||
See tests/module_execute.py for an example.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ Created : 2015-03-12
|
||||
|
||||
@author: Eric Lapouyade
|
||||
"""
|
||||
__version__ = '0.12.0'
|
||||
__version__ = '0.14.0'
|
||||
|
||||
# flake8: noqa
|
||||
from .inline_image import InlineImage
|
||||
|
||||
@ -7,11 +7,12 @@ 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=f'docxtpl [-h] [-o] {TEMPLATE_ARG} {JSON_ARG} {OUTPUT_ARG}',
|
||||
usage=f'python -m docxtpl [-h] [-o] [-q] {TEMPLATE_ARG} {JSON_ARG} {OUTPUT_ARG}',
|
||||
description='Make docx file from existing template docx and json data.')
|
||||
parser.add_argument(TEMPLATE_ARG,
|
||||
type=str,
|
||||
@ -25,6 +26,9 @@ def make_arg_parser() -> argparse.ArgumentParser:
|
||||
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
|
||||
|
||||
|
||||
@ -50,7 +54,7 @@ def is_argument_valid(arg_name: str, arg_value: str,overwrite: bool) -> bool:
|
||||
elif arg_name == OUTPUT_ARG:
|
||||
return arg_value.endswith('.docx') and check_exists_ask_overwrite(
|
||||
arg_value, overwrite)
|
||||
elif arg_name == OVERWRITE_ARG:
|
||||
elif arg_name in [OVERWRITE_ARG, QUIET_ARG]:
|
||||
return arg_value in [True, False]
|
||||
|
||||
|
||||
@ -108,9 +112,11 @@ def render_docx(doc:DocxTemplate, json_data: dict) -> DocxTemplate:
|
||||
raise RuntimeError(f'An error ocurred while trying to render the docx') from e
|
||||
|
||||
|
||||
def save_file(doc: DocxTemplate, output_path: Path) -> None:
|
||||
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(f'Document successfully generated and saved at {output_path}')
|
||||
except PermissionError as e:
|
||||
print(f'{e.strerror}. Could not save file {e.filename}.')
|
||||
@ -122,17 +128,18 @@ def main() -> None:
|
||||
# Everything is in a try-except block that cacthes a RuntimeError that is
|
||||
# raised if any of the individual functions called cause an error
|
||||
# themselves, terminating the main function.
|
||||
try:
|
||||
parsed_args = get_args(parser)
|
||||
try:
|
||||
validate_all_args(parsed_args)
|
||||
json_data = get_json_data(Path(parsed_args[JSON_ARG]).resolve())
|
||||
doc = make_docxtemplate(Path(parsed_args[TEMPLATE_ARG]).resolve())
|
||||
doc = render_docx(doc,json_data)
|
||||
save_file(doc, Path(parsed_args[OUTPUT_ARG]).resolve())
|
||||
save_file(doc, parsed_args)
|
||||
except RuntimeError as e:
|
||||
print('Error: '+e.__str__())
|
||||
return
|
||||
finally:
|
||||
if not parsed_args[QUIET_ARG]:
|
||||
print('Exiting program!')
|
||||
|
||||
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
from docxtpl.__main__ import main
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
TEMPLATE_PATH = 'templates/template_for_main_test.docx'
|
||||
JSON_PATH = 'templates/json_for_main_test.json'
|
||||
OUTPUT_FILENAME = 'output/output_for_main_test.docx'
|
||||
OVERWRITE = '-o'
|
||||
|
||||
|
||||
# Simulate command line arguments, running with overwrite flag so test can be
|
||||
# run repeatedly without need for confirmation:
|
||||
sys.argv[1:] = [TEMPLATE_PATH, JSON_PATH, OUTPUT_FILENAME, OVERWRITE]
|
||||
|
||||
main()
|
||||
|
||||
output_path = Path(OUTPUT_FILENAME)
|
||||
|
||||
if output_path.exists():
|
||||
print(f'File {output_path.resolve()} exists.')
|
||||
|
||||
22
tests/module_execute.py
Normal file
22
tests/module_execute.py
Normal file
@ -0,0 +1,22 @@
|
||||
import sys, os
|
||||
from pathlib import Path
|
||||
|
||||
TEMPLATE_PATH = 'templates/module_execute_tpl.docx'
|
||||
JSON_PATH = 'templates/module_execute.json'
|
||||
OUTPUT_FILENAME = 'output/module_execute.docx'
|
||||
OVERWRITE = '-o'
|
||||
QUIET = '-q'
|
||||
|
||||
|
||||
output_path = Path(OUTPUT_FILENAME)
|
||||
if output_path.exists():
|
||||
output_path.unlink()
|
||||
|
||||
os.chdir(Path(__file__).parent)
|
||||
cmd = f'python -m docxtpl {TEMPLATE_PATH} {JSON_PATH} {OUTPUT_FILENAME} {OVERWRITE} {QUIET}'
|
||||
print(f'Executing "{cmd}" ...')
|
||||
os.system(cmd)
|
||||
|
||||
if output_path.exists():
|
||||
print(f' --> File {output_path.resolve()} has been generated.')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user