Openpyxl 错误 - Valueerror {0} 不是有效的坐标或范围
Openpyxl error - Valueerror {0} is not a valid coordinate or range
我正在尝试将列表写入 excel 列,但遇到错误。我正在尝试将 matchingName 的每个值写入 V 列中的工作表 aSheet。
Traceback (most recent call last):
File "C:/Users/PycharmProjects/smartCompare/excelmain.py", line 40, in
aSheet[V] = matchingName[i3]
File
"C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\worksheet\worksheet.py", line 380, in setitem
self[key].value = value
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\worksheet\worksheet.py", line 357, in getitem
min_col, min_row, max_col, max_row = range_boundaries(key)
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\utils\cell.py", line 129, in range_boundaries
raise ValueError("{0} is not a valid coordinate or range")
ValueError: {0} is not a valid coordinate or range
进程已完成,退出代码为 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')
您正在创建这样的查找 ws["'W4'"]
,这是无效坐标,您需要 ws["W4"]
。您应该始终 使用ws.cell()
进行编程访问。
我正在尝试将列表写入 excel 列,但遇到错误。我正在尝试将 matchingName 的每个值写入 V 列中的工作表 aSheet。
Traceback (most recent call last): File "C:/Users/PycharmProjects/smartCompare/excelmain.py", line 40, in aSheet[V] = matchingName[i3] File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\worksheet\worksheet.py", line 380, in setitem self[key].value = value File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\worksheet\worksheet.py", line 357, in getitem min_col, min_row, max_col, max_row = range_boundaries(key) File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\utils\cell.py", line 129, in range_boundaries raise ValueError("{0} is not a valid coordinate or range") ValueError: {0} is not a valid coordinate or range
进程已完成,退出代码为 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')
您正在创建这样的查找 ws["'W4'"]
,这是无效坐标,您需要 ws["W4"]
。您应该始终 使用ws.cell()
进行编程访问。