[en] Use Python scripting in NiceLabel

La traducción oficial aún no está disponible.

[en] Problem

[en] Why use Python programming language?

[en] Python helps you perform complex calculations and build the data streams that fit into your label specifications, or make decisions and take control over your printing process.

[en] 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.

[en] Python is a programming language that provides the following:

  • [en] 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.

  • [en] 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.

  • [en] 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.

[en] There is a large knowledge base of ready-made code available on the Internet that you can include in your solution.

[en] Solution

[en] Prerequisites

[en] If you want to run the core Python and use functions from .NET Framework assemblies, you do not have to install any extra components.

[en] 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.

[en] 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 the correct syntax.

[en] We recommend using the assemblies available in .NET Framework.

[en] How to enable Python as a scripting language in NiceLabel?

[en] Enable Python in different NiceLabel modules:

  • [en] NiceLabel Desktop Designer (label design). When you design a label template, Python is available as a function Python Script.

  • [en] NiceLabel Desktop 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.

  • [en] 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.

[en] Tips and tricks using Python

  • [en] 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.

  • [en] 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

  • [en] 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.

    [en] Python commands are case-sensitive. For example, to use the "if" command, you have to enter it as 'if', not 'IF' or 'If'

  • [en] When using variable values in Designer PowerForms, refer to the property ".Value", not just to the variable name.

    [en] For example: to set the value “123” to variable var1, do the following:

    var1.Value = '123'

    [en] Note the capital “V” in “Value”, the property is case sensitive.

  • [en] Indenting code is important, for example when using the "if" command.

  • [en] 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.

    [en] Generate value in one script:

    import sys
    sys.global_holder = complex_object

    [en] Use value in another:

    import sys
    complex_object = sys.global_holder