首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用ReportLab批量生成条形码

利用ReportLab批量生成条形码
EN

Stack Overflow用户
提问于 2018-08-10 15:02:32
回答 2查看 1.1K关注 0票数 7

昨天,我问了一个可能过于宽泛的问题。

今天,为了实现解决方案,我已经对我的想法采取了行动。

使用ReportLabpdfqueryPyPDF2,我试图自动化在PDF文档中数百页上生成条形码的过程。

每页都需要一个条形码。但是,如果一个页面的右上角有一个字母('A‘到'E'),那么它需要使用与前一页相同的条形码。右上角有字母的文件是具有类似信息的重复表单。

如果没有出现任何字母,则应该在该页上使用唯一的条形码号码(增加一个就可以了)。

我的代码似乎有效,但我有两个问题:

  1. 条形码移动得如此轻微(小问题)。
  2. 条形码值不会改变(主要问题)。在所有页面上只设置第一个条形码。

我似乎不知道为什么价值没有变化。有人有线索吗?

代码在这里:

代码语言:javascript
复制
import pdfquery
import os
from io import BytesIO
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.graphics.barcode import eanbc
from reportlab.graphics.shapes import Drawing 
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF

pdf = pdfquery.PDFQuery("letters-test.pdf")

total_pages = pdf.doc.catalog['Pages'].resolve()['Count']
print("Total pages", total_pages)

barcode_value = 12345670

output = PdfFileWriter()

for i in range(0, total_pages):
    pdf.load(i) # Load page i into memory
    duplicate_letter = pdf.pq('LTTextLineHorizontal:in_bbox("432,720,612,820")').text()

    if duplicate_letter != '':
        print("Page " + str(i+1) + " letter " + str(duplicate_letter))
        print(barcode_value)
        packet = BytesIO()
        c = canvas.Canvas(packet, pagesize=letter)

        # draw the eanbc8 code
        barcode_eanbc8 = eanbc.Ean8BarcodeWidget(str(barcode_value))
        bounds = barcode_eanbc8.getBounds()
        width = bounds[2] - bounds[0]
        height = bounds[3] - bounds[1]
        d = Drawing(50, 10)
        d.add(barcode_eanbc8)
        renderPDF.draw(d, c, 400, 700)
        c.save()

        packet.seek(0)

        new_pdf = PdfFileReader(packet)

        # read existing PDF
        existing_pdf = PdfFileReader(open("letters-test.pdf", "rb"))

        # add the "watermark" (which is the new pdf) on the existing page
        page = existing_pdf.getPage(i)
        page.mergePage(new_pdf.getPage(0))
        output.addPage(page)

    else:
        # increment barcode value
        barcode_value += 1
        print("Page " + str(i+1) + " isn't a duplicate.")
        print(barcode_value)
        packet = BytesIO()
        c = canvas.Canvas(packet, pagesize=letter)

        # draw the eanbc8 code
        barcode_eanbc8 = eanbc.Ean8BarcodeWidget(str(barcode_value))
        bounds = barcode_eanbc8.getBounds()
        width = bounds[2] - bounds[0]
        height = bounds[3] - bounds[1]
        d = Drawing(50, 10)
        d.add(barcode_eanbc8)
        renderPDF.draw(d, c, 420, 710)
        c.save()

        packet.seek(0)

        new_pdf = PdfFileReader(packet)

        # read existing PDF
        existing_pdf = PdfFileReader(open("letters-test.pdf", "rb"))

        # add the "watermark" (which is the new pdf) on the existing page

        page = existing_pdf.getPage(i)
        page.mergePage(new_pdf.getPage(0))
        output.addPage(page)

     # Clear page i from memory and re load.
     # pdf = pdfquery.PDFQuery("letters-test.pdf")


outputStream = open("newpdf.pdf", "wb")
output.write(outputStream)
outputStream.close()

这是字母-test.pdf

EN

回答 2

Stack Overflow用户

发布于 2018-08-17 12:31:45

正如Kamil的回答所指出的,将有效数字限制在7

代码语言:javascript
复制
class Ean8BarcodeWidget(Ean13BarcodeWidget):
    _digits=7
...
self.value=max(self._digits-len(value),0)*'0'+value[:self._digits]

您可以更改您的编码方案或使用EAN 13条形码与Ean13BarcodeWidget,它有12位数可用。

票数 3
EN

Stack Overflow用户

发布于 2018-08-16 13:46:29

条形码没有改变的原因是您在eanbc.Ean8BarcodeWidget中提供了太长的整数。

根据EAN标准,EAN-8条形码长8位(7位+校验位)

解决方案:

如果您将barcode_value12345670更改为1234560并运行您的脚本,您将看到条形码值随您的需要而增加,校验位数作为第八个数字追加。

有了这些信息,您应该只使用7位数来编码条形码中的信息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51789298

复制
相关文章

相似问题

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