python中关于Excel的简单操作

  1. 打开桌面的excel.xlsx:
import os
import openpyxl

path = r"C:\Users\Desktop"
os.chdir(path)  # 修改工作路径
wb = openpyxl.load_workbook('excel.xlsx')

wb = openpyxl.load_workbook(r'C:\Users\Desktop\excel.xlsx')
  1. 工作表的读取
print(wb.sheetnames)  # 打印所有表

sheet = wb["新的sheet"]  # 获取指定sheet表
print(sheet)
print('表格的尺寸大小为:' + sheet.dimensions)  # 获取表格的尺寸大小

sheet1 = wb.active  #获取活动表,关闭Excel前最后查看的表
print(sheet1)

 # 获取单元格的数据
cell1 = sheet['C5']
cell2 = sheet['F15']
print(cell1, cell2)
print(cell1.value, cell2.value) 
cell3 = sheet['E12'].value 
print(cell3)
cell4 = sheet.cell(row=1, column=2).value
cell5 = sheet.cell(row=3, column=1).value
print(cell4, cell5)
cell6 = sheet.cell(5, 1)
print(cell6.value, cell6.row, cell6.column, cell6.coordinate)

cell7 = sheet['A1:A5']  # 获取A1到A5的数据
for i in cell7:
    for j in i:
        print(j.value)

cell8 = sheet["A"]
for i in cell8:
    print(i.value)

cell9 = sheet['5']
for i in cell9:
    print(i.value)

print('按行获取值')
for i in sheet.iter_rows(min_row=2, max_row=5, min_col=1, max_col=2):
    for j in i:
        print(j.value)

print('按列获取值')
for i in sheet.iter_cols(min_row=2, max_row=5, min_col=1, max_col=2):
    for j in i:
        print(j.value)

rows = sheet.max_row
column = sheet.max_column
print(rows, column)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值