当前位置:   article > 正文

python读excel表格_python读取excel sheet

python读取excel sheet

一、xlrd模块读取

python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库

import xlrd
def read_excel():
    path = './Sec_BLE_Zigbee.xlsx'
    book = xlrd.open_workbook(path) # 打开一个工作表
    for sheet in book.sheets():# book.sheets()#返回book中所有工作表的名字   
        print(sheet.name)# Sec_BLE_Zigbee,zig
    book1 = book.sheet_by_name('Sec_BLE_Zigbee')# 通过表明获取表
    book2 = book.sheet_by_index(0)# 通过索引获取表
    nrows = book1.nrows# 表的行数
    print(nrows)
    for i in range(nrows):
        print(book1.row_values(i))# 每一行的值
if __name__ == '__main__':
    read_excel()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

  • xlrd安装方法
pip install xlrd
  • 1

报错 读取xlsx文件错误:xlrd.biffh.XLRDError: Excel xlsx file; not supported
可能是xlrd版本太高了。需要降级

参考https://blog.csdn.net/Erickkkkkk/article/details/124696751

二、Pandas读取Excel的不同sheet中的数据

sheet_name

total = pd.read_excel(cost_total_path,sheet_name='主板')
  • 1

三、保存数据

data.to_excel(file_path, encoding="gb2312", index=0, float_format='%.7f')
  • 1

float_format设置float类型数据保存的小数的格式
两列元素点乘

data['扩展成本'] = data.apply(lambda x: np.multiply(x['Quantity'], x['物料成本不含税']), axis=1)
  • 1

在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号