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

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

Generate PDF containing multiple SVG barcodes

问题

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

  1. 我正在尝试生成一个包含所有条形码的PDF格式为SVG以下是迄今为止我编写的代码
  2. 来自code128库的导入Code128
  3. import csv
  4. 来自reportlab.graphics.shapes的导入GroupDrawing
  5. 来自reportlab.graphics的导入renderPDF
  6. 来自reportlab.lib.pagesizes的导入letter
  7. # SVG图像的缩放比例
  8. SVG_SCALE = 10
  9. # 读取CSV文件
  10. with open('products.csv', newline='') as csvfile:
  11. reader = csv.DictReader(csvfile)
  12. products = [(row['code'], row['name']) for row in reader]
  13. # 为每个产品生成条形码并添加到绘图中
  14. drawing = Drawing(width=letter[0], height=letter[1])
  15. x, y = 50, 50
  16. for product_code, product_name in products:
  17. # 使用code128库生成SVG条形码
  18. ean = Code128(product_code)
  19. barcode_svg = ean.svg(scale=SVG_SCALE)
  20. # 为条形码创建reportlab组并设置其变换
  21. barcode_group = Group()
  22. barcode_group.transform = (SVG_SCALE, 0, 0, SVG_SCALE, x, y)
  23. barcode_group.add(barcode_svg)
  24. drawing.add(barcode_group)
  25. # 在条形码下方添加产品名称
  26. drawing.addString(x + (barcode_svg.width * SVG_SCALE) / 2, y - 10, product_name, fontSize=10, textAnchor="middle")
  27. # 将光标移到下一个条形码位置
  28. y += 75
  29. if y > 750:
  30. y = 50
  31. x += 200
  32. # 将绘图渲染为PDF
  33. renderPDF.drawToFile(drawing, "barcodes.pdf", "Barcode Sheet")
  34. 我遇到的错误是`ImportError: 无法从'code128'导入名称'Code128'`
  35. 我找不到实现我想要的另一种方法有人能帮助我修复导入/代码或实现这一目标的其他方法吗
英文:

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

  1. from code128 import Code128
  2. import csv
  3. from reportlab.graphics.shapes import Group, Drawing
  4. from reportlab.graphics import renderPDF
  5. from reportlab.lib.pagesizes import letter
  6. # Scale of SVG images
  7. SVG_SCALE = 10
  8. # Read the CSV file
  9. with open('products.csv', newline='') as csvfile:
  10. reader = csv.DictReader(csvfile)
  11. products = [(row['code'], row['name']) for row in reader]
  12. # Generate barcodes for each product and add to drawing
  13. drawing = Drawing(width=letter[0], height=letter[1])
  14. x, y = 50, 50
  15. for product_code, product_name in products:
  16. # Generate SVG barcode using code128 library
  17. ean = Code128(product_code)
  18. barcode_svg = ean.svg(scale=SVG_SCALE)
  19. # Create a reportlab group for the barcode and set its transform
  20. barcode_group = Group()
  21. barcode_group.transform = (SVG_SCALE, 0, 0, SVG_SCALE, x, y)
  22. barcode_group.add(barcode_svg)
  23. drawing.add(barcode_group)
  24. # Add product name below barcode
  25. drawing.addString(x + (barcode_svg.width * SVG_SCALE) / 2, y - 10, product_name, fontSize=10, textAnchor="middle")
  26. # Move cursor to the next barcode position
  27. y += 75
  28. if y > 750:
  29. y = 50
  30. x += 200
  31. # Render drawing as PDF
  32. 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 包,则必须卸载。

  1. from barcode.codex import Code128
  2. from barcode.writer import SVGWriter
  3. import csv
  4. import os
  5. BARCODE_SCALE = 3
  6. if not os.path.exists('gen_svg'):
  7. os.makedirs('gen_svg')
  8. def generate_barcode_svg(code):
  9. barcode_svg = Code128(code, writer=SVGWriter())
  10. return barcode_svg.render()
  11. with open('products.csv', newline='') as csvfile:
  12. reader = csv.DictReader(csvfile)
  13. for row in reader:
  14. svg_name = row['code']
  15. product_string = row['code'] + ' : ' + row['name']
  16. barcode_svg = generate_barcode_svg(product_string)
  17. with open(f'gen_svg/{svg_name}.svg', 'w') as f:
  18. 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.

  1. from barcode.codex import Code128
  2. from barcode.writer import SVGWriter
  3. import csv
  4. import os
  5. BARCODE_SCALE = 3
  6. if not os.path.exists('gen_svg'):
  7. os.makedirs('gen_svg')
  8. def generate_barcode_svg(code):
  9. barcode_svg = Code128(code, writer=SVGWriter())
  10. return barcode_svg.render()
  11. with open('products.csv', newline='') as csvfile:
  12. reader = csv.DictReader(csvfile)
  13. for row in reader:
  14. svg_name = row['code']
  15. product_string = row['code'] + ' : ' + row['name']
  16. barcode_svg = generate_barcode_svg(product_string)
  17. with open(f'gen_svg/{svg_name}.svg', 'w') as f:
  18. 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:

确定