Using Python scripting in NiceLabel
Article ID: 204 - Updated: Feb 16, 2021 - Products: Automation, Control Center, Designer PowerForms, Designer Pro, SDK, Web printing - Version: V2019, V2017 - Category: Connectivity and Database
Why use Python programming language?
Python helps you perform complex calculations and build the data streams that fit into your label specifications, or make decisions and take control over printing process.
NiceLabel ships with the embedded IronPython - this is .NET variant of the Python language (you can also call .NET functions in your code). IronPython is based on Python 2.7.X branch.
Python is a programming language that provides the following:
- Enhanced data-manipulation ability, allows you to comply with complex check-digit algorithms, perform custom calculations, data comparison, obtain data from databases, read/write data to files, and similar.
- Decision-making ability, lets you decide if and when to execute certain actions (print a label, write to file), you can also decide if the label objects print or not.
- Executing functions from the add-on libraries. You can call the majority of the Python 2.7.X functions. In this case, you have to manually install Python 2.7.X and PyWin32 extensions. You can also call the functions from .NET assemblies.
There is a large knowledge base of ready-made code available on the Internet that you can include in your solution.
If you want to run the core Python and use functions from .NET Framework assemblies, you do not have to install any extra component.
If you want to use functions from external Python modules, you have to install a desktop variant of Python 2.7.X and PyWin32 extensions. These two components are not automatically installed with NiceLabel. For more information on installation, see this KB article.
NOTE: IronPython does not support the Python modules that are written in C. When you try to use such a module, you get error messages even if your code has correct syntax.
We recommend using the assemblies available in .NET Framework.
How to enable Python as a scripting language in NiceLabel?
Enable Python in different NiceLabel modules:
- NiceLabel Designer (label design). When you design a label template, Python is available as a function Python Script.
- NiceLabel Designer (solution design). When you design forms in your solutions, you can associate each form with one scripting engine (VBScript or Python). All conditions you use in the form and all Execute Script actions use the selected engine. Select the script engine in the Form properties > Additional Settings > Form scripting language.
- NiceLabel Automation Builder. Each trigger in the configuration can associate with one scripting engine (VBScript or Python). All conditions you use in the trigger and all Execute Script actions use the selected engine. Select the script engine in the Trigger properties > Settings > Other > Scripting language.
Tips and tricks using Python
- You cannot use spaces in the names of variables, but you can use underscores “_”. You cannot use periods in the names of variables. The names of variables can contain alphanumerical characters and underscore, and the name must not start with a digit.
- The following reserved words cannot be used as variable names: and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield
- Python is case sensitive. Use the variable names exactly as you have defined them in the form application. To make things simpler, use only low-case letters for variable names.
Python commands are case sensitive. For example, to use "if" command, you have to enter it as 'if', not 'IF' or 'If'
- When using variable values in PowerForms, refer to the property ".Value", not just to the variable name.
For example: to set the value “123” to variable var1, do the following:
var1.Value = '123'
Note the capital “V” in “Value”, the property is case sensitive.
- Indenting code is important, for example when using the "if" command.
- Transferring the complex data structures from one Python script to another. The data is typically either a python dict object or some complex .NET object. Since Nicelabel variables are only text, you can't use them to store these kinds of objects. The "sys" object persists across the scripts you are running. When you need to persist an object, just add it to the "sys" object.
Generate value in one script:
sys.global_holder = complex_object
Use value in another:
complex_object = sys.global_holder