Whether you need to use a barcode scanner to scan the code when entering information, scan the code for identification when checking the information, or have other needs to generate barcodes, how can we quickly generate barcode images from the numbers and text columns in the table? This can be easily implemented with Python script in the SeaTable table, that is, a certain field in the table, such as ID number and phone number, is converted into the corresponding barcode and stored in the picture column. For example, if you have the following table, there are two columns of phone number and barcode image, you need to convert the phone number in each row into barcode and transfer it to the barcode image (BarcodeImage) column. How to achieve it?
Generating Barcodes with Python in SeaTable
New Python script
First, in the table, click the "Script" function to create a new Python script.
write code
Then write the following code in the script (if you need to run offline, you need to install the Python-barcode dependency package)
import os
import time
import barcode
from barcode.writer import ImageWriter
from seatable_api import Base, context
api_token = context.api_token or "859ad340d9a2b11b067c11f43078992e14853af5"
server_url = context.server_url or "https://cloud.seatable.cn"
TEXT_COL = "PhoneNum" # 需要转换成条码的列
BARCODE_IMAGE_COL = "BarcodeImage" # 图片列, 存储条码
TABLE_NAME = 'Table1'
BARCODE_TYPE = 'code128'
CUSTOM_OPTIONS = {
"module_width": 0.2, # 单个条纹的最小宽度, mm
"module_height": 15.0, # 条纹带的高度, mm
"quiet_zone": 6.5, # 图片两边与首尾两条纹之间的距离, mm
"font_size": 10, # 条纹底部文本的大小,pt
"text_distance": 5.0, # 条纹底部与条纹之间的距离, 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):
# 如果图片列中已有内容, 则跳过
if row.get(BARCODE_IMAGE_COL):
continue
# 如果电话号码列为空,则跳过
if not row.get(TEXT_COL):
continue
try:
row_id = row.get('_id')
msg = str(row.get(TEXT_COL))
# 生成条码对象
code_img = CODE(msg, writer=ImageWriter())
save_name = "%s_%s" % (row_id, get_time_stamp())
# 保存为图片并暂存
file_name = code_img.save("/tmp/%s" % save_name, options=CUSTOM_OPTIONS)
# 将图片上传至 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('Table1', row_id, row)
# 移除暂存文件
os.remove(file_name)
except Exception as error:
print("error occured during barcode generate", error)
continue
run script
Click the Run button with one click. After the script is run, the phone number in the table will automatically generate the corresponding barcode and save it to the barcode image column. Convenient.
Set barcode reference
In addition, if you also need to set the barcode, stripe length, and text length, etc., you can do so by adjusting the parameters in CUSTOM_OPTIONS in the script. Here are some examples:
default mode
module_width 1mm
module_height 5mm
quiet_zone 0.5mm
font_size 6pt
text_distance 2
Application scenarios
Converting the value of the text column into a barcode has been completed in the front. For example, a long list of admission ticket numbers has been converted into a barcode image. Let's see how to apply it.
Print barcodes for pasting answer sheets
After converting each student's admission ticket number into a barcode image, the test staff can click the barcode image column to download all of them and use the {column name} reference field to automatically name all barcodes, which is convenient for follow-up work.
Design the admission ticket page for identifying information
It is a common test work and usage scenario to design the student's name, photo, bar code of the admission ticket and other information into the admission ticket page, which is used for scanning code reading, checking, and entering information. The recording, processing, and application of such data can be quickly completed on the SeaTable table.
Add a page design plug-in to the form, and you can customize and design multiple page templates. The page can drag and drop field information such as text, numbers, and pictures in the row record. After the design is completed and saved, you can print the current record page or print all pages with one click, or you can set the content of the reference field using {field name}, and download it as PDF with one click.
Summarize
Through the above examples, have you discovered more flexibility and scenarios in the use of SeaTable? However, there is a problem that needs to be paid attention to in the generation and conversion of barcodes. One-dimensional barcodes do not support Chinese character conversion by nature, and only support basic Conversion of numbers, letters and some characters, but this is basically enough for daily business. In short, based on the rich functions of SeaTable, combined with Python scripts, it can help us achieve many unexpected effects, and it is a treasure software tool worth having and sharing.
Case reference
Python + SeaTable | Calculate the number of working days between two days
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。