简体   繁体   English

如何在python 3.7中生成条码

[英]how to generate barcode in python 3.7

I am using python 3.7, for barcode generation I am trying to install pyBarcode library using pip install pyBarcode '.我正在使用 python 3.7,对于条形码生成,我正在尝试使用pip install pyBarcode ' 安装 pyBarcode 库。 but it shows the following error:但它显示以下错误:

Could not find a version that satisfies the requirement pyBarcode (from versions: ) no matching distribution found for pyBarcode找不到满足 pyBarcode 要求的版本(来自版本:)找不到 pyBarcode 的匹配分布

Now, how can I install pyBarcode for my Python version?现在,如何为我的 Python 版本安装pyBarcode

1st install the right lib:第一安装正确的库:

pip install python-barcode

then code:然后代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import barcode
from barcode.writer import ImageWriter

def testEan():
    EAN = barcode.get_barcode_class('ean13')
    ean = EAN(u'123456789011', writer=ImageWriter())
    fullname = ean.save('my_ean13_barcode')

if __name__ == '__main__':
    testEan()

this code produces这段代码产生

在此处输入图片说明

UPDATE更新

That means it does not support python 3.7.这意味着它不支持 python 3.7。 Try this pip install python-barcode试试这个pip install python-barcode

Run this example to help you understand:运行这个例子来帮助你理解:

import barcode
from barcode.writer import ImageWriter
from barcode import generate

print(barcode.PROVIDED_BARCODES)
EAN = barcode.get_barcode_class('ean13')
ean = EAN('5901234123457')
fullname = ean.save('ean13_barcode')
ean = EAN('5901234123457', writer=ImageWriter())

f = open('barcode.svg', 'wb')
ean.write(f)

name = generate('EAN13', '5901234123457', output='barcode_svg')
generate('EAN13', '5901234123457', writer=ImageWriter(), output='barcode')
import barcode
from barcode.writer import ImageWriter
from barcode import generate
def testEan():
    EAN = barcode.get_barcode_class('ean13')
    ean = EAN(u'5901234123457', writer=ImageWriter())
    fullname = ean.save('ean13_barcode')
    u'ean13_barcode.png'
    name = generate('EAN13', u'5901234123457', output='barcode_svg')
    print(name)
if __name__ == '__main__':
    testEan()

Install treepoem package:安装 treepoem 包:

pip install treepoem
# or
python -m pip install treepoem

Run this code运行此代码

import treepoem
image = treepoem.generate_barcode(
    barcode_type="code128",  # One of the BWIPP supported codes.
    # barcode_type="qrcode",  
    # One of the BWIPP supported codes.
    # barcode_type="interleaved2of5",  # One of the BWIPP supported codes.
    # barcode_type="code128",  # One of the BWIPP supported codes.
    #    # barcode_type="isbn",  # One of the BWIPP supported codes.
    #    # data="978-3-16-148410-0",
    #   barcode_type="code128",  # One of the BWIPP supported codes.
    #   barcode_type="micropdf417",  # One of the BWIPP supported codes.
    #   barcode_type="ean13",  # One of the BWIPP supported codes.
    data="Your String -978316148fsd4100",
)
image.convert("1").save("output_qrcode_or_barcode.png")

在此处输入图片说明

More Detalils 更多详情

install these packages:安装这些包:

pip install python-barcode pip 安装 python-barcode

pip install Pillow pip 安装枕头

then try this code snippet:然后试试这个代码片段:

from barcode import EAN13
from barcode.writer import ImageWriter

with open('sample.png', 'wb') as f:
    e = EAN13('123412341234', writer=ImageWriter())
    e.write(f)

Note: EAN13 is one of the formats to generate, there are other formats available as well.注意:EAN13 是要生成的格式之一,还有其他格式可用。 EAN13 expects 12 digits, and only numeric input is allowed. EAN13 需要 12 位数字,并且只允许输入数字。 The 12 digits is taken as input and the 13th digit is checksum of the input. 12 位数字作为输入,第 13 位数字是输入的校验和。

eg:例如:

input: 123412341234输入:123412341234

output:1234123412344 output:1234123412344

import barcode

hr = barcode.get_barcode_class('ean13')
Hr = hr('1234567891012')
qr = Hr.save('123')

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

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