加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python对Excel的操作

发布时间:2020-12-20 10:22:49 所属栏目:Python 来源:网络整理
导读:? ?一:安装模块 ① pip3 install xlwt(写操作模块) ② pip3 install openpyxl(读操作模块) 二:openpyxl关于工作薄和sheet的操作 ① 创建一个工作薄,会自动创建一个sheet表 excel=openpyxl.Workbook() ② 打开一个已有的工作薄 excel=openpyxl.load_workbo

?

?一:安装模块

  ① pip3 install xlwt(写操作模块)

  ② pip3 install openpyxl(读操作模块)

二:openpyxl关于工作薄和sheet的操作

  ① 创建一个工作薄,会自动创建一个sheet表

     excel=openpyxl.Workbook()
  ② 打开一个已有的工作薄

    excel=openpyxl.load_workbook("路径")

  ③ 通过sheet名字打开一个sheet

    sheet=excel[‘收入‘]

    sheet=excel.get_sheet_by_name[‘收入‘]

  ④ 获取所有工作薄

    sheet_names=excel.get_sheet_names()

  ⑤ 通过index获取对应的sheet

    sheet=excel.get_sheet_by_name[sheet_names[index]]

  ⑥ 调用得到正在运行的sheet

    sheet=excel.active

?  ⑦ 读取单元格
    data=sheet[‘A1‘]

    data = sheet.cell(row=1,column=1)

  ⑧对单元格写入数据

    sheet.cell(row=1,column=4,value=‘aa‘)

    sheet[‘A2‘]=‘bbb‘

    sheet.append([1,2,3])#在新的一行写入

  ⑨ 其他属性

    print(sheet.title,sheet.max_row,sheet.max_column)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读