Nazmul Hasan Pias
Nazmul Hasan Pias

Reputation: 33

Running Command Line from python to generate barcode

I'm trying to generate barcode using Zint Barcode Studio through command line . Using cmd.exe of windows I can do this using the following code :

F:\Zint>zint -o .\qr\datamatrix.png -b 20 --notext -d "Data to encode"

How can I run this command from python ??

I tried this , but no luck

from subprocess import call

dir1=r'F:\Zint\zint.exe'

cmd1='zint -o .\qr\datamatrix.png -b 20 --notext -d "Data to encode"'
rc=call(cmd1,cwd=dir1)

Please help me to run this command from python

Upvotes: 1

Views: 460

Answers (1)

Lost
Lost

Reputation: 1008

With subprocess.call() you enter your command as a list:

cmd1 = ['zint' '-o', '.\qr\datamatrix.png', '-b', '20', '--notext', '-d', 'data_to_encode_goes_here']

Note that in recent version of Python, subprocess.run() is the new standard (I don't see your version specified). It also takes a list as the command and not a string.

import os
import subprocess
out_dir = os.path.join(os.getcwd(), 'qr')
if not os.path.exists(out_dir):
    os.makedirs(out_dir)
out_file = os.path.join(out_dir, 'datamatrix.png')
cmd1 = ['zint' '-o', out_file, '-b', '20', '--notext', '-d', '"data_to_encode_goes_here"']
rc = subprocess.call(cmd1)

Try something like that

Upvotes: 1

Related Questions