• openpyxl处理excel例子


    import openpyxl
    
    inv_file = openpyxl.load_workbook("inventory.xlsx")
    product_list = inv_file["Sheet1"]
    
    products_per_supplier = {}
    total_value_per_supplier = {}
    products_under_10_inv = {}
    
    print(product_list)
    
    for product_row in range(2, product_list.max_row + 1):
        supplier_name = product_list.cell(product_row, 4).value
        inventory = product_list.cell(product_row, 2).value
        price = product_list.cell(product_row, 3).value
        product_num = product_list.cell(product_row, 1).value
        inventory_price = product_list.cell(product_row, 5)
    
        if supplier_name in products_per_supplier:
            current_num_products = products_per_supplier.get(supplier_name)
            products_per_supplier[supplier_name] = current_num_products + 1
        else:
            products_per_supplier[supplier_name] = 1
    
        if supplier_name in total_value_per_supplier:
            current_total_value = total_value_per_supplier.get(supplier_name)
            total_value_per_supplier[supplier_name] = current_total_value + inventory + price
    
        total_value_per_supplier[supplier_name] = inventory * price
    
        if inventory < 10:
            products_under_10_inv[product_num] = int(inventory)
    
        inventory_price.value = inventory * price
    
    
    print(products_per_supplier)
    print(total_value_per_supplier)
    print(products_under_10_inv)
    
    inv_file.save("inventory_with_total_value.xlsx")

    源文件 inventory.xlsx

     

    生成文件 inventory_with_total_value.xlsx:

  • 相关阅读:
    解决硬盘分区无法挂载的问题
    Java EE学习记录(一)
    Netbeans 8.2启动参数含义及配置
    汉化CodeBlock
    linux解决无法打开资源管理器
    netbean下搭建mariadb数据库
    mariadb中执行数据库脚本的方法
    修复受损的linux引导
    修复无法启动的mariadb
    no update
  • 原文地址:https://www.cnblogs.com/zhishaofei/p/16366440.html
Copyright © 2020-2023  润新知