我在python中创建了一个excel比较工具。这是我的代码:
bananacell.py:
import time
print("Welcome to Banana Cell the open source excel tool!")
time.sleep(1)
import compare
compare.main()
compare.py:
import openpyxl, tkinter, time
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from openpyxl.utils import get_column_letter
def main():
print('Select file 1 from folder')
root = Tk()
filename = askopenfilename()
root.withdraw()
wb1 = openpyxl.load_workbook(filename)
names1 = input('Sheet 1 name: ')
sheet1 = wb1.get_sheet_by_name(str(names1))
print('Select file 2 from folder')
root1 = Tk()
filename1 = askopenfilename()
root1.withdraw()
wb2 = openpyxl.load_workbook(str(filename1))
names2 = input('Sheet 2 name: ')
sheet2 = wb2.get_sheet_by_name(str(names2))
object1 = list(sheet1['A1':get_column_letter(sheet1.max_row)])
object2 = list(sheet2['A1':get_column_letter(sheet2.max_row)])
for i in object1:
for x in object2:
if i != x:
print('Diff found!')
print (i.value, i.coordinate)
print (x.value, x.coordinate)
print('----------')
break
但是我继续得到以下ValueError:
有谁知道如何解决这个问题?任何帮助将不胜感激。
答案 0 :(得分:2)
看起来你达到了工作表中列的限制。
openpyxl支持的工作表中最多18,278列的There's a limit,以及MS Office Excel(16,384)和Apple Numbers(仅255列)中的even less。
您可以在代码中添加检查,例如:
MAX_COLUMNS = 18278
if active_sheet.max_column > MAX_COLUMNS:
raise ValueError(
'Too many columns in the sheet, the limit is {}, found: {}'.format(
MAX_COLUMNS,
ctive_sheet.max_column,
)
)
或者制作一个支持更多列的pull request to openpyxl:)
答案 1 :(得分:1)
这是错误的:
object1 = list(sheet1['A1':get_column_letter(sheet1.max_row)])
您传递sheet1['A1':D]
之类的内容,您需要sheet1['A1':D1000]
我相信这个嵌套循环不起作用:
for i in object1:
for x in object2: