# -*- coding: utf-8 -*-
import xlrd
from datetime import date,datetime
def read_excel():
ExcelFile=xlrd.open_workbook(r'/root/test.xls')
print ExcelFile.sheet_names()
sheet=ExcelFile.sheet_by_name('hbk')
print sheet.name,sheet.nrows,sheet.ncols
rows=sheet.row_values(2)#第三行内容
cols=sheet.col_values(1)#第二列内容
print cols,rows
print sheet.cell(1,0).value.encode('utf-8')
print sheet.cell_value(1,0).encode('utf-8')
print sheet.row(1)[0].value.encode('utf-8')
print sheet.cell(1,0).ctype
if __name__ =='__main__':
read_excel()
运行该程序时,遇到ImportError: No module named ‘xlrd’ 错误,需要安装xlrd,使用pip安装xlrd是比较方便,而pip的安装需要setuptools,所以第一步先安装setuptools
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
tar -zxvf setuptools-19.6.tar.gz
cd setuptools-19.6
python setup.py build
python setup.py install
然后安装pip
wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
tar -zxvf pip-8.0.2.tar.gz
cd pip-8.0.2
python setup.py build
python setup.py install
然后使用pip安装xlrd
pip install xlrd