美文网首页
python往excel里面插入图片

python往excel里面插入图片

作者: 我问你瓜保熟吗 | 来源:发表于2020-07-09 18:13 被阅读0次
    import os
    import xlrd
    from openpyxl import load_workbook
    from openpyxl.drawing.image import Image
    
    image_dir = 'D:/桌面/xlsxwriter/2020_07_05'
    xlsx_path = 'D:/桌面/xlsxwriter/反馈信息表.xlsx'
    
    succes_text = open("succeses.txt", "r+", encoding='utf8')
    false_text = open("false.txt", "r+", encoding='utf8')
    
    # 图片名称的数组
    images = [k for i, j, k in os.walk(image_dir)][0]
    images2 = []
    for i in images:
        if i.startswith('AD') and i.endswith(".png"):
            images2.append(i)
    
    print("===============================================")
    print(" ")
    print("所有的签名图片名称: ")
    print(images2)
    print(" ")
    print("===============================================")
    
    
    # 打开基础信息sheet
    wb = load_workbook(xlsx_path)
    sheet = wb["基本信息表"]
    
    FileObj = xlrd.open_workbook(xlsx_path)
    # 查找id_number所在的位置
    sheet_x = FileObj.sheet_by_name("基本信息表")
    row_count = sheet_x.nrows
    lists = sheet_x.col_values(10)
    
    print(" ")
    print("所有图片里包含的身份证号")
    print(lists)
    print(" ")
    print("===============================================")
    
    
    def insert_image(insert_location, image_path):
    
        # # 要插入的图片路径、插入后的图片尺寸   
        # print(image_path)
    
        img = Image(image_path)
        new_size = (150, 80)
        img.width, img.height = new_size
    
        sheet[insert_location] = ""
        sheet.add_image(img,insert_location)
    
        wb.save(xlsx_path)
        print("插入成功!")
    
    
    # 执行图片插入
    count_success = 0
    count_false = 0
    count = 0
    for i in images2:
    
        # 图片绝对路径
        image_absolute_path = image_dir + "/" + i
        # 身份证号
        id_number = i.split('_')[4]
    
        print("")
        print("正在执行第:", count, "个")
        print(id_number)
        print(image_absolute_path)
    
        id_number_row = {}
        row = 2
    
        count = count + 1
        if id_number in lists:
            insert_image("I" + str(lists.index(id_number) + 13), image_absolute_path)
            count_success = count_success+1
            print("匹配成功!")
            succes_text.write(image_absolute_path)
            succes_text.write('\r\n')
    
        if id_number not in lists:
            count_false = count_false + 1
            print("匹配失败!")
            false_text.write(image_absolute_path)
            false_text.write('\r\n')
    
    wb.save(xlsx_path)
    wb.close()
    
    print("")
    print("===============================================")
    print("")
    print("执行结束。保存、关闭excel")
    
    print("")
    print("===============================================")
    print("")
    print("共 ", count, "张签名图片")
    print("成功匹配并插入签名图片个数:", count_success)
    print("签名未在excel中找到的签名图片个数:", count_false)
    

    相关文章

      网友评论

          本文标题:python往excel里面插入图片

          本文链接:https://www.haomeiwen.com/subject/wywscktx.html