# -*- coding: utf-8 -*- """ Created on Fri Sep 13 16:49:05 2024 比较 stdBcdpath 和 filepath 中的 barcodes 列表,求出二者的并集和为包含在 stdBcdpath 中的 barcodes 清单 @author: ym """ import os from openpyxl import load_workbook, Workbook def read_xlsx(): stdBcdpath = r"\\192.168.1.28\share\已标注数据备份\对比数据\barcode\total_barcode_6588" filepath = r"\\192.168.1.28\share\联华中环店\中环店商品信息.xlsx" existingPath = r'\\192.168.1.28\share\联华中环店\中环店商品信息_已有商品.xlsx' lackingPath = r'\\192.168.1.28\share\联华中环店\中环店商品信息_未包含商品.xlsx' workbook = load_workbook(filename=filepath) sheet = workbook['Sheet1'] barcodeCol = [sheet.cell(row=r, column=1).value for r in range(1, sheet.max_row+1)] zhBarcodeList = [barcodeCol[i] for i in range(1, len(barcodeCol))] stdBarcodeList = [] for filename in os.listdir(stdBcdpath): filepath = os.path.join(stdBcdpath, filename) if not os.path.isdir(filepath) or not filename.isdigit(): continue stdBarcodeList.append(int(filename)) stdBarcodeSet = set(stdBarcodeList) zhBarcodeSet = set(zhBarcodeList) interBarcodes = list(zhBarcodeSet.intersection(stdBarcodeSet)) print(len(interBarcodes)) dest_wb1 = Workbook() dest_sheet1 = dest_wb1.active for row in sheet.iter_rows(min_row=1, max_col=sheet.max_column, values_only=True): if str(row[0]).find("商品条码")>=0: dest_sheet1.append(row) if row[0] in interBarcodes: dest_sheet1.append(row) dest_wb1.save(filename=existingPath) dest_wb1.close() diffBarcodes = list(zhBarcodeSet.difference(stdBarcodeSet)) dest_wb2 = Workbook() dest_sheet2 = dest_wb2.active for row in sheet.iter_rows(min_row=1, max_col=sheet.max_column, values_only=True): if str(row[0]).find("商品条码")>=0: dest_sheet2.append(row) if row[0] in diffBarcodes: dest_sheet2.append(row) dest_wb2.save(filename=lackingPath) dest_wb2.close() workbook.close() if __name__ == '__main__': # main() read_xlsx()