1.1 写数据
语句 | 说明 |
---|---|
工作表["a1"] = 值 | 写数据到一个单元格 |
工作表.cell(行,列).value = 值 | 写数据到一个单元格 |
工作表.cell(行,列, value=值) | 同上 |
工作表.append(列表) | 追加一行数据 |
工作表.insert_cols(idx=开始列,amount=总列数) | 从开始列,一共插入amount列 |
工作表.insert_rows(idx=开始行,amount=总行数) | 从开始行,一共插入amount行 |
工作表.delete_cols(idx=开始列,amount=总列数) | 从开始列,一共删除amount列 |
工作表.delete_rows(idx=开始行,amount=总行数) | 从开始行,一共删除amount行 |
1.1.1 一个单元格写数据
# 工作表[单元格] = 值
from openpyxl import load_workbook
wb = load_workbook("d:/test.xlsx")
sh = wb.active
sh["a1"] = "hello world"
wb.save("d:/test.xlsx")
也可以通过cell(row,column).value写数据
from openpyxl import load_workbook
wb = load_workbook("d:/test.xlsx")
sh = wb.active
sh.cell(3, 3).value = "hello world"
wb.save("d:/test.xlsx")
1.1.2 追加一行数据
数据放在列表里,一次性追加进去。
from openpyxl import load_workbook
wb = load_workbook("d:/test.xlsx")
sh = wb.active
sh.append(['hello',100,100,3.5])
wb.save("d:/test.xlsx")
1.1.3 一个区域写入数据
遍历这个区域,写上数据。 实际相当于一个单元格写入数据
from openpyxl import load_workbook
wb = load_workbook("d:/test.xlsx")
sh = wb.active
for row in sh["a1:c2"]:
for col in row:
col.value = '默认值'
wb.save("d:/test.xlsx")
1.1.4 插入列
# 工作表.insert_cols(idx=开始列,amount=总列数)
from openpyxl import load_workbook
wb = load_workbook("d:/test.xlsx")
sh = wb.active
sh.insert_cols(idx=2,amount=5)
wb.save("d:/test.xlsx")
1.1.5 插入行
# 工作表.insert_rows(idx=开始行,amount=总行数)
from openpyxl import load_workbook
wb = load_workbook("d:/test.xlsx")
sh = wb.active
sh.insert_rows(idx=2, amount=5)
wb.save("d:/test.xlsx")
1.1.6 删除列
# 工作表.delete_cols(idx=开始列,amount=总列数)
from openpyxl import load_workbook
wb = load_workbook("d:/test.xlsx")
sh = wb.active
sh.delete_cols(idx=2, amount=5)
wb.save("d:/test.xlsx")
1.1.7 删除行
# 工作表.delete_rows(idx=开始行,amount=总行数)
from openpyxl import load_workbook
wb = load_workbook("d:/test.xlsx")
sh = wb.active
sh.delete_rows(idx=2, amount=5)
wb.save("d:/test.xlsx")
标签:load,xlsx,openpyxl,PYTHON,amount,sh,test,wb
From: https://www.cnblogs.com/three-sheep/p/16946004.html