xlrd下载之后,直接把scripts目录下的文件放到C:\python27\tools\scripts目录下;另外xlrd直接放在C:\python27目录下即可。在目录C:\Python27\xlrd\examples下有提供一些API的演示。
到这里开发环境算是弄好了,不过我想你还需要一个IDE,下载eclipse,安装pydev插件即可。
#coding='utf-8'
import xlrd
def getPathFileName(path):
tmp = path.split('/');
return tmp[len(tmp) -1 ].split('.')[0];
def xlsToxmlPath(path):
return path.split('.')[0] + '.xml';
def export(path):
data = xlrd.open_workbook(path,
formatting_info = True, encoding_override="utf-8")
table = data.sheets()[0]
f = open(xlsToxmlPath(path), 'wb')
f.write(u'\n')
f.write(u'<%s>\n' % (getPathFileName(path)+'s'))
for i in range(1, table.nrows):
s = u'<%s '% getPathFileName(path);
tmp = [u'%s = "%s"' % (str(table.cell_value(0,j)), str(table.cell_value(i,j))) for j in range(table.ncols)];
s += u' '.join(map(str, tmp));
s += u' />\n';
f.write(s);
f.write(u'%s>' % (getPathFileName(path)+'s'));
export('E:/test.xls')