python读写excel例子,python读写excel,你知道吗?python也


你知道吗?python也可以方便的操作excel。 python的xlrd模块用来读取excel文件,xlwt用来写excel文件。

下面是python写excel的例子:

import xlwtfrom datetime import datetimestyle0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',    num_format_str='#,##0.00')style1 = xlwt.easyxf(num_format_str='D-MMM-YY')wb = xlwt.Workbook()ws = wb.add_sheet('A Test Sheet')ws.write(0, 0, 1234.56, style0)ws.write(1, 0, datetime.now(), style1)ws.write(2, 0, 1)ws.write(2, 1, 1)ws.write(2, 2, xlwt.Formula("A3+B3"))wb.save('example.xls')

下面的例子代码展示如何读取excel:

import xlrdbook = xlrd.open_workbook("myfile.xls")print "The number of worksheets is", book.nsheetsprint "Worksheet name(s):", book.sheet_names()sh = book.sheet_by_index(0)print sh.name, sh.nrows, sh.ncolsprint "Cell D30 is", sh.cell_value(rowx=29, colx=3)for rx in range(sh.nrows):     print sh.row(rx)

更多api请参考这里

评论关闭