最近在研究盘点机盘点的问题,其中有一部分的商品没有的条形码,就想着自个生成条形码。参考了网上的教程,关于Excel批量生成条形码有大体如下三种:python
尝试了以上三种后,阅读openpyxl示例文档的时候,发现支持直接在Excel中插入图片,就有了以下代码:字体
from openpyxl import load_workbook,Workbook from openpyxl.drawing.image import Image from pystrich.code128 import Code128Encoder def excelCode128(file): ''' file:在文件目录下执行,excel的文件名。 A列是商品编码,B列是生成的条形码。 ''' wb = load_workbook(file) sheet = wb.active x = 0 for r in range(1,sheet.max_row+1): for i in range(1,3): if i == 1: # A列数据生成条码 var = sheet.cell(column=i,row=r).value filename = str(var)+'.PNG' Code128Encoder(str(var)).save(filename,bar_width=1) if i == 2: # B列数据插入条码 img = Image(filename) B = 'B'+str(r) sheet.add_image(img,B) x += 1 wb.save(file) print("执行成功!共计%s条!" %str(x))