python,pylatex – How to Create Barcodes Using PyLaTeX Library in Python

pylatexpython

I'm trying to create a python program that creates numerous pdfs each containing a unique barcode using pylatex. Is there a simple way to do this?

Here is an example of how the latex document is being generated using pylatex:

with doc.create(MiniPage(width=r"\textwidth")) as page:
   with page.create(TextBlock(100, 0, 0)):
      page.append("**** Ten Thousand Dollars")

   with page.create(TextBlock(100, 0, 30)):
      page.append("COMPANY NAME")
      page.append("\nSTREET, ADDRESS")

   with page.create(TextBlock(100, 150, 40)):
      page.append()

   with page.create(TextBlock(80, 150, 0)):
      page.append("DATE")
      page.append(MediumText(bold("test")))
      page.append(HorizontalSpace("10mm"))

   with page.create(TextBlock(70, 150, 30)):
      page.append(MediumText(bold("$***** 10,000.00")))

   page.append(VerticalSpace("100mm"))

   doc.generate_pdf(doc_location, clean_tex=False)

I would like to figure out a way to append a barcode to the page.

Best Answer

The python code you included doesn't seem to produce the expected output, perhaps it is just my settings or I have not used it correctly. Regardless, please see this code which implements the makebarcode package and prints a barcode:

# -*- coding: utf-8 -*-

from pylatex import Document, Command, Package, MiniPage, TextBlock, MediumText, HorizontalSpace, VerticalSpace
from pylatex.utils import bold

def main():

    paper_size = "b5paper"
    geometry_options = {"paper": paper_size, "top": "1.5cm", "bottom": "1.5cm"}

    #------------------- Change barcode package options here --------------------------#
    makebarcode_options = {"code":"Code39", "X":".5mm", "ratio":"2.25","H": "1cm"}
    doc = Document('Testing', geometry_options=geometry_options)

    #------------------- Change barcode package here if you don't want makebarcode --------------------------#
    doc.packages.append(Package('makebarcode', options=makebarcode_options))

    with doc.create(MiniPage(width=r"\textwidth")) as page:
       with page.create(TextBlock(100, 0, 0)):
          page.append("**** Ten Thousand Dollars")

       with page.create(TextBlock(100, 0, 30)):
          page.append("COMPANY NAME")
          page.append("\nSTREET, ADDRESS")

       with page.create(TextBlock(100, 150, 40)):
          page.append("Test")

       with page.create(TextBlock(80, 150, 0)):
          page.append("DATE")
          page.append(MediumText(bold("test")))
          page.append(HorizontalSpace("10mm"))

       with page.create(TextBlock(70, 150, 30)):
          page.append(MediumText(bold("$***** 10,000.00")))
          page.append(VerticalSpace("100mm"))

    #------- Change bracket string to something random for unique barcodes ----------#
    doc.append(Command('barcode',["ZDENEK WAGNER"]))

    doc.generate_pdf(filepath=your_path_CHANGE, clean=True, clean_tex=False)
    doc.generate_tex()

if __name__ == "__main__":
    main()

This produces this broken output, I am not sure why but the code you included is not compiling properly:

enter image description here

I am sure you can debug why it is not working, I do not know and cannot profess to great knowledge of python, pylatex or latex as I am still learning! It produces your barcode though, you can change the string (ZDENEK WAGNER in this case which was the one from the makebarcode package documentation) to something random, I am sure this is trivial in python to create random or pseudorandom strings.

Change the package options (makebarcode_options has been set according to the makebarcode package documentation on page 2, have a look and change as needed) and or package in the places I have commented using the ctan link that Willie Wong posted in the comments if you wish. Hope this helps!