欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python Excel操作之將ISBN轉(zhuǎn)條形碼

 更新時(shí)間:2025年06月22日 09:35:11   作者:晨曦之光Wing  
這篇文章主要為大家詳細(xì)介紹了Python Excel操作中如何將ISBN轉(zhuǎn)條形碼功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、效果

原始文件:

輸出文件:

二、代碼

import os
import logging
from openpyxl import load_workbook
from openpyxl.drawing.image import Image as ExcelImage
from barcode import EAN13
from barcode.writer import ImageWriter
 
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
 
def delete_files(directory):
    file_list = os.listdir(directory)
    for file in file_list:
        file_path = os.path.join(directory, file)
        if os.path.isfile(file_path):
            os.remove(file_path)
 
 
def generate_barcode_image_with_text(barcode_str, output_path_without_ext):
    # Generate barcode image without text
    ean = EAN13(barcode_str, writer=ImageWriter())
    saved_path = ean.save(output_path_without_ext)
 
    return saved_path
 
 
def insert_barcodes_to_excel(excel_file, ISBNCol, imgCol):
    logging.info("開始處理")
 
    if not os.path.exists('barcode_temp'):
        os.makedirs('barcode_temp')
 
    wb = load_workbook(excel_file)
    ws = wb.active
    logging.info(f"加載工作表: {ws.title}")
 
    img_height_in_points = 45  # 你設(shè)置的圖片高度,單位是磅,1磅約等于1.33像素
 
    for row in range(2, ws.max_row + 1):
        barcode_cell = ws.cell(row=row, column=ISBNCol)
        barcode_str = str(barcode_cell.value).strip()
        if not barcode_str or len(barcode_str) != 13 or not barcode_str.isdigit():
            logging.warning(f"第{row}行條碼格式不正確,跳過: {barcode_str}")
            continue
 
        img_path_no_ext = os.path.join('barcode_temp', f'barcode_{barcode_str}')
        try:
            img_path = generate_barcode_image_with_text(barcode_str, img_path_no_ext)
        except Exception as e:
            logging.error(f"生成條碼失敗,行{row},條碼{barcode_str},錯(cuò)誤: {e}")
            continue
 
        img_for_excel = ExcelImage(img_path)
        img_for_excel.width = 180
        img_for_excel.height = 60
 
        img_cell = f'{imgCol}{row}'
 
        # # 設(shè)置列寬
        ws.column_dimensions[imgCol].width = img_for_excel.width * 0.15
        # # 設(shè)置行高
        ws.row_dimensions[row].height = img_for_excel.height
 
        ws.add_image(img_for_excel, img_cell)
 
        # 調(diào)整當(dāng)前行高,避免圖片重疊
        current_height = ws.row_dimensions[row].height
        if current_height is None or current_height < img_height_in_points:
            ws.row_dimensions[row].height = img_height_in_points
 
        # logging.info(f"插入條碼圖片到 {img_cell} 并調(diào)整行高")
 
    new_file = os.path.splitext(excel_file)[0] + '_with_barcodes.xlsx'
    try:
        wb.save(new_file)
        logging.info(f"保存新文件: {new_file}")
    except PermissionError:
        logging.error(f"保存文件失敗,可能文件被打開: {new_file}")
 
    if os.path.exists('barcode_temp'):
        logging.info("刪除臨時(shí)文件")
        delete_files('barcode_temp')
 
    logging.info("完成處理!")
 
 
if __name__ == "__main__":
    excel_path = 'D:\\temp\\圖書清單.xlsx'
    insert_barcodes_to_excel(excel_path, 1, "B")

三、說明

1、insert_barcodes_to_excel參數(shù)1:原始Excel表格文件絕對(duì)路徑。

2、insert_barcodes_to_excel參數(shù)2:ISBN所在列,數(shù)字格式。例如:ISBN在A列則輸入1,在B列則輸入2。

3、insert_barcodes_to_excel參數(shù)3:生成的條形碼需要放在第幾列,大寫字母格式。例如:需要放在第二列則輸入B,第三列則輸入C。

到此這篇關(guān)于Python Excel操作之將ISBN轉(zhuǎn)條形碼的文章就介紹到這了,更多相關(guān)Python ISBN轉(zhuǎn)條形碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 三種Matplotlib中動(dòng)態(tài)更新繪圖的方法總結(jié)

    三種Matplotlib中動(dòng)態(tài)更新繪圖的方法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了如何隨著數(shù)據(jù)的變化動(dòng)態(tài)更新Matplotlib(Python的數(shù)據(jù)可視化庫)圖,文中介紹了常用的三種方法,希望對(duì)大家有所幫助
    2024-04-04
  • 關(guān)于pandas.DataFrame的類SQL操作

    關(guān)于pandas.DataFrame的類SQL操作

    這篇文章主要介紹了關(guān)于pandas.DataFrame的類SQL操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,
    2023-08-08
  • python繪制簡(jiǎn)單折線圖代碼示例

    python繪制簡(jiǎn)單折線圖代碼示例

    這篇文章主要介紹了python繪制簡(jiǎn)單折線圖代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Python 利用scrapy爬蟲通過短短50行代碼下載整站短視頻

    Python 利用scrapy爬蟲通過短短50行代碼下載整站短視頻

    近日,有朋友向我求助一件小事兒,他在一個(gè)短視頻app上看到一個(gè)好玩兒的段子,想下載下來,可死活找不到下載的方法。經(jīng)過我的一番研究才找到解決方法,下面小編給大家分享Python 利用scrapy爬蟲通過短短50行代碼下載整站短視頻的方法,感興趣的朋友一起看看吧
    2018-10-10
  • Python GUI庫Tkiner使用方法代碼示例

    Python GUI庫Tkiner使用方法代碼示例

    這篇文章主要介紹了Python GUI庫Tkiner使用方法代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • pytorch中常用的損失函數(shù)用法說明

    pytorch中常用的損失函數(shù)用法說明

    這篇文章主要介紹了pytorch中常用的損失函數(shù)用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python求解排列中的逆序數(shù)個(gè)數(shù)實(shí)例

    Python求解排列中的逆序數(shù)個(gè)數(shù)實(shí)例

    這篇文章主要介紹了Python求解排列中的逆序數(shù)個(gè)數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 解決python打不開文件(文件不存在)的問題

    解決python打不開文件(文件不存在)的問題

    今天小編就為大家分享一篇解決python打不開文件(文件不存在)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • python基于TCP實(shí)現(xiàn)的文件下載器功能案例

    python基于TCP實(shí)現(xiàn)的文件下載器功能案例

    這篇文章主要介紹了python基于TCP實(shí)現(xiàn)的文件下載器功能,結(jié)合具體實(shí)例形式分析了Python使用socket模塊實(shí)現(xiàn)的tcp協(xié)議下載功能客戶端與服務(wù)器端相關(guān)操作技巧,需要的朋友可以參考下
    2019-12-12
  • 使用Pandas對(duì)數(shù)據(jù)進(jìn)行篩選和排序的實(shí)現(xiàn)

    使用Pandas對(duì)數(shù)據(jù)進(jìn)行篩選和排序的實(shí)現(xiàn)

    這篇文章主要介紹了使用Pandas對(duì)數(shù)據(jù)進(jìn)行篩選和排序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評(píng)論