Openpyxl 单元格赋值错误
import timefrom openpyxl import load_workbook
import openpyxl
wb = openpyxl.load_workbook(r"C:\Users\xxxx\Desktop\Question.xlsx")
ws = wb['Data']
n = ws.max_row+1
for z in range(n-14,n+1):
w=str(z)
oc=ws['e'+ w].value
od=ws['f'+ w].value
r = str(n)
n = n + 1
ws['a' + r] = i
ws['b' + r] = trs
ws['e' + r] = time.strftime("%Y-%m-%d", time.localtime())
ws['f' + r] = trs1
ws['i' + r] = time.strftime("%Y-%m", time.localtime())
ws['c '+ r] = oc
ws['d' + r] = od
wb.save(r"C:\Users\xxxx\Desktop\Question.xlsx")
提示如下错误:
--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-8096ceec7509> in <module>
47 ws['f' + r] = trs1
48 ws['i' + r] = time.strftime("%Y-%m", time.localtime())
---> 49 ws['c '+ r] = oc
50 ws['d' + r] = od
51
c:\users\hchen\appdata\local\programs\python\python38-32\lib\site-packages\openpyxl\worksheet\worksheet.py in __setitem__(self, key, value)
307
308 def __setitem__(self, key, value):
--> 309 self.value = value
310
311
c:\users\hchen\appdata\local\programs\python\python38-32\lib\site-packages\openpyxl\worksheet\worksheet.py in __getitem__(self, key)
284 key = str(key
285 )
--> 286 min_col, min_row, max_col, max_row = range_boundaries(key)
287
288 if not any():
c:\users\hchen\appdata\local\programs\python\python38-32\lib\site-packages\openpyxl\utils\cell.py in range_boundaries(range_string)
133 m = ABSOLUTE_RE.match(range_string)
134 if not m:
--> 135 raise ValueError(msg)
136
137 min_col, min_row, sep, max_col, max_row = m.groups()
ValueError: c 122 is not a valid coordinate or range
ValueError: c 122 is not a valid coordinate or range,没有"c 122"这个单元。
看代码里,c后面多了个空格:
ws['c '+ r] = oc
页:
[1]