Skip to main content

PowerPoint template engine with slide copying and placeholder replacement

Project description

pptx-templatex

A PowerPoint template engine for Python that provides slide copying and placeholder replacement functionality.

日本語README

Features

  • Copy slides from a source PowerPoint file (.pptx)
  • Replace placeholders enclosed in {{ }}
  • Support for nested object access (e.g., {{ user.name }}, {{ company.department.name }})
  • Support for array element access (e.g., {{ items[0].name }}, {{ users.[0].email }})
  • Batch processing with JSON configuration files

Installation

pip install -e .

For development with testing dependencies:

pip install -e ".[dev]"

Quick Start

  1. Create a PowerPoint template file with placeholders:

    • Open PowerPoint and create a presentation
    • Add text boxes with placeholders like {{ name }}, {{ user.email }}, {{ items[0].title }}
    • Save as template.pptx
  2. Create a JSON configuration file (config.json):

{
  "slides": [
    {
      "src_page": 1,
      "replace_texts": {
        "name": "John Doe",
        "user": {
          "email": "john@example.com"
        },
        "items": [
          {"title": "First Item"}
        ]
      }
    }
  ]
}
  1. Run the command:
pptx-templatex template.pptx config.json output.pptx

Usage

Command Line Interface

After installation, you can use the pptx-templatex command:

# Basic usage
pptx-templatex template.pptx config.json output.pptx

# View help
pptx-templatex --help

# View version
pptx-templatex --version

Python API

from pptx_templatex import TemplateEngine

# Load template file
engine = TemplateEngine("template.pptx")

# Define configuration
config = {
    "slides": [
        {
            "src_page": 1,
            "replace_texts": {
                "name": "John Doe",
                "title": "Software Engineer"
            }
        }
    ]
}

# Process and output
engine.process(config, "output.pptx")

Nested Object Access

config = {
    "slides": [
        {
            "src_page": 1,
            "replace_texts": {
                "user": {
                    "name": "John Doe",
                    "email": "john@example.com"
                }
            }
        }
    ]
}

In the template:

Name: {{ user.name }}
Email: {{ user.email }}

Array Element Access

config = {
    "slides": [
        {
            "src_page": 1,
            "replace_texts": {
                "items": [
                    {"name": "Product A", "price": "1000"},
                    {"name": "Product B", "price": "2000"}
                ]
            }
        }
    ]
}

In the template:

First item: {{ items[0].name }} - ${{ items[0].price }}
Second item: {{ items[1].name }} - ${{ items[1].price }}

Creating Multiple Slides

config = {
    "slides": [
        {"src_page": 1, "replace_texts": {"title": "Introduction"}},
        {"src_page": 2, "replace_texts": {"content": "Main Content"}},
        {"src_page": 1, "replace_texts": {"title": "Conclusion"}},
    ]
}

Using JSON Configuration File

Create a config.json file:

{
  "slides": [
    {
      "src_page": 1,
      "replace_texts": {
        "name": "John Doe",
        "items": [{"key": "value"}]
      }
    }
  ]
}

Use with CLI:

pptx-templatex template.pptx config.json output.pptx

Or use with Python API:

engine = TemplateEngine("template.pptx")
engine.process("config.json", "output.pptx")

Real-World Example

Scenario: Generate personalized presentation for multiple users

  1. Create template.pptx with:

    • Slide 1: Title slide with {{ name }} and {{ title }}
    • Slide 2: Content slide with {{ company.name }} and {{ company.address }}
    • Slide 3: List slide with {{ items[0].description }}
  2. Create config.json:

{
  "slides": [
    {
      "src_page": 1,
      "replace_texts": {
        "name": "Alice Smith",
        "title": "Senior Developer"
      }
    },
    {
      "src_page": 2,
      "replace_texts": {
        "company": {
          "name": "Tech Corp",
          "address": "123 Main St"
        }
      }
    },
    {
      "src_page": 3,
      "replace_texts": {
        "items": [
          {"description": "Improved performance by 50%"},
          {"description": "Reduced bugs by 30%"}
        ]
      }
    }
  ]
}
  1. Generate:
pptx-templatex template.pptx config.json alice_presentation.pptx

This creates a 3-slide presentation with all placeholders replaced.

Configuration Format

Configuration Object Structure

{
  "slides": [
    {
      "src_page": 1,
      "replace_texts": {
        "key": "value"
      }
    }
  ]
}
  • slides (required): Array of slide configurations
    • src_page (required): Source slide number to copy (1-based index)
    • replace_texts (optional): Mapping of text to replace

Placeholder Syntax

  • Simple replacement: {{ key }}
  • Nested keys: {{ user.name }}, {{ company.department.name }}
  • Array access: {{ items[0] }}, {{ users[0].name }}
  • Complex paths: {{ company.departments[0].teams[1].name }}

Testing

pytest

With coverage report:

pytest --cov=pptx_templatex --cov-report=html

Project Structure

pptx-templatex/
├── pptx_templatex/
│   ├── __init__.py
│   ├── template_engine.py       # Main template engine
│   ├── placeholder_replacer.py  # Placeholder replacement logic
│   └── exceptions.py            # Custom exceptions
├── tests/
│   ├── __init__.py
│   ├── test_template_engine.py
│   └── test_placeholder_replacer.py
├── examples/
│   ├── example_usage.py
│   └── config.json
├── pyproject.toml
└── README.md

License

MIT

Author

SpringMT

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

pptx_templatex-0.1.1.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

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

pptx_templatex-0.1.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file pptx_templatex-0.1.1.tar.gz.

File metadata

  • Download URL: pptx_templatex-0.1.1.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for pptx_templatex-0.1.1.tar.gz
Algorithm Hash digest
SHA256 21f5d3e161c6ba2f95cacfef3d015e7cbf91341f58d31a38e1e6f22a689efb06
MD5 2db2c8ef1bdfa730a5733855461b44c8
BLAKE2b-256 4ad2bb6ba343bfb1c27eefdf3c67816b1d541937ce88307a375f99847ce67496

See more details on using hashes here.

File details

Details for the file pptx_templatex-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pptx_templatex-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for pptx_templatex-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ad65b9e2d6ed8ef3f7354b885a54eca186051563d98e21843bacc7c4d7654f9
MD5 04b20f7a45daee97ac014caed9e43df3
BLAKE2b-256 8044373fa635f887cc35a11e295a2a066597090f3d1dca597b2dec4bddd26caf

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