首页 文章

Openpyxl错误 - Valueerror {0}不是有效的坐标或范围

提问于
浏览
-1

我正在尝试将列表写入excel列并遇到错误 . 我试图将matchingName的每个值写入第V列中的工作表aSheet .

回溯(最近一次调用最后一次):文件“C:/Users/PycharmProjects/smartCompare/excelmain.py”,第40行,在aSheet [V] = matchingName [i3]文件“C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ openpyxl \ worksheet \ worksheet.py“,第380行,在setitem self [key] .value = value文件”C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ openpyxl \ worksheet \ worksheet.py“,第357行,getitem min_col,min_row,max_col,max_row = range_boundaries(key)文件”C:\ Users \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ openpyxl \ utils \ cell.py“,第129行,在range_boundaries中引发ValueError(”{0}不是有效坐标或范围“)ValueError:{0}不是有效坐标或范围

进程以退出代码1结束

这个错误似乎发生在for循环中 . 我检查了openpyxl文档,但没有解决它的运气 . 有什么建议?

import openpyxl
from difflib import SequenceMatcher

fruit = []
fruit2 = []
compareScore = []
matchingName = []
matchingRatioNum = []
wb = openpyxl.load_workbook('test.xlsx')
aSheet = wb.get_sheet_by_name('AMIS')
cSheet = wb.get_sheet_by_name('CMMS')

for col in aSheet['F']:
    fruit.append(col.value)

for col in cSheet['E']:
    fruit2.append(col.value)

length = 5
length2 = 5
i = 0
i2 = 0

for i in range(0, length):
    for i2 in range(0, length2):
        ratio = SequenceMatcher(None, fruit[i], fruit2[i2]).ratio()
        compareScore.append(ratio)
        i2 += 1
    matchRatio = compareScore.index(max(compareScore))
    match = fruit2[matchRatio]
    ratioNum = compareScore[matchRatio]
    matchingName.append(match)
    matchingRatioNum.append(ratioNum)
    compareScore = []
    i += 1

i3 = 0
for i3 in range(0, length):
    V = "'" + 'V' + str(i3+1) + "'"
    aSheet[V] = matchingName[i3]
    del V
    i3 += 1

i4 = 0
for i4 in range(0, length):
    W = "'" + 'W' + str(i4+1) + "'"
    aSheet[W] = matchingRatioNum[i4]
    del W
    i4 += 1
wb.save('test.xlsx')

1 回答

  • 2

    你正在创建像这样 ws["'W4'"] 的查找,它们是无效的坐标,你想要 ws["W4"] . 您应该 always 使用 ws.cell() 进行编程访问 .

相关问题