gpt4 book ai didi

python - 使用 ReportLab 批量生成条形码

转载 作者:太空狗 更新时间:2023-10-29 21:48:16 29 4
gpt4 key购买 nike

Yesterday, I asked a question that was perhaps too broad.

今天,我已经按照我的想法付诸行动,努力实现解决方案。

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

每一页都需要有一个条形码。但是,如果页面右上角有一个字母(“A”到“E”),则它需要使用与上一页相同的条形码。右上角带有字母的文件是具有相似信息的重复表格。

如果不存在字母,则应在该页面上使用唯一的条形码编号(递增 1 即可)。

我的代码似乎可以工作,但我有两个问题:

  1. 条形码移动非常轻微(小问题)。
  2. 条形码值不会改变(主要问题)。所有页面只设置第一个条码编号。

我似乎无法说出为什么值没有改变。有人知道吗?

代码在这里:

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

And here is letters-test.pdf

最佳答案

正如 Kamil Nicki 的回答所指出的,Ean8BarcodeWidget limiting effective digits to 7 :

class Ean8BarcodeWidget(Ean13BarcodeWidget):
_digits=7
...
self.value=max(self._digits-len(value),0)*'0'+value[:self._digits]

您可以更改编码方案或将 EAN 13 条码与 Ean13BarcodeWidget 一起使用,它有 12 位可用。

关于python - 使用 ReportLab 批量生成条形码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51789298/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com