Generating PDF/UA from Excel templates
This guide explains how to generate an Excel workbook from a template and convert the generated file to PDF/UA with Nutrient Python SDK.
Use this workflow for invoices, financial reports, statements, and other workbooks that use a fixed worksheet design with variable content. PDF/UA helps generated PDFs meet accessibility requirements for assistive technologies such as screen readers.
Download sampleGenerating a workbook and exporting PDF/UA
Nutrient Python SDK handles the template processing and PDF/UA export workflow. The sample performs the following tasks:
- Open an Excel template.
- Load a JSON data model.
- Apply the data model to replace placeholders.
- Configure PDF/UA conformance.
- Export the generated document as a PDF.
Complete implementation
The following sections build the complete Python example. First, import the classes required for document processing, template editing, PDF conformance, and error handling:
from nutrient_sdk import Documentfrom nutrient_sdk import SpreadsheetEditorfrom nutrient_sdk import PdfConformancefrom nutrient_sdk import NutrientExceptionOpen the Excel template file. The context-manager(opens in a new tab) syntax closes the document when processing finishes:
def main(): try: with Document.open("input_spreadsheet.xlsx") as document:Create a SpreadsheetEditor instance for template processing:
editor = SpreadsheetEditor.edit(document)Read the JSON data model that contains the values for the template placeholders:
try: with open("input_spreadsheet_model.json", "r") as f: model = f.read() except IOError: print("Failed to read template model file") returnApply the template model, save the generated workbook, and close the editor:
editor.apply_template_model(model) editor.save() editor.close()Configure PDF/UA conformance and export the document as an accessible PDF:
# Configure PDF settings for PDF/UA conformance pdf_settings = document.settings.pdf_settings pdf_settings.conformance = PdfConformance.PDF_UA_1
document.export_as_pdf("output.pdf") print("Successfully generated accessible PDF from template") except NutrientException as e: print(f"Error: {e}")
if __name__ == "__main__": main()Conclusion
The template processing workflow consists of five steps:
- Open the Excel template.
- Create a spreadsheet editor.
- Apply the JSON template model.
- Configure PDF/UA conformance.
- Export the generated workbook as a PDF.
Use this pattern when you have a fixed Excel worksheet design and variable content from a data model.
Download the sample package to run this example as-is.