今天,为了实现解决方案,我已经对我的想法采取了行动。
使用ReportLab、pdfquery和PyPDF2,我试图自动化在PDF文档中数百页上生成条形码的过程。
每页都需要一个条形码。但是,如果一个页面的右上角有一个字母('A‘到'E'),那么它需要使用与前一页相同的条形码。右上角有字母的文件是具有类似信息的重复表单。
如果没有出现任何字母,则应该在该页上使用唯一的条形码号码(增加一个就可以了)。
我的代码似乎有效,但我有两个问题:
我似乎不知道为什么价值没有变化。有人有线索吗?
代码在这里:
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()发布于 2018-08-17 12:31:45
正如Kamil的回答所指出的,将有效数字限制在7
class Ean8BarcodeWidget(Ean13BarcodeWidget):
_digits=7
...
self.value=max(self._digits-len(value),0)*'0'+value[:self._digits]您可以更改您的编码方案或使用EAN 13条形码与Ean13BarcodeWidget,它有12位数可用。
发布于 2018-08-16 13:46:29
条形码没有改变的原因是您在eanbc.Ean8BarcodeWidget中提供了太长的整数。
根据EAN标准,EAN-8条形码长8位(7位+校验位)
解决方案:
如果您将barcode_value从12345670更改为1234560并运行您的脚本,您将看到条形码值随您的需要而增加,校验位数作为第八个数字追加。
有了这些信息,您应该只使用7位数来编码条形码中的信息。
https://stackoverflow.com/questions/51789298
复制相似问题