Read Excel files from Python[转]

简介:

http://scienceoss.com/read-excel-files-from-python/

 

 

Use the excellent xlrd package, which works on any platform. That means you can read Excel files from Python in Linux! Example usage:

Open the workbook

import xlrd
wb = xlrd.open_workbook('myworkbook.xls')

Check the sheet names

wb.sheet_names()

Get the first sheet either by index or by name

sh = wb.sheet_by_index(0)
sh = wb.sheet_by_name(u'Sheet1')

Iterate through rows, returning each as a list that you can index:

for rownum in range(sh.nrows):
    print sh.row_values(rownum)

If you just want the first column:

first_column = sh.col_values(0)

Index individual cells:

cell_A1 = sh.cell(0,0).value
cell_C4 = sh.cell(rowx=3,colx=2).value

(Note Python indices start at zero but Excel starts at one)

Turns out the put_cell() method isn’t supported, so ignore the following section (Thanks for the heads up, John!)

Put something in the cell:

row = 0
col = 0
ctype = 1  # see below
value = 'asdf'
xf = 0  # extended formatting (use 0 to use default)
sh.put_cell(row, col, ctype, value, xf)
sh.cell(0,0)  # text:u'asdf'
sh.cell(0,0).value  # 'asdf'

Possible ctypes: 0 = empty, 1 = string, 2 = number, 3 = date, 4 = boolean, 5 = error

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!
相关文章
|
6天前
|
Python
Python办公自动化:xlwings对Excel进行分类汇总
Python办公自动化:xlwings对Excel进行分类汇总
21 1
|
6天前
|
Python
Python自动化:xlwings合并Excel
Python自动化:xlwings合并Excel
19 0
|
25天前
|
数据采集 存储 数据挖掘
使用Python读取Excel数据
本文介绍了如何使用Python的`pandas`库读取和操作Excel文件。首先,需要安装`pandas`和`openpyxl`库。接着,通过`read_excel`函数读取Excel数据,并展示了读取特定工作表、查看数据以及计算平均值等操作。此外,还介绍了选择特定列、筛选数据和数据清洗等常用操作。`pandas`是一个强大且易用的工具,适用于日常数据处理工作。
|
6天前
|
数据可视化 数据处理 Python
Python操作Excel:轻松实现数据处理与分析
Python操作Excel:轻松实现数据处理与分析
10 0
|
6天前
|
Python
Python办公自动化:xlwings对Excel进行列拆分
Python办公自动化:xlwings对Excel进行列拆分
14 0
|
6天前
|
Python
Python办公自动化:xlwings拆分Excel
Python办公自动化:xlwings拆分Excel
13 0
|
6天前
|
Python
Python自动化:xlwings替换Excel中内容
Python自动化:xlwings替换Excel中内容
17 0
|
6天前
|
Python
Python:Pandas实现批量删除Excel中的sheet
Python:Pandas实现批量删除Excel中的sheet
16 0
|
2月前
|
前端开发 Python
使用Python+openpyxl实现导出自定义样式的Excel文件
本文介绍了如何使用Python的openpyxl库导出具有自定义样式的Excel文件,包括设置字体、对齐方式、行列宽高、边框和填充等样式,并提供了完整的示例代码和运行效果截图。
40 1
使用Python+openpyxl实现导出自定义样式的Excel文件
|
1月前
|
数据挖掘 数据处理 Python
python如何高效处理excel图表案例分享
python如何高效处理excel图表案例分享
30 2
下一篇
无影云桌面