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()
#隱藏了二欄,處理結果如下:
總結
以上是生活随笔為你收集整理的python openpyxl写入多行_Python openpyxl读写操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pythonbyte连接_Python3
- 下一篇: 残差平方和ssr的计算公式为_如何为你的