如何在Python 3.7中生成条形码

4

我正在使用Python 3.7,想要生成条形码,尝试使用pip install pyBarcode命令安装pyBarcode库,但出现以下错误:

找不到满足pyBarcode要求的版本(来自版本:) 没有找到匹配的分发版。

现在,我该如何为我的Python版本安装pyBarcode呢?

6个回答

7

首先安装正确的库:

pip install python-barcode

那么代码是:

#!/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()

这段代码会生成以下结果:

这里是图片描述


(注:该图片与IT技术有关,但具体内容无法确定)

它还显示了以下内容:“没有名为'barcode.writer'的模块”;“barcode”不是一个包。 - mitul rana
请回复,我需要解决方案。 - mitul rana
1
你使用的是哪个版本的Python?因为在我的电脑上这个程序可以正常运行。 - ΦXocę 웃 Пepeúpa ツ
谢谢你的回答。我找到了解决方案,现在它正常工作。 - mitul rana
请注意,EAN13只接受数字,并且有限制,例如不能是1234。如果要添加字母,请使用Code128而不是EAN13。从条形码中导入Code128。 - undefined

3

更新

这意味着它不支持Python 3.7。 尝试这个命令 pip install python-barcode

运行此示例以帮助您理解:

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')

我尝试过了,但是我无法从中获取'barcode.PROVIDED_BARCODES'。 - mitul rana
这段代码显示:没有名为'barcode.writer'的模块;'barcode'不是一个包。 - mitul rana
1
然后你将包pip安装到另一个版本的Python或虚拟环境中,并尝试在另一个版本中运行代码。我只是重新运行了代码,没有出现错误。 - Kostas Charitidis
我正在Anaconda提示符中运行pip install命令,并在“anaconda spyder”中运行我的代码。 - mitul rana
谢谢您提供的所有答案,现在我已经找到了解决我的问题的方法。它正常运行。 - mitul rana
显示剩余6条评论

1
安装treepoem包:

pip install treepoem
# or
python -m pip install treepoem

运行这段代码。
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")

enter image description here

更多细节


很棒的东西.. 特别是这篇文章。谢谢!需要一种可靠且简单的方法来生成ITFs(交错2of5)。 - Tomblarom

0

安装以下软件包:

pip install python-barcode

pip install Pillow

然后尝试此代码片段:

from barcode import EAN13
from barcode.writer import ImageWriter

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

注意: EAN13是生成条形码的格式之一,还有其他可用的格式。 EAN13需要12位数字,只允许输入数字。 12位数字作为输入,第13位数字是输入的校验和。

e.g:

输入:123412341234

输出:1234123412344


1
已经提供了更好的答案。 - Martin

0
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()

-1
import barcode

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

2
基本上它和这个回答是一样的。 - Gino Mempin
1
你好!虽然这段代码可能可以解决问题,但是如果您能够解释一下为什么以及如何解决问题,将会极大地提高您的回答质量,并有可能得到更多的赞同。请记住,您的回答不仅仅是为了回答当前的提问者,也是为未来的读者解决问题。请编辑您的回答并添加说明,以及相关的限制和假设条件。 - Brian61354270

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接