Electronic stock management. -- not updated anymore
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

barcode_utils.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from exceptions import ValueError
  2. import barcode
  3. from barcode.writer import ImageWriter
  4. import os
  5. BC_SIZES = {
  6. "normal": (10.0, 10),
  7. "medium": (5.0, 10),
  8. "mini": (2.0, 8),
  9. "mini_notext": (2.0, 0)
  10. }
  11. def generate_ean13(part_type, part_id, dir_out):
  12. """
  13. 00 category ID
  14. .. 000 000 000 00 part number
  15. -----------------
  16. 00 000 000 000 00
  17. """
  18. _type = "%s" % part_type
  19. _type = _type.zfill(2)
  20. _id = "%s" % part_id
  21. barcode_str = "{0}".format(_type)
  22. barcode_str += _id.zfill(11)
  23. if len(barcode_str) > 13:
  24. raise ValueError("barcode length exceded 13")
  25. dir_output = os.path.join(dir_out, "barcodes")
  26. def generate(txt, size):
  27. opts = dict(module_width=0.2, module_height=size[0], quiet_zone=1, font_size=size[1], text_distance=1)
  28. ean = barcode.get('ean13', barcode_str, writer=ImageWriter())
  29. out = os.path.join(dir_output, _type, "{0}_{1}".format(txt, barcode_str))
  30. if not os.path.isdir(os.path.join(dir_output, _type)):
  31. os.makedirs(os.path.join(dir_output, _type))
  32. ean.save(out, opts)
  33. generate('normal', BC_SIZES['normal'])
  34. generate('medium', BC_SIZES['medium'])
  35. generate('mini', BC_SIZES['mini'])
  36. generate('mini_notext', BC_SIZES['mini_notext'])
  37. return {"Barcode": barcode_str,
  38. "Out": dir_output,
  39. "Status": "OK",
  40. "Size": len(barcode_str),
  41. "GeneratedSizes": ['normal', 'mini']}