Python python-barcode模块(生成条形码)

一、安装python-barcode

python -m pip install python-barcode -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

二、生成条形码三种方法

a、get(name,code,writer_mode,set_style,bottom_text)

必选项:【	1、name:条形码格式;
					2、code:条形码内容】
可选项:【
         1、ImageWriter或SVGWriter:要使用的写入程序;默认是SVGWriter
         2、 set_style:(字典)生成条形码图片的设置样式; 默认是default_writer_options这个变量
         3、bottom_text:条形码底部显示的文本
           】
# barcode模块能自动计算验证码
import matplotlib.pyplot as plt
import barcode
from barcode.writer import ImageWriter
code1 = barcode.get('ean13','1359748367234')  # 生成条形码
code1.save('erer') # 保存文件'erer'

b、get_barcode_class(name)

# 一个参数:条形码的格式
# barcode模块能自动计算验证码
import barcode
code = barcode.get_barcode_class('ean13')  # 返回的是一个函数对象
bar = code('1232123212321')  ## 实际还是调用get,一个参数:条形码内容
code.save('erer')

c、generate(name,code,writer,output,text) (快速生成条形码函数)

# name:要使用的条形码类型的名称。 (必选)
# code:要编码到条形码中的数据。(必选)
#  writer:要使用的写入程序(例如:ImageWriter或SVGWriter)。(可选:默认为SVGWriter)
# output:保存生成条形码的目标文件或路径。(必选)
# writer_options:传递到writer实例的选项。 (条形码的样式,字典类型)
#  text:要在条形码下呈现的文本。
# barcode模块能自动计算验证码
import barcode	
code1 = barcode.generate('ean13','2312123123132',
                         writer=barcode.writer.ImageWriter(),
                         output='barcode_png2',
                         writer_options={"background": "red"},
                         text='weerwr')
# 简:写:
# code2 = barcode.generate('ean13','2312123123132',barcode.writer.ImageWriter(),'barcode_png2',{"background": "red"},text='weerwr')

三、默认条形码样式设置

writer_options:选项如下:(默认选项)
  • default_writer_options = {
  • “module_width”: 0.2,
  • “module_height”: 15.0,
  • “quiet_zone”: 6.5,
  • “font_size”: 10,
  • “text_distance”: 5.0,
  • “background”: “white”,
  • “foreground”: “black”,
  • “write_text”: True,
  • “text”: “”,}

四、条形码生成格式:

 __BARCODE_MAP (源代码的格式选项:)
# "ean13": EAN13,
# "ean": EAN13,
# "gtin": EAN14,
# "ean14": EAN14,
# "jan": JAN,
# "upc": UPCA,
# "upca": UPCA,
# "isbn": ISBN13,
# "isbn13": ISBN13,
# "gs1": ISBN13,
# "isbn10": ISBN10,
# "issn": ISSN,
# "code39": Code39,
# "pzn": PZN,
# "code128": Code128,
# "itf": ITF,
# "gs1_128": Gs1_128,}

五、python_barcode源码执行流程


	def get(name, code=None, writer=None, options=None):
		"""Helper method for getting a generator or even a generated code.

		:param str name: The name of the type of barcode desired.
		:param str code: The actual information to encode. If this parameter is
			provided, a generated barcode is returned. Otherwise, the barcode class
			is returned.
		:param Writer writer: An alternative writer to use when generating the
			barcode.
		:param dict options: Aditional options to be passed on to the barcode when
			generating.
		"""
		# 注释翻译:
		# :param name:要使用的条形码类型的名称。
		# :param code:要编码到条形码中的数据。
		# :param writer:要使用的写入程序(例如:ImageWriter或SVGWriter)。
		# :param output:保存生成条形码的目标文件或路径。
		# :param writer_options:传递到writer实例的选项。
		# :param text:要在条形码下呈现的文本。
		options = options or {}
		try:
			barcode = __BARCODE_MAP[name.lower()]
		except KeyError:
			raise BarcodeNotFoundError(
				"The barcode {!r} you requested is not known.".format(name)
			)

		if code is not None:
			return barcode(code, writer, **options)
		else:
			return barcode

	def get_class(name):
		return get_barcode(name)

	get_barcode = get
	get_barcode_class = get_class
# 理解:
	1、get_barcode_class实际调用的是get_class,而get_class由返回的是get_barcode,get_barcode由调用的get,所以实际还是get生成条形码
	2、get_barcode_class()接受一个参数(条形码格式),返回的是get_barcode=get,所以get_barcode_class("ean13") == get("ean13")

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值