Openpyxl get_column_letter 值错误

Openpyxl get_column_letter ValueError

我正在 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:

有谁知道如何解决这个问题?任何帮助将不胜感激。

您似乎已达到 sheet 中的列数限制。

There's a limit of max 18,278 columns in the sheet supported by openpyxl, and even less 在 MS Office Excel (16,384) 和 Apple Numbers(仅 255 列)中。

您可以像这样向代码添加检查:

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:)

这是错误的:

object1 = list(sheet1['A1':get_column_letter(sheet1.max_row)])  

你传递了类似 sheet1['A1':D] 的东西,你想要 sheet1['A1':D1000]

我相信这个嵌套循环不会起作用:
for i in object1: for x in object2: