file-generation


Namefile-generation JSON
Version 0.2.2 PyPI version JSON
download
home_pageNone
SummaryA package for generating DOCX and XLSX files and zipping them.
upload_time2025-01-31 20:13:00
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseMIT License Copyright (c) 2025 Gnel Sedrakyan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords docx xlsx file-generation templating zip
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

```bash
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}")
```

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

3. 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.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "file-generation",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "docx, xlsx, file-generation, templating, zip",
    "author": null,
    "author_email": "Gnel Sedrakyan <gnel@sedrakyan.am>",
    "download_url": "https://files.pythonhosted.org/packages/10/f0/48f7d9514c385a8904467eb77e738d8981eca024e2fe83af65ee35183e0f/file_generation-0.2.2.tar.gz",
    "platform": null,
    "description": "# File Generation\n\nA Python package for generating DOCX and XLSX files from templates and zipping them into a single archive. Perfect for automating document generation workflows.\n\n---\n\n## Features\n\n- **DOCX Generation**: Create `.docx` files from templates using the `docxtpl` library.\n- **XLSX Generation**: Generate `.xlsx` files with dynamic data and loops using `openpyxl`.\n- **Zipping**: Combine multiple generated files into a single `.zip` archive.\n- **Customizable**: Easily extendable for custom file types and workflows.\n- **Framework-Agnostic**: Works independently of Django or other frameworks.\n\n---\n\n## Installation\n\nInstall the package via pip:\n\n```bash\npip install file-generation\n```\n## Usage\n1. Generating DOCX Files\nUse the BaseDocxGenerator to generate .docx files from templates.\n```\nfrom file_generation.generators import BaseDocxGenerator\nfrom file_generation.core import MultiFileGenerationService\n\n# Define the output directory and instance ID\nbase_output_dir = \"/path/to/output\"\ninstance_id = 42\n\n# Define the context data for the template\ndocx_context = {\n    \"company_name\": \"Example Corp\",\n    \"date\": \"2025-01-31\",\n    \"user\": {\"name\": \"John Doe\", \"email\": \"john.doe@example.com\"}\n}\n\n# Initialize the DOCX generator\ndocx_generator = BaseDocxGenerator(\n    base_output_dir=base_output_dir,\n    instance_id=instance_id,\n    templates=[\"/path/to/template1.docx\", \"/path/to/template2.docx\"],\n    context=docx_context\n)\n\n# Generate files and create a ZIP archive\nservice = MultiFileGenerationService(generators=[docx_generator])\nzip_path = service.create_zip(\"output_files.zip\")\nprint(f\"ZIP created at: {zip_path}\")\n```\n\n2. Generating XLSX Files\nUse the BaseXlsxGenerator to generate .xlsx files with dynamic data and loops.\n```\nfrom file_generation.generators import BaseXlsxGenerator\nfrom file_generation.core import MultiFileGenerationService\n\n# Define the output directory and instance ID\nbase_output_dir = \"/path/to/output\"\ninstance_id = 42\n\n# Define the context data for the template\nxlsx_context = {\n    \"items\": [\n        {\"name\": \"Item 1\", \"price\": 10.99},\n        {\"name\": \"Item 2\", \"price\": 15.99},\n        {\"name\": \"Item 3\", \"price\": 7.50}\n    ],\n    \"total\": 34.48\n}\n\n# Initialize the XLSX generator\nxlsx_generator = BaseXlsxGenerator(\n    base_output_dir=base_output_dir,\n    instance_id=instance_id,\n    template_path=\"/path/to/template.xlsx\",\n    context=xlsx_context\n)\n\n# Generate files and create a ZIP archive\nservice = MultiFileGenerationService(generators=[xlsx_generator])\nzip_path = service.create_zip(\"output_files.zip\")\nprint(f\"ZIP created at: {zip_path}\")\n```\n\n3. Combining DOCX and XLSX Generation\nYou can combine multiple generators to create both .docx and .xlsx files in a single ZIP archive.\n\n```\nfrom file_generation.generators import BaseDocxGenerator, BaseXlsxGenerator\nfrom file_generation.core import MultiFileGenerationService\n\n# Define the output directory and instance ID\nbase_output_dir = \"/path/to/output\"\ninstance_id = 42\n\n# DOCX context and generator\ndocx_context = {\n    \"company_name\": \"Example Corp\",\n    \"date\": \"2025-01-31\"\n}\ndocx_generator = BaseDocxGenerator(\n    base_output_dir=base_output_dir,\n    instance_id=instance_id,\n    templates=[\"/path/to/template1.docx\"],\n    context=docx_context\n)\n\n# XLSX context and generator\nxlsx_context = {\n    \"items\": [\n        {\"name\": \"Item 1\", \"price\": 10.99},\n        {\"name\": \"Item 2\", \"price\": 15.99}\n    ],\n    \"total\": 26.98\n}\nxlsx_generator = BaseXlsxGenerator(\n    base_output_dir=base_output_dir,\n    instance_id=instance_id,\n    template_path=\"/path/to/template.xlsx\",\n    context=xlsx_context\n)\n\n# Generate files and create a ZIP archive\nservice = MultiFileGenerationService(generators=[docx_generator, xlsx_generator])\nzip_path = service.create_zip(\"output_files.zip\")\nprint(f\"ZIP created at: {zip_path}\")\n```\n\n## Advanced Usage\n### Customizing Output Filenames\nOverride the _get_output_filename method in your generator to customize filenames.\n```\nclass CustomDocxGenerator(BaseDocxGenerator):\n    def _get_output_filename(self, doc_info: Dict) -> str:\n        return f\"custom_{self.instance_id}_{doc_info['template_name']}.docx\"\n```\n\n### Adding New File Types\nExtend the BaseFileGenerator to support new file types.\n\n```\nclass CustomFileGenerator(BaseFileGenerator):\n    def generate_files_and_return_paths(self) -> List[str]:\n        # Implement your custom file generation logic here\n        return [\"/path/to/generated/file.custom\"]\n```\n\n## License\nThis project is licensed under the MIT License. See the LICENSE file for details.\n\n## Support\nIf you encounter any bug or have questions, please open an issue.\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2025 Gnel Sedrakyan\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.\n        ",
    "summary": "A package for generating DOCX and XLSX files and zipping them.",
    "version": "0.2.2",
    "project_urls": {
        "Documentation": "https://github.com/SedrakyanGnel/file-generation#readme",
        "Homepage": "https://github.com/SedrakyanGnel/file-generation",
        "Issues": "https://github.com/SedrakyanGnel/file-generation/issues",
        "Source": "https://github.com/SedrakyanGnel/file-generation"
    },
    "split_keywords": [
        "docx",
        " xlsx",
        " file-generation",
        " templating",
        " zip"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "445926bf79a46828cea7bf25cc7af76a2bb09462fd32b5b3ba60f9ec0054f43c",
                "md5": "16007a19903637f1c7d39b556e19ce03",
                "sha256": "d456e5088eade1a82ba02642ecf47ce08f07416a7a55e33050036c273bbcc13d"
            },
            "downloads": -1,
            "filename": "file_generation-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "16007a19903637f1c7d39b556e19ce03",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 7533,
            "upload_time": "2025-01-31T20:12:58",
            "upload_time_iso_8601": "2025-01-31T20:12:58.946118Z",
            "url": "https://files.pythonhosted.org/packages/44/59/26bf79a46828cea7bf25cc7af76a2bb09462fd32b5b3ba60f9ec0054f43c/file_generation-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "10f048f7d9514c385a8904467eb77e738d8981eca024e2fe83af65ee35183e0f",
                "md5": "6523e3e9c1d56ed3a2ee772a51a3681f",
                "sha256": "cc3a1cf912114afe009d0dd70e68db1c530a40cab138740eab4ba8de941b6309"
            },
            "downloads": -1,
            "filename": "file_generation-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "6523e3e9c1d56ed3a2ee772a51a3681f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 7613,
            "upload_time": "2025-01-31T20:13:00",
            "upload_time_iso_8601": "2025-01-31T20:13:00.782747Z",
            "url": "https://files.pythonhosted.org/packages/10/f0/48f7d9514c385a8904467eb77e738d8981eca024e2fe83af65ee35183e0f/file_generation-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-31 20:13:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SedrakyanGnel",
    "github_project": "file-generation#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "file-generation"
}
        
Elapsed time: 0.48842s