How to Use Formula Fields in ZPL Label Designer

How to Use Formula Fields in ZPL Label Designer

While designing product labels in ZPL Label Designer, you may need more control over how product field values are displayed.

For example, you may want to:

  • Display dates in a specific format

  • Round prices or quantities

  • Add currency symbols

  • Limit the number of displayed characters

  • Combine multiple fields into a single label element

  • Show default values when fields are empty

Formula Fields make these customizations possible by allowing you to use Python expressions to transform and format data before it is printed on a label.

Before You Start

Formula fields are an advanced feature and are disabled by default.

To use formula fields:

  1. Open the ZPL Label Designer settings in Odoo (Developer Mode must be enabled).

  2. Enable the Formula Support option.

  3. Save the settings.

Once enabled, formula fields can be used in labels.

For security reasons, we recommend disabling this option after configuring the required labels.

Screenshot 2026-05-30 at 00.53.01.png

Creating a Formula Field

To create a formula field:

  1. Add a new Text or Field element to the label.

  2. Enter a Python expression in the field value and save your changes.

  3. Preview or print the label to verify the result

Example Product

To better demonstrate how formula fields work, let’s use a sample product with the following values:

Field

Value

Name

Organic Coffee Beans Premium Blend 1kg

Internal Reference

COF-PREMIUM-001

Barcode

8712345678901

Sales Price

19.95

Weight

1.25

The examples below demonstrate how these values can be transformed and formatted before being printed on a label.

Screenshot 2026-05-30 at 00.54.40.png

Common Formula Examples

The following examples demonstrate some of the most common use cases for formula fields.

Each example includes the formula expression and the resulting value that will be printed on the label.

The examples are based on the sample product shown above.

In formula expressions, values are typically accessed through the doc object. For example:

Expression

Field

doc.name

Product Name

doc.default_code

Internal Reference

doc.barcode

Barcode

doc.list_price

Sales Price

doc.weight

Weight

All formula expressions in ZPL Label Designer must be enclosed between %%FORMULA: and %%.

Example:

%%FORMULA:(doc.name or '').upper()%%

Example 1: Combine Multiple Fields

Formula fields can be used to combine multiple values into a single label element.

In this example, we combine the product’s internal reference and name. The expressions doc.default_code and doc.name refer to the Internal Reference and Name fields from the sample product shown above.

Formula:

%%FORMULA:(doc.default_code or '') + ' / ' + (doc.name or '')%%

Result:

COF-PREMIUM-001 / Organic Coffee Beans Premium Blend 1kg

Example 2: Round a Price Value

Formula fields can also be used to control how numeric values are displayed on a label.

In this example, the product’s sales price is rounded to a single decimal place.

Formula:

%%FORMULA:'{:.1f}'.format(doc.list_price or 0)%%

Result:

20.0

Since the original value is 19.95, it is rounded to 20.0 when displayed with a single decimal place.


Example 3: Add a Currency Symbol

Formula fields can combine dynamic values with static text.

In this example, we add a currency symbol before the product’s sales price.

Formula:

%%FORMULA:'$ ' + '{:.2f}'.format(doc.list_price or 0)%%

Result:

$ 19.95

Date Formatting Examples

Date formatting is one of the most common use cases for formula fields. By default, date and datetime values may not always be displayed in the desired format. Formula fields allow you to fully control how date values are printed on a label.

For the examples below, assume that the product’s Create Date value is:

2026-05-13 10:30:00

Example 4: Display a Date in DD.MM.YYYY Format

This format is commonly used in Europe and many other countries.

Formula:

%%FORMULA:doc.create_date.strftime('%d.%m.%Y')%%

Result:

13.05.2026

Example 5: Display a Date in YYYY-MM-DD Format

This format is commonly used in databases, integrations, and international environments.

Formula:

%%FORMULA:doc.create_date.strftime('%Y-%m-%d')%%

Result:

2026-05-13

Example 6: Display Date and Time

Formula fields can also be used to display both date and time values.

Formula:

%%FORMULA:doc.create_date.strftime('%d.%m.%Y %H:%M')%%

Result:

13.05.2026 10:30

Example 7: Display Time Only

In some scenarios, only the time portion of a datetime field is required.

Formula:

%%FORMULA:doc.create_date.strftime('%H:%M')%%

Result:

10:30

Common Date Formatting Patterns

The table below contains some of the most commonly used date and time formatting patterns.

Pattern

Result

%d.%m.%Y

13.05.2026

%Y-%m-%d

2026-05-13

%m/%d/%Y

05/13/2026

%H:%M

10:30

%d.%m.%Y %H:%M

13.05.2026 10:30

You can use any of these patterns inside the strftime() function to format date and datetime values according to your requirements.

Handling Empty Values

In real-world scenarios, some fields may not always contain a value. Formula fields can be used to display a custom fallback value instead of leaving the label blank.


Example 8: Display a Default Value for Empty Fields

In this example, a default value is displayed when the barcode field is empty.

Formula:

%%FORMULA:doc.barcode or 'NO BARCODE'%%

Result when a barcode exists:

8712345678901

Result when the barcode is empty:

NO BARCODE

Example 9: Display a Default Price

The same approach can be used with other fields.

Formula:

%%FORMULA:doc.list_price or 'PRICE NOT SET'%%

Result:

19.95

Result when the price is empty:

PRICE NOT SET

Text Formatting Examples

Formula fields can also be used to modify how text values are displayed on a label.


Example 10: Display Text in Uppercase

Formula:

%%FORMULA:(doc.name or '').upper()%%

Result:

ORGANIC COFFEE BEANS PREMIUM BLEND 1KG

Example 11: Limit the Number of Characters

This can be useful when label space is limited and only a portion of the value should be displayed.

Formula:

%%FORMULA:(doc.name or '')[:20]%%

Result:

Organic Coffee Beans

Additional Formula Examples

Below are several additional examples that may be useful when designing labels.

Display Text in Lowercase

Converts all characters to lowercase.

Formula:

%%FORMULA:(doc.name or '').lower()%%

Result:

organic coffee beans premium blend 1kg

Display Weight with a Unit

Adds a unit of measure after the value.

Formula:

%%FORMULA:'{:.2f}'.format(doc.weight or 0) + ' kg'%%

Result:

1.25 kg

Convert Weight to Grams

Formula:

%%FORMULA:str(int((doc.weight or 0) * 1000)) + ' g'%%

Result:

1250 g

Display a Custom Status

Conditional expressions can be used to display different text depending on a value.

Formula:

%%FORMULA:'HEAVY' if (doc.weight or 0) > 1 else 'LIGHT'%%

Result:

HEAVY

Display a Price Including Tax

Formula:

%%FORMULA:'{:.2f}'.format((doc.list_price or 0) * 1.20)%%

Result:

23.94

Display a Price with Thousands Separator

Useful for large values.

Formula:

%%FORMULA:'{:,.2f}'.format(doc.list_price or 0)%%

Example result:

1,234.50

Display the First 10 Characters of a Value

Formula:

%%FORMULA:(doc.name or '')[:10]%%

Result:

Organic Co

Final Notes

The examples in this article demonstrate only a small subset of what can be achieved with Formula Fields.

Formula fields support Python expressions, allowing you to perform a wide range of formatting, transformation, and calculation operations on label data. Most standard Python string, numeric, date, and conditional expressions should work as expected.

Feel free to adapt the examples shown above to fit your own business requirements and label designs.

If you encounter any issues or need assistance creating a specific formula, you can always contact us through our Support Portal and our team will be happy to help.

Related Posts