Hey everybody,
here is a new tutorial how to create a barcode within SeaTable with python and a button column:
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…