python批量生成條形碼的示例
在工作中,有時會遇見需要將數(shù)字轉(zhuǎn)換為條碼的問題,每次都需要打開條碼轉(zhuǎn)換的網(wǎng)站,一次次的轉(zhuǎn)換后截圖,一兩個還行,但是當(dāng)需要轉(zhuǎn)換的數(shù)量較多時,就會顯得特別麻煩,弄不好還會遺漏或者重復(fù),為了解決這個問題,使用python寫了以下腳本,用來解決此問題
1、安裝python-barcode庫和pillow庫
需要導(dǎo)入的python庫
import barcodefrom barcode.writer import ImageWriter
2.將需要轉(zhuǎn)換的條形碼數(shù)據(jù)保存到同級目錄下的 EAN.txt 內(nèi)讀取EAN.txt文件并保存到 EAN_list 列表中
EAN_list = []f = open(’EAN.txt’, ’r+’)while True: line = f.readline() if line == ’’: f.close() break else: line = eval(line) EAN_list.append(str(line))
3.使用for循環(huán),將列表中的所有內(nèi)容轉(zhuǎn)換成EAN條形碼圖片,并將轉(zhuǎn)換后的圖片保存到當(dāng)前目錄
for i in EAN_list: EAN = barcode.get_barcode_class('code128') ean = EAN(i, writer=ImageWriter()) ean.save(i + 'image')
我這里使用的是128的編碼,如果需要EAN8或者EAN13的編碼,只需要將
EAN = barcode.get_barcode_class('code128')中的‘code128’更換為 ‘EAN8’或者‘EAN13’ 便可
完整代碼如下:
import barcodefrom barcode.writer import ImageWriterEAN_list = []f = open(’EAN.txt’, ’r+’)while True: line = f.readline() if line == ’’: f.close() break else: line = eval(line) EAN_list.append(str(line))for i in EAN_list: EAN = barcode.get_barcode_class('code128') ean = EAN(i, writer=ImageWriter()) ean.save(i + 'image')
以上就是python批量生成條形碼的示例的詳細(xì)內(nèi)容,更多關(guān)于python 生成條形碼的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python如何讀寫CSV文件2. php測試程序運行速度和頁面執(zhí)行速度的代碼3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. 三個不常見的 HTML5 實用新特性簡介5. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁6. ajax請求添加自定義header參數(shù)代碼7. python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法8. 解決Python 進程池Pool中一些坑9. Python使用jupyter notebook查看ipynb文件過程解析10. 解決python腳本中error: unrecognized arguments: True錯誤
