简体   繁体   English

从python运行命令行以生成条形码

[英]Running Command Line from python to generate barcode

I'm trying to generate barcode using Zint Barcode Studio through command line . 我正在尝试通过命令行使用Zint Barcode Studio生成条形码。 Using cmd.exe of windows I can do this using the following code : 使用Windows的cmd.exe,可以使用以下代码执行此操作:

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

How can I run this command from python ?? 如何从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 请帮助我从python运行此命令

With subprocess.call() you enter your command as a list: 使用subprocess.call(),您可以在列表中输入命令:

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). 请注意,在最新版本的Python中,subprocess.run()是新标准(我看不到指定的版本)。 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 试试这样的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM