首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python报告实验室条形码code128大小

python报告实验室条形码code128大小
EN

Stack Overflow用户
提问于 2015-02-26 23:00:25
回答 4查看 7.5K关注 0票数 3

我想创建一个带有code128条形码的标签(57*32 to )。这一切都很好,但条形码太小=(我如何才能使这个条形码更大?

from reportlab.graphics.barcode import code128
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas

c = canvas.Canvas("test.pdf")
c.setPageSize((57*mm,32*mm))
barcode = code128.Code128("123456789")
barcode.drawOn(c, 2*mm, 20*mm)
c.showPage()
c.save()
EN

回答 4

Stack Overflow用户

发布于 2017-01-28 12:16:02

通过在GitHub上查找reportlab的源代码,我发现这个条形码对象有一个宽度属性。通过简单地使用

canvas.saveState()
canvas.translate(2*cm, 3*cm) # bottom left corner of the barcode
canvas.scale(15*cm / barcode.width, 2*cm / barcode.height) # resize (15 cm and 2 cm)
barcode.drawOn(canvas, 0, 0)
canvas.restoreState()

你可以获得你想要的确切大小。

票数 3
EN

Stack Overflow用户

发布于 2015-03-19 00:08:48

您可以通过barHeight和barWidth来设置条码大小:

barcode = code128.Code128("123456789",barHeight=.9*inch,barWidth = 1.2)
票数 2
EN

Stack Overflow用户

发布于 2018-08-18 08:44:24

条形码的总宽度是bar_width * total_char_widths +静音空间,所以..可以通过以下方式确定正确的barWidth

from reportlab.graphics.barcode import code128
final_size = 100 # arbitrary
# setting barWidth to 1
initial_width = .1

barcode128 = code128.Code128(barcode_value, humanReadable=True, barWidth=initial_width,
                             barHeight=1)
# creates the barcode, computes the total size
barcode128._calculate()
# the quiet space before and after the barcode
quiet = barcode128.lquiet + barcode128.rquiet
# total_wid = barWidth*charWid + quiet_space
# char_wid = (total_width - quiet) / bar_width
char_width = (barcode128._width - quiet) / barcode128.barWidth
# now that we have the char width we can calculate the bar width
bar_width = (final_size - quiet) / char_width
# set the new bar width
barcode128.barWidth = bar_width
# re-calculate
barcode128._calculate()

# draw the barcode on the canvas
wid, hgt = barcode128._width, barcode128._height
x_pos = y_pos = final_size # arbitrary
barcode128.drawOn(your_canvas, x_pos, y_pos)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28745703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档