from openpyxl.reader.excel import load_workbook
def 写入():
import openpyxl
# D:\aa\例子
wb = openpyxl.load_workbook('工作簿1.xlsx')
sheet = wb['Sheet1']
sheet['A1'] = '1'
sheet['B1'] = '2'
sheet['C1'] = '3'
wb.save('工作簿1.xlsx')
def 读取():
# 导入模块
from openpyxl import load_workbook
# 导入一个WorkBook
workBook = load_workbook("工作簿1.xlsx")
# 获取sheet对象
sheet = workBook["Sheet1"]
# 找到要操作的单元格
cell = sheet.cell(row=1, column=2)
print(cell.value)
#关闭
workBook.close()
def 获取每行每个单元格对应的值():
workBook = load_workbook("工作簿1.xlsx")
sheet = workBook["Sheet1"]
row = sheet.rows # 获取所有行对象
for i in list(row):
print(i)
for ii in i:
print(ii.value) # 通过双层for循环,获取每行每个单元格对应的值
workBook.close()
if __name__ == '__main__':
写入()
读取()
获取每行每个单元格对应的值()
python操作Excel.py
于 2023-04-06 01:10:05 首次发布