Skip to main content

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 .docx files from templates using the docxtpl library.
  • XLSX Generation: Generate .xlsx files with dynamic data and loops using openpyxl.
  • Zipping: Combine multiple generated files into a single .zip archive.
  • 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

  1. 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}")
  1. 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}")
  1. 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

file_generation-0.2.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

file_generation-0.2.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file file_generation-0.2.1.tar.gz.

File metadata

  • Download URL: file_generation-0.2.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for file_generation-0.2.1.tar.gz
Algorithm Hash digest
SHA256 520909bc930214be8d0897585ff42bf4d6b2b5a808ec7ae1d24662d5d7c1551b
MD5 1abcfda75ece9049bd9680e9c31f5624
BLAKE2b-256 11e33936b18e8bb94987517bfd3a006c92babd3afcf7436ab229a6e2d104f40a

See more details on using hashes here.

File details

Details for the file file_generation-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for file_generation-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a52369417252e9c36a219093cb1a7966b833007cfc408e5d68b18af1fc249747
MD5 c826f5db9557c843a112f19de1601e33
BLAKE2b-256 7f97abae15f5bc1c9a31229301df5c736c9d1a4d506a6b9f17769d8e6e6d9ec6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page