栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中关于Excel的简单操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中关于Excel的简单操作

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

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

wb = openpyxl.load_workbook(r'C:UsersDesktopexcel.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)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960584.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号