生成包含多个SVG条形码的PDF。

huangapple go评论71阅读模式
英文:

Generate PDF containing multiple SVG barcodes

问题

以下是您提供的代码的翻译部分:

我正在尝试生成一个包含所有条形码的PDF格式为SVG以下是迄今为止我编写的代码

来自code128库的导入Code128
import csv
来自reportlab.graphics.shapes的导入GroupDrawing
来自reportlab.graphics的导入renderPDF
来自reportlab.lib.pagesizes的导入letter

# SVG图像的缩放比例
SVG_SCALE = 10

# 读取CSV文件
with open('products.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    products = [(row['code'], row['name']) for row in reader]

# 为每个产品生成条形码并添加到绘图中
drawing = Drawing(width=letter[0], height=letter[1])
x, y = 50, 50
for product_code, product_name in products:
    # 使用code128库生成SVG条形码
    ean = Code128(product_code)
    barcode_svg = ean.svg(scale=SVG_SCALE)

    # 为条形码创建reportlab组并设置其变换
    barcode_group = Group()
    barcode_group.transform = (SVG_SCALE, 0, 0, SVG_SCALE, x, y)
    barcode_group.add(barcode_svg)
    drawing.add(barcode_group)

    # 在条形码下方添加产品名称
    drawing.addString(x + (barcode_svg.width * SVG_SCALE) / 2, y - 10, product_name, fontSize=10, textAnchor="middle")

    # 将光标移到下一个条形码位置
    y += 75
    if y > 750:
        y = 50
        x += 200

# 将绘图渲染为PDF
renderPDF.drawToFile(drawing, "barcodes.pdf", "Barcode Sheet")

我遇到的错误是`ImportError: 无法从'code128'导入名称'Code128'`

我找不到实现我想要的另一种方法有人能帮助我修复导入/代码或实现这一目标的其他方法吗
英文:

I am trying to generate a PDF containing all the barcodes in SVG format. Here is the code I've written so far.

from code128 import Code128
import csv
from reportlab.graphics.shapes import Group, Drawing
from reportlab.graphics import renderPDF
from reportlab.lib.pagesizes import letter

# Scale of SVG images
SVG_SCALE = 10

# Read the CSV file
with open('products.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    products = [(row['code'], row['name']) for row in reader]

# Generate barcodes for each product and add to drawing
drawing = Drawing(width=letter[0], height=letter[1])
x, y = 50, 50
for product_code, product_name in products:
    # Generate SVG barcode using code128 library
    ean = Code128(product_code)
    barcode_svg = ean.svg(scale=SVG_SCALE)

    # Create a reportlab group for the barcode and set its transform
    barcode_group = Group()
    barcode_group.transform = (SVG_SCALE, 0, 0, SVG_SCALE, x, y)
    barcode_group.add(barcode_svg)
    drawing.add(barcode_group)

    # Add product name below barcode
    drawing.addString(x + (barcode_svg.width * SVG_SCALE) / 2, y - 10, product_name, fontSize=10, textAnchor="middle")

    # Move cursor to the next barcode position
    y += 75
    if y > 750:
        y = 50
        x += 200

# Render drawing as PDF
renderPDF.drawToFile(drawing, "barcodes.pdf", "Barcode Sheet")

The error I get is ImportError: cannot import name 'Code128' from 'code128'

I can't find another way to achieve what I'm after. Can someone please help me fix the imports/code or another way of achieving this?

答案1

得分: 0

首先安装 python_barcode 包。不要与 barcode 包混淆。如果已安装 barcode 包,则必须卸载。

from barcode.codex import Code128
from barcode.writer import SVGWriter
import csv
import os

BARCODE_SCALE = 3

if not os.path.exists('gen_svg'):
    os.makedirs('gen_svg')

def generate_barcode_svg(code):
    barcode_svg = Code128(code, writer=SVGWriter())
    return barcode_svg.render()

with open('products.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        svg_name = row['code']
        product_string = row['code'] + ' : ' + row['name']
        barcode_svg = generate_barcode_svg(product_string)
        with open(f'gen_svg/{svg_name}.svg', 'w') as f:
            f.write(barcode_svg.decode('utf-8'))
英文:

Firstly install python_barcode package. Don't get confused with barcode package. barcode package must be un-installed if already isntalled.

from barcode.codex import Code128
from barcode.writer import SVGWriter
import csv
import os

BARCODE_SCALE = 3

if not os.path.exists('gen_svg'):
    os.makedirs('gen_svg')


def generate_barcode_svg(code):
    barcode_svg = Code128(code, writer=SVGWriter())
    return barcode_svg.render()


with open('products.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        svg_name = row['code']
        product_string = row['code'] + ' : ' + row['name']
        barcode_svg = generate_barcode_svg(product_string)
        with open(f'gen_svg/{svg_name}.svg', 'w') as f:
            f.write(barcode_svg.decode('utf-8'))

huangapple
  • 本文由 发表于 2023年2月27日 10:14:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定