【问题标题】:Running Command Line from python to generate barcode从 python 运行命令行以生成条形码
【发布时间】:2018-11-20 22:55:01
【问题描述】:

我正在尝试通过命令行使用Zint Barcode Studio 生成条形码。使用 windows 的 cmd.exe 我可以使用以下代码来做到这一点:

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

如何从 python 运行这个命令??

我试过了,但没有运气

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)

请帮我从 python 运行这个命令

【问题讨论】:

    标签: python cmd subprocess barcode


    【解决方案1】:

    使用 subprocess.call() 您可以将命令作为列表输入:

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

    请注意,在最新版本的 Python 中,subprocess.run() 是新标准(我没有看到您指定的版本)。它还接受一个列表作为命令,而不是一个字符串。

    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)
    

    试试类似的方法

    【讨论】:

    • Hi Lost , :Traceback(最近一次通话最后一次):文件“C:/Users/HP/Desktop/Card 05-06-2018/cmd_python.py”,第 16 行,在 rc=call(cmd1,cwd=dir1) 文件“C:\Program Files (x86)\Python36-32\lib\subprocess.py”,第 267 行,与 Popen(*popenargs, **kwargs) 作为 p 调用:文件“C:\Program Files (x86)\Python36-32\lib\subprocess.py”,第 707 行,在 init restore_signals, start_new_session) 文件“C:\Program Files (x86)\Python36 -32\lib\subprocess.py", 第 992 行, in _execute_child startupinfo) FileNotFoundError: [WinError 2] 系统找不到指定的文件
    • 可能是它无法解析您的目标文件名。也许import os 然后代替 .\qr\datamatrix.png 尝试放置:os.path.join(os.getcwd(), 'qr', 'datamatrix.png')
    • 嗨迷路了,没有运气,仍然是同样的错误。你能帮我写完整的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2022-07-20
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多