Tutorial: create a Barcode and save it as image to SeaTable

Hey everybody,

here is a new tutorial how to create a barcode within SeaTable with python and a button column:

barcode generation

Here is the complete python script I used. Just copy and paste it and adapt it to your needs. Scroll down if you want to read where I explain the different components of this script. By the way, you will need a free account at https://pdf.co/.

import os
import sys
import requests
import pytz
from seatable_api import Base, context
from datetime import datetime
import random
import string

# VARIABLES
server_url = context.server_url
api_token = context.api_token
table_name = 'Table1'
timezone = "Europe/Berlin"
timeformat = "%Y-%m-%d %H:%M"

pdfco_api = 'your-api-token-from-pdf.co'

# GET ROW-ID FROM BUTTON COLUMN OR FORCE IT
if context.current_row:
    row_id = context.current_row['_id']
    input_value = context.current_row['Value']
else:
    row_id = "dYKme7OrSEyX6K9cdBoaKg"
    input_value = "test value"

###########
# USUALLY YOU DON'T HAVE TO CHANGE ANYTHING BELOW THIS LINE
###########

# HELPER-FUNCTION TO GENERATE RANDOM FILE NAME
def randStr(chars = string.ascii_uppercase + string.digits, N=10):
	return ''.join(random.choice(chars) for _ in range(N))

# AUTHENTICATE
base = Base(api_token, server_url)
base.auth()


# GENERATE BARCODE
url = "https://api.pdf.co/v1/barcode/generate"
headers = {
  'Content-Type': 'application/json',
  'x-api-key': pdfco_api,
}
data = {
  'name': randStr(),
  'value': input_value,
  'type': 'Code128'
}
gen_barcode = requests.post(url, json=data, headers=headers).json()
# to DEBUG remove the following comment:
#print(gen_barcode)

# UPLOAD THE BARCODE FROM THE URL
upload = requests.get(gen_barcode['url'])
info_dict = base.upload_bytes_file(gen_barcode['name'], upload.content, file_type="image", replace="True")
# to DEBUG remove the following comment:
#print(info_dict)


# INSERT THE BARCODE INTO A COLUMN
img_url = info_dict.get('url')
data = {
  "barcode": [img_url],
  "barcode_ctime": str(datetime.now().astimezone(pytz.timezone(timezone)).strftime(timeformat))
}
result = base.update_row(table_name, row_id, data)
print(result)

print("Credits remaining at pdf.co: " + str(gen_barcode['remainingCredits']))

Allow me to explain the script in more details

get the input row-id and the input value for the barcode generation

# GET ROW-ID FROM BUTTON COLUMN OR FORCE IT
if context.current_row:
    row_id = context.current_row['_id']
    input_value = context.current_row['Value']
else:
    row_id = "dYKme7OrSEyX6K9cdBoaKg"
    input_value = "test value"

SeaTable needs to know in which row you would like to operate. Either you execute the python script with a button column or you execute it directly in the python editor. In both cases, you should get the “right” row-id:

  • if you use the button, it should work with the row where you pressed the button
  • if you execute it from the editor, you should define the row_id you want to use.

If you would like to know how to get a specific row id, here is the manual:


Generate a barcode with pdf.co

# GENERATE BARCODE
url = "https://api.pdf.co/v1/barcode/generate"
headers = {
  'Content-Type': 'application/json',
  'x-api-key': pdfco_api,
}
data = {
  'name': randStr(),
  'value': input_value,
  'type': 'Code128'
}
gen_barcode = requests.post(url, json=data, headers=headers).json()
# to DEBUG remove the following comment:
#print(gen_barcode)

The python scripts use the API from pdf.co to generate a barcode. It sends a post request that is authenticated via the API-key in the header. Change the type Code128 to another value if you want to get other barcode types. You can find the list of supported barcodes here:
Barcode Generator | PDF.co API


Get the barcode

# UPLOAD THE BARCODE FROM THE URL
upload = requests.get(gen_barcode['url'])
info_dict = base.upload_bytes_file(gen_barcode['name'], upload.content, file_type="image", replace="True")
# to DEBUG remove the following comment:
#print(info_dict)

# INSERT THE BARCODE INTO A COLUMN
img_url = info_dict.get('url')
data = {
  "barcode": [img_url],
  "barcode_ctime": str(datetime.now().astimezone(pytz.timezone(timezone)).strftime(timeformat))
}
result = base.update_row(table_name, row_id, data)
print(result)

SeaTable cannot upload the barcode directly into the SeaTable cell. Every file upload has to be done in two steps. First you download the file from the URL and in the next step you can update the base. It is also possible to upload a local file. You can find more information here:
https://seatable.github.io/seatable-scripts/python/files/

Just a hint: every time I create and save a barcode, I want to store the creation time in the base but I have to prepare the value:

  • I get the current date and time with datetime.now().
  • Then I change the timezone that the time is correct with astimezone().
  • Then I format the date that SeaTable can handle it with strftime()
  • to store it as string I use str()

pdf.co limit

print("Credits remaining at pdf.co: " + str(gen_barcode['remainingCredits']))

Well that is easy to explain. The free account of pdf.co has a limit. Therefore I just print this remaining credits that you can see it in the script log.

Have fun…

Of course you could also create QR-codes with links to any websites for example. Just replace the requested barcode type from Code128 to QRCode.

data = {
  'name': randStr(),
  'value': input_value,
  'type': 'QRCode'
}

Here is the result

2 Likes

Nicely done but with draw backs:

  1. You have to register and (maybe, depending on scale of your needs) pay yet another service provider, in order to get something done with the service provider, you already pay.
  2. organizations, which use somewaht shielded environments for security cannot use it, as data is sent to pdf.co, which should not be sent to the internet in the first place.

So after all: The sollution is only for those who can afford both, the pay and the security risk.

If you want to avoid paying for the barcode service, then use Free Online QR Code Generator: Create QR Codes to Encode Plain Text. This service is free.

If you would rather not send your data to another service provider, host your own service. There are jQuery plugins available like here for example: jQuery QR Code Plugins | jQuery Script

1 Like

For anyone on this same barcode journey-> there is a better way pitched in the Github examples. This uses no external API.

I have slightly adjusted the code, because there was one variable hardcoded on line 55.

import os
import time
import barcode
from barcode.writer import ImageWriter
from seatable_api import Base, context
"""
The python script shows how to transfer a slice of text into a barcode image and save it into
the image column
"""

api_token = context.api_token or "859ad340d9a2b11b067c11f43078992e14853af5"
server_url = context.server_url or "https://cloud.seatable.io"

TEXT_COL = "YourColumnName"  # column which is expected to be transferred into barcode
BARCODE_IMAGE_COL = "BarcodeImage"
TABLE_NAME = 'YourTableName'
BARCODE_TYPE = 'code128'

CUSTOM_OPTIONS = {
    "module_width": 0.2,       # width of single stripe of barcode, mm
    "module_height": 15.0,     # height of barcode, mm
    "quiet_zone": 6.5,         # padding size of first and last stripe to the image, mm
    "font_size": 10,           # font size of the text below the barcode,pt
    "text_distance": 5.0,      # distance between the text and the barcode, mm
}


CODE = barcode.get_barcode_class(BARCODE_TYPE)
base = Base(api_token, server_url)
base.auth()

def get_time_stamp():
    return str(int(time.time()*100000))

for row in base.list_rows(TABLE_NAME):
    # continue if the image is already shown up here
    if row.get(BARCODE_IMAGE_COL):
        continue

    try:
        row_id = row.get('_id')
        msg = str(row.get(TEXT_COL))

        # create a barcode object
        code_img = CODE(msg, writer=ImageWriter())
        save_name = "%s_%s" % (row_id, get_time_stamp())

        # temporarily saved as an image
        file_name = code_img.save("/tmp/%s" % save_name, options=CUSTOM_OPTIONS)

        # upload the barcode image to the base
        info_dict = base.upload_local_file(file_name, name=None, file_type='image', replace=True)
        img_url = info_dict.get('url')
        row[BARCODE_IMAGE_COL] = [img_url]
        base.update_row(TABLE_NAME, row_id, row)

        # remove the image file which is saved temporarily
        os.remove(file_name)
    except Exception as error:
        print("error occured during barcode generate", error)
        continue

source:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.