有没有Python库可以生成ean128条形码。我尝试了 'python-barcode' ,但在该库中没有 ean128 的选项。
最初问这个问题的人可能不会从中受益,但这适合那些仍在寻找答案的人。
注意:Code-128 与 GS1-128 不同
前几天我也被这个问题困扰了,终于找到了一个支持GS1-128(UCC/EAN-128)条码的python库。
我指的图书馆叫做“treepoem”。奇怪的名字我同意这就是它很难找到的原因。它是 BWIPP 库的包装器。它也依赖于 Ghostscript。关于名字;条形码 -> 树皮颂 -> 树诗。
重要链接:
1) 鬼脚本;下载并安装它,并将其“bin”和“lib”文件夹添加到
系统的路径变量。
https://www.ghostscript.com/download/gsdnld.html
2)树诗
https://pypi.org/project/treepoem/
3)全码;在存储库中,您可以找到所有支持的条形码。
https://github.com/adamchainz/treepoem/blob/master/treepoem/data.py
import treepoem
image = treepoem.generate_barcode(
barcode_type='gs1-128', # One of the BWIPP supported codes.
data='(01)14-digit-product-code')
image.convert('1').save('barcode.png')
如果其他人搜索此内容(在 python 中搜索 GS1-128 EAN128 代码时的第一个答案):
您可以直接使用 python-barcode (barcode.codex.Gs1_128) 生成它们。但为了生成下面的人类可读文本,您需要重写 get_fullcode 函数以在括号中显示应用程序标识符。这是工作示例:
import barcode
from barcode.writer import ImageWriter
class GS1_Barcode(barcode.codex.Gs1_128):
# Note barcode.codex.Gs1_128 is almost as barcode.codex.Code128 but
# adds the FNC1_CHAR (u'\xf1') prefix
def __init__(self,gtin14,batch,serial=None,*args,**kwargs):
self.gtin14 = gtin14 #often 0+GTIN13/EAN13
self.batch = batch
self.serial = serial
if serial :
## AI 10 (batch) is of variable length, so a FNC1_CHAR char must be added if followed by another AI
str_barcode = f"01{gtin14}10{batch}{self.FNC1_CHAR}21{serial}"
else:
str_barcode = f"01{gtin14}10{batch}"
return super().__init__(code=str_barcode,*args,**kwargs)
def get_fullcode(self):
"""
Return human readable version of code (written below the barcode)
Returns
-------
str
human readable.
"""
if self.serial:
return f"(01){self.gtin14}(10){self.batch}(21){self.serial}"
else:
return f"(01){self.gtin14}(10){self.batch}"
str_gtin14 = "00614141007349" #often 0+GTIN13/EAN13
str_batch = "64E0001"
str_serial = "02"
opts = {"module_width":0.3,
"module_height":6.0,
"font_size":10,
"text_distance":5,
}
code128 = GS1_Barcode(str_gtin14,str_batch,serial=str_serial,
writer=ImageWriter())
fullname = code128.save('D:\\10_Projects\\CodeBarre\\barcode',
options=opts)