Apply Formatting to Numbers, Dates, and Text in Python
Leave feedback
Overview
GroupDocs.Assembly for Python via .NET provides comprehensive formatting capabilities for numbers, dates, and text in templates. You can use format strings (.NET format strings, whichever language calls the assembler) and built-in text transformation functions to control how data is displayed in your documents.
The main syntax elements are:
Format strings: <<[expression]:"format">> - apply .NET format strings
Text functions: upper, lower, caps, firstCap - transform text case
Here are the steps to apply formatting:
Use format strings in expression tags for numbers and dates
Apply text transformation functions for string formatting
Combine formatting with expressions and calculations
Test formatting to ensure desired output
Note
Format strings follow .NET formatting conventions, not Python’s. Use standard numeric format specifiers (C, N, F, etc.) and date/time format strings (yyyy-MM-dd, etc.). The value’s Python type decides what a format string can do: an int, float or Decimal takes a numeric format, a datetime or date takes a date format, and a str is written as it is.
A digit placeholder format such as "(###) ###-####" needs a number: keep the phone number an int in the data, not a string.
Formatting in Different Document Types
Formatting works consistently across document types:
fromdatetimeimportdatetimefromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerinvoice={"Amount":Decimal("1500.75"),"CreatedDate":datetime(2015,1,15,14,30,0),}assembler=DocumentAssembler()assembler.assemble_document("invoice-formatting.xlsx","invoice-formatting-report.xlsx",DataSourceInfo(invoice,"invoice"))print("Formatting applied in spreadsheet successfully.")
The spreadsheet template holds the same tags in its cells:
Amount
Created
<<[invoice.Amount]:"C">>
<<[invoice.CreatedDate]:"yyyy-MM-dd">>
Warning
Format strings and text functions are case-sensitive. Use uppercase letters for standard format specifiers (C, N, F, P) and follow .NET formatting conventions exactly.
Advanced Usage Topics
To learn more about custom format providers, culture-specific formatting, and advanced formatting scenarios, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: