The project automates the process of creating Word documents.
Main library: docxtpl
The idea is to begin to create an example of the document you want to generate with Microsoft Word, it can be as complex as you want : pictures, index tables, footer, header, variables, anything you can do with word. Then, as you are still editing the document with microsoft word, you insert jinja2-like tags directly in the document. You save the document as a .docx file (xml format) : it will be your .docx template file.
Now you can use python-docx-template to generate as many word documents you want from this .docx template and context variables you will associate.
The script was created for GitLab, so here is this file .gitlab-ci.yaml
The main modules are located in the src directory, everything else just imports them with different settings.
- The
loadermodule manages data loading - The
docx_makermodule manages the creation and saving of reports - The
mainmodule is the entry point of the program, it connects all modules
❗Sample data and Word documents are stored in tests/
Clone the repository to any folder, for example via HTTPS:
git clone https://github.com/Ulad/DocuCraft.git
cd 'your directory'The project uses UV to resolve dependencies and virtual environment, if you are already using it, you can skip this step
pip install uvAfter installation:
uv syncAll dependencies are specified in the pyproject.toml file.
UPD: For backward compatibility, it was added requirements.txt in pre-commit, installation via pip (Not recommended!):
pip install -r requirements.txt --require-hashesThe project uses separate settings file with pydantic_settings library:
settings.py
By default, variables for local development are specified there. In the production environment, you need to create environment variables with the same names or create an .env file in which you can also specify these variables. There is an example in the .env.example file.
Important! In the loader module, the main function load_excel_data loads only from Excel's named tables, I did it because I want to and it's convenient. 😊
PATH_TO_EXCEL = "path/to/your/excel/file.xlsx"
TABLE_NAME_IN_EXCEL = "table_name"
PATH_TO_DOCX_TPL = "path/to/your/template.docx"
OUTPUT_DIR_DOCX = "path/to/output/docx/"Just run docucraft/main.py:
python main.pySome transformations have been done in the main so far, I don't know how to do it in a separate module yet, since the transformations are different in each case.
To output all logs to the console, it is enough to change the file src/helpers/log_settings.yaml
consoleHandler:
level: INFO # Change from ERROR to INFO for detailed logsThis repository is licensed under the MIT License