Home  >  Article  >  Backend Development  >  How to read excel files in python

How to read excel files in python

藏色散人
藏色散人Original
2019-10-29 11:22:0714472browse

How to read excel files in python

python怎么读取excel文件?

1.首先说明我是使用的python3.5,我的office版本是2010,首先打开dos命令窗,安装必须的两个库,命令是:

pip3 install xlrd
Pip3 install xlwt

How to read excel files in python

2.准备好excel,例如我的一个工作文件,我放在D盘/百度经验/11.xlsx,只有一个页签A,内容是一些销售数据

How to read excel files in python

3.打开pycharm,新建一个excel.py的文件,首先导入支持库

import xlrdimport xlwt

How to read excel files in python

4.针对刚入门的新手,先介绍三个知识,第一个:获取excel的sheet名称,第二:获取excel行数与列数,第三:获取第几行第几列的具体值,这是最常用的三个知识点

How to read excel files in python

5.贴出代码,具体分析:

1.要操作excel,首先得打开excel,使用open_workbook(‘路径’)

2.要获取行与列,使用nrows(行),ncols(列)

3.获取具体的值,使用cell(row,col).value

workbook=xlrd.open_workbook(r'E:\11.xlsx')print (workbook.sheet_names())  sheet2=workbook.sheet_by_name('A')  nrows=sheet2.nrows  ncols=sheet2.ncols  print(nrows,ncols) cell_A=sheet2.cell(1,1).value print(cell_A)

How to read excel files in python

6.要在excel里写入值,就要使用write属性,重点说明写入是用到xlwt这个支援库,思路是先新建excel,然后新建页签B,然后将一组数据写入到B,最后保存为excel.xls,这里建议保存为2003的格式,大部分电脑都能打开,特别注意保存的excel的路径是在python工作文件的目录下面,贴出代码:

stus = [['年', '月'], ['2018', '10'], ['2017', '9'], ['2016', '8']]Excel = xlwt.Workbook()  # 新建excelsheet = Excel.add_sheet('B') #新建页签Brow = 0for stu in stus:    col = 0    for s in stu:        sheet.write(row, col, s)  #开始写入        col = col + 1    row = row + 1Excel.save('Excel.xls') #保存

How to read excel files in python

How to read excel files in python

The above is the detailed content of How to read excel files in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn