A package for generating DOCX and XLSX files and zipping them.
Project description
File Generation
A Python package for generating DOCX and XLSX files from templates and zipping them into a single archive. Perfect for automating document generation workflows.
Features
- DOCX Generation: Create
.docxfiles from templates using thedocxtpllibrary. - XLSX Generation: Generate
.xlsxfiles with dynamic data and loops usingopenpyxl. - Zipping: Combine multiple generated files into a single
.ziparchive. - Customizable: Easily extendable for custom file types and workflows.
- Framework-Agnostic: Works independently of Django or other frameworks.
Installation
Install the package via pip:
pip install file-generation
Usage
- Generating DOCX Files Use the BaseDocxGenerator to generate .docx files from templates.
from file_generation.generators import BaseDocxGenerator
from file_generation.core import MultiFileGenerationService
# Define the output directory and instance ID
base_output_dir = "/path/to/output"
instance_id = 42
# Define the context data for the template
docx_context = {
"company_name": "Example Corp",
"date": "2025-01-31",
"user": {"name": "John Doe", "email": "john.doe@example.com"}
}
# Initialize the DOCX generator
docx_generator = BaseDocxGenerator(
base_output_dir=base_output_dir,
instance_id=instance_id,
templates=["/path/to/template1.docx", "/path/to/template2.docx"],
context=docx_context
)
# Generate files and create a ZIP archive
service = MultiFileGenerationService(generators=[docx_generator])
zip_path = service.create_zip("output_files.zip")
print(f"ZIP created at: {zip_path}")
- Generating XLSX Files Use the BaseXlsxGenerator to generate .xlsx files with dynamic data and loops.
from file_generation.generators import BaseXlsxGenerator
from file_generation.core import MultiFileGenerationService
# Define the output directory and instance ID
base_output_dir = "/path/to/output"
instance_id = 42
# Define the context data for the template
xlsx_context = {
"items": [
{"name": "Item 1", "price": 10.99},
{"name": "Item 2", "price": 15.99},
{"name": "Item 3", "price": 7.50}
],
"total": 34.48
}
# Initialize the XLSX generator
xlsx_generator = BaseXlsxGenerator(
base_output_dir=base_output_dir,
instance_id=instance_id,
template_path="/path/to/template.xlsx",
context=xlsx_context
)
# Generate files and create a ZIP archive
service = MultiFileGenerationService(generators=[xlsx_generator])
zip_path = service.create_zip("output_files.zip")
print(f"ZIP created at: {zip_path}")
- Combining DOCX and XLSX Generation You can combine multiple generators to create both .docx and .xlsx files in a single ZIP archive.
from file_generation.generators import BaseDocxGenerator, BaseXlsxGenerator
from file_generation.core import MultiFileGenerationService
# Define the output directory and instance ID
base_output_dir = "/path/to/output"
instance_id = 42
# DOCX context and generator
docx_context = {
"company_name": "Example Corp",
"date": "2025-01-31"
}
docx_generator = BaseDocxGenerator(
base_output_dir=base_output_dir,
instance_id=instance_id,
templates=["/path/to/template1.docx"],
context=docx_context
)
# XLSX context and generator
xlsx_context = {
"items": [
{"name": "Item 1", "price": 10.99},
{"name": "Item 2", "price": 15.99}
],
"total": 26.98
}
xlsx_generator = BaseXlsxGenerator(
base_output_dir=base_output_dir,
instance_id=instance_id,
template_path="/path/to/template.xlsx",
context=xlsx_context
)
# Generate files and create a ZIP archive
service = MultiFileGenerationService(generators=[docx_generator, xlsx_generator])
zip_path = service.create_zip("output_files.zip")
print(f"ZIP created at: {zip_path}")
Advanced Usage
Customizing Output Filenames
Override the _get_output_filename method in your generator to customize filenames.
class CustomDocxGenerator(BaseDocxGenerator):
def _get_output_filename(self, doc_info: Dict) -> str:
return f"custom_{self.instance_id}_{doc_info['template_name']}.docx"
Adding New File Types
Extend the BaseFileGenerator to support new file types.
class CustomFileGenerator(BaseFileGenerator):
def generate_files_and_return_paths(self) -> List[str]:
# Implement your custom file generation logic here
return ["/path/to/generated/file.custom"]
License
This project is licensed under the MIT License. See the LICENSE file for details.
Support
If you encounter any bug or have questions, please open an issue.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file file_generation-0.2.3.tar.gz.
File metadata
- Download URL: file_generation-0.2.3.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5016463d1f9cf16f6acea219ee141ba59d79373ca9661bc51f32819a807acb2
|
|
| MD5 |
24252000aa51dedfba79bb103ac45c48
|
|
| BLAKE2b-256 |
a87d6ae9fc11af375196f1fdb6a26f32e1dedc87a156dd858619401e9329f94c
|
File details
Details for the file file_generation-0.2.3-py3-none-any.whl.
File metadata
- Download URL: file_generation-0.2.3-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
199477ad9d4c334a00bba6a2f4b256d71bea4196e0901f134092e8beeadbba0c
|
|
| MD5 |
6339fb026ca5fcee70b959b045618706
|
|
| BLAKE2b-256 |
40ca3e363787bcb4c61a6298a1b05aeda87a6f8e31f79ab54f5a176f094ae7fb
|