openpyxl单元格内容替换失败
for each_row in ws['A1':'G10']:for each_cell in each_row:
try:
#example = "事业部经理,电子1925班主任,电商专业组长"
print(type(each_cell))
example = each_cell.value
print(example)
example=example.split(",")
print(example)
res = []
exam = ","
for i in each_cell:
if "班主任" not in i:
res.append(i)
print(res)
a = exam.join(res)
print(a)
ws.value = a
except:
continue
print(ws['A1'].value)
代码如上,为什么ws['A1']里还是没有把替换之后的值写入,请教各位大佬 你的第一行代码是切片把,切片不是都是用的索引值切的吗,你这还能用字符串??我第一次见 有用字符串切片的!所以你自己可以打印看看,应该是你切片错误导致! 代码发全,xlsx也发一下。 gdmao002 发表于 2021-8-27 10:20
你的第一行代码是切片把,切片不是都是用的索引值切的吗,你这还能用字符串??我第一次见 有用字符串切片 ...
openpyxl的功能:https://openpyxl.readthedocs.io/en/stable/tutorial.html#accessing-many-cells suchocolate 发表于 2021-8-27 15:01
openpyxl的功能:https://openpyxl.readthedocs.io/en/stable/tutorial.html#accessing-many-cells
好的 没用过这个功能哈哈。谢谢 r=int(each_cell.row)
ws.cell(row=r, column=2).value = a
我用这个方法往单元格里写入值就可以了
页:
[1]