Skip to main content

Templates

Upload and reuse document templates (PDF, Word, Excel) to automatically fill them with data.

Overview

Templates allow you to treat documents as reusable forms. You upload a document once, and DocuDevs analyzes it to find fillable fields. You can then fill these templates repeatedly with new data via the API.

Supported Formats:

  • PDF Forms: Standard AcroForms.
  • Word Documents: .docx files with placeholders or content controls.
  • Excel Spreadsheets: .xlsx files.
  • PowerPoint Presentations: .pptx files.

Managing Templates

Uploading a Template

Upload a document to create a new template. DocuDevs will automatically detect the fields available for filling.

from docudevs.docudevs_client import DocuDevsClient
import os
import asyncio

client = DocuDevsClient(token=os.getenv('API_KEY'))

async def upload_invoice_template():
with open('invoice_template.pdf', 'rb') as f:
# Upload template with name "invoice"
response = await client.upload_template(
name="invoice",
document=f,
mime_type="application/pdf"
)

# The response contains the detected form fields
print(f"Template uploaded. Detected fields: {response.parsed.form_fields}")

# asyncio.run(upload_invoice_template())

Listing Templates

Get a list of all available templates in your organization.

templates = await client.list_templates()
for template in templates:
print(f"Name: {template.name}, Created: {template.created_at}")

Inspecting Template Metadata

Retrieve details about a specific template, including its fillable fields.

metadata = await client.metadata("invoice")
print(f"Fields for {metadata.name}:")
for field in metadata.form_fields:
print(f"- {field.name} ({field.type})")

Deleting a Template

Remove a template when it is no longer needed.

await client.delete_template("invoice")

Filling Templates

Once a template is uploaded, you can fill it with data. The data structure depends on the template type.

Filling a PDF Form

PDF forms typically use a flat dictionary of field names and values.

from docudevs.models import TemplateFillRequest

async def fill_invoice():
fill_request = TemplateFillRequest(
fields={
"customerName": "Acme Corp",
"invoiceNumber": "INV-2024-001",
"totalAmount": "1500.00",
"paid": True
}
)

response = await client.fill(name="invoice", body=fill_request)

# Save the filled PDF
with open("filled_invoice.pdf", "wb") as f:
f.write(response.content)

# asyncio.run(fill_invoice())

Filling a Word Template with Tables

Word templates support nested data structures, allowing you to fill tables and repeated sections.

async def fill_report():
fill_request = TemplateFillRequest(
fields={
"reportTitle": "Quarterly Sales",
"date": "2024-04-01",
"items": [
{"product": "Widget A", "sales": 100, "revenue": 5000},
{"product": "Widget B", "sales": 200, "revenue": 8000},
{"product": "Widget C", "sales": 50, "revenue": 2500}
],
"summary": {
"totalRevenue": 15500,
"growth": "15%"
}
}
)

response = await client.fill(name="sales_report", body=fill_request)

with open("filled_report.docx", "wb") as f:
f.write(response.content)

Best Practices

  • Field Naming: Use clear, consistent names for your form fields (e.g., camelCase like customerName).
  • Testing: Upload your template and inspect the metadata to ensure all fields are detected correctly.
  • Data Types: Ensure the data you send matches the expected type (e.g., booleans for checkboxes).
  • Configurations: Combine templates with Named Configurations to standardize the filling process if you have complex logic.