python批量生成條形碼的示例
在工作中,有時(shí)會遇見需要將數(shù)字轉(zhuǎn)換為條碼的問題,每次都需要打開條碼轉(zhuǎn)換的網(wǎng)站,一次次的轉(zhuǎn)換后截圖,一兩個(gè)還行,但是當(dāng)需要轉(zhuǎn)換的數(shù)量較多時(shí),就會顯得特別麻煩,弄不好還會遺漏或者重復(fù),為了解決這個(gè)問題,使用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. 利用CSS3新特性創(chuàng)建透明邊框三角2. phpstudy apache開啟ssi使用詳解3. xml中的空格之完全解說4. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式5. XML解析錯(cuò)誤:未組織好 的解決辦法6. phpstorm斷點(diǎn)調(diào)試方法圖文詳解7. JavaScrip簡單數(shù)據(jù)類型隱式轉(zhuǎn)換的實(shí)現(xiàn)8. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享9. asp讀取xml文件和記數(shù)10. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
