我正在制作一个 excel 比较程序,该程序需要一定数量的工作表,比较它们并检查其中一张工作表中的值是否在另一张工作表中不存在。但是,我收到一个重复的问题。为了澄清,这是我的代码:import tkinter as tkfrom tkinter import filedialogimport openpyxl, os, csvfrom openpyxl.utils import get_column_letter, column_index_from_string# Output FileoutputFile = open('output.csv','w',newline='')outputWriter = csv.writer(outputFile)# Tk initialization for file dialogroot = tk.Tk()root.withdraw()# Number of sheets to be comparednumber = input('Enter number of workbook sheets for comparison: ')number = int(number)# Functions for generating file pathsdef generate_file_path(): file_path = filedialog.askopenfilename(title="Open Workbook") return file_path# Variables to store file paths, workbooks and worksheetsall_ws = []# Core function for program's logisticsdef core(): # for loops for generating file paths, workbooks and worsheets for x in range(number): path = generate_file_path() wb = openpyxl.load_workbook(path) ws = wb['CBF'] all_ws.append(ws) # for loop to use for finding diff for row in all_ws[1].iter_cols(): for cellz in row: sheet_cols.append(cellz.value) # loop that checks if the value does not exist for ws_diff in range(number): for row,row2 in zip(all_ws[0].iter_cols(),all_ws[1].iter_cols()): for cell,cell2 in zip(row,row2): if cell.value not in sheet_cols: outputWriter.writerow([str(cell2.value)])但是,当我检查我的 csv 文件时,输出的“差异”似乎在两个文件上。有人对我有什么建议吗?一切都很好,谢谢
2 回答
慕码人8056858
TA贡献1803条经验 获得超6个赞
也许你可以这样做:
改变:
for row in all_ws[1].iter_cols():
至:
for row in all_ws[0].iter_cols():
之后:
改变 if cell.value not in sheet_cols:
至: if cell2.value in sheet_cols:
添加回答
举报
0/150
提交
取消