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.5.tar.gz (7.7 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.5-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: file_generation-0.2.5.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

Hashes for file_generation-0.2.5.tar.gz
Algorithm Hash digest
SHA256 5fa713e59abfff84304093dab526df36a8eb93d71be997b4899a15dc2f6d12dd
MD5 cad728b3e595f987f71a4c7da9b27ca7
BLAKE2b-256 2089f0a7c32d53ee310c405d80b687b9c256613f4c859b310b8acf86d8c12d11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for file_generation-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8a9b795e034dd8a8ee7038e0e0adde45abed6d435730b308bf193ef1d2d4b276
MD5 08a83efaa6e1353fdcaed8215c53cf45
BLAKE2b-256 9db4e33cb5b192fb5ab1ba082c3d52c7bc23eebfdb6f8d256220765ed4bd92b6

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