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

时间:2017-05-15 14:25:14

标签: python openpyxl

我正在尝试将列表写入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 个答案:

答案 0 :(得分:2)

您正在创建此ws["'W4'"]的查找,这些查询是无效坐标,您需要ws["W4"]。您应始终使用ws.cell()进行编程访问。