python3 读写excel
一直认为python3可以很快的实现很多简单的功能,今天要读excel表格数据,想来很简单,网上一搜,用xlrd即可,然后很多人给出了不同的版本,号称xlrd3,实际上官网一看,xlrd0.9.4兼容2和3,因此直接下载即可,网址:https://pypi.python.org/pypi/xlrd
下载后解压,然后通过命令行进入下载的目录,然后C:\python34\python.exe setup.py install即可(我的python安装在C盘,是3.4版)
然后输入以下代码:
#!/usr/bin/env python3
#coding: utf-8
import xlrd
wb = xlrd.open_workbook("D:\\send\\mydata.xls")
sh=wb.sheet_by_index(0)#第一个表
cellName = sh.cell(3,2).value
print(cellName)
发现这个错误出现了:Python XLRD Error : formula/tFunc unknown FuncID:186
找了好久,终于国外有个家伙也碰到了,网址如下:http://stackoverflow.com/questions/29971186/python-xlrd-error-formula-tfunc-unknown-funcid186
解决方案的原文回答如下:
For now I just wanted to make sure the xlrd read fine. I hacked my xlrd package so that it loads.
I don't gaurentee the correctness of the output.
Just add the following line in formula.py of your pythonlibs/xlrd package.
Around line 240 where each number is map to a function create a hacked function here. I've inserted 'HACKED' in there. I don't understand exactly what's going on.
-- added the line that starts with 186:
184: ('FACT', 1, 1, 0x02, 1, 'V', 'V'),
186: ('HACKED', 1, 1, 0x02, 1, 'V', 'V'),
189: ('DPRODUCT', 3, 3, 0x02, 3, 'V', 'RRR'),
Here is the discussion by xlrd group. Essentially, this is a complicated problem that can't be resolved. :)
很清楚了,找到formula.py文件186行左右,在文字184和文字189中间加插入一行186: ('HACKED', 1, 1, 0x02, 1, 'V', 'V'),
再运行程序,搞定