首页 > python openpyxl写入多行_Python openpyxl读写操作

python openpyxl写入多行_Python openpyxl读写操作

import openpyxl #导入 模块

#读取xlsx文件

rb = openpyxl.load_workbook(r"c:11.xlsx")

#获取工作表--Sheet

sheet = rb['Sheet1']

#A1--E1表的启始位置

a1 = sheet["a1"]

b1 = sheet["b1"]

c1 = sheet["c1"]

d1 = sheet["d1"]

e1 = sheet["e1"]

def sum():

munber = 1

#新建一个XLSX文件

wb = openpyxl.Workbook()

#读取单元格的值

for row in sheet.rows:

# 复杂版,先读K1的cellrow行数,再遍历一次读单元的值

# k1 = sheet["A1":"E1"]

# for cellrow in k1:

# for cellvalue in cellrow:

# print(cellvalue.value)

# print("-------------------")

#简单版

a1 = sheet["A{}".format(munber)]

b1 = sheet["B{}".format(munber)]

c1 = sheet["C{}".format(munber)]

d1 = sheet["D{}".format(munber)]

e1 = sheet["E{}".format(munber)]

values = d1.value.strip()+"-"+e1.value.strip()

# 将读取值处理好写入到另一个XLSX文件

ws.cell(munber,1).value = a1.value

ws.cell(munber,2).value = b1.value

ws.cell(munber,3).value = c1.value

ws.cell(munber,4).value = d1.value

ws.cell(munber,5).value = e1.value

ws.cell(munber,6).value = values

munber += 1

#保存文件

wb.save(r"c:1111.xlsx")

if __name__ == "__main__":

sum()

#隐藏了二栏,处理结果如下:

更多相关: