dbf到xls – 第一个非标题行不写

我想使用python将.dbf文件转换为.xls。 我已经引用这个片段 ,但是我不能得到第一个非标题行写入使用此代码:

from xlwt import Workbook, easyxf import dbfpy.dbf dbf = dbfpy.dbf.Dbf("C:\\Temp\\Owner.dbf") book = Workbook() sheet1 = book.add_sheet('Sheet 1') header_style = easyxf('font: name Arial, bold True, height 200;') for (i, name) in enumerate(dbf.fieldNames): sheet1.write(0, i, name, header_style) for row in range(1, len(dbf)): for col in range(len(dbf.fieldNames)): sheet1.row(row).write(col, dbf[row][col]) book.save("C:\\Temp\\Owner.xls") 

我怎样才能得到第一个非标题行写?

谢谢

您错过了第一行dbf中的第0行。 在dbf文件中,列名不是一行。 但是,Excel文件中的第0行是标题,因此索引需要在dbf和xls中有所不同,因此需要将1添加到Excel工作表中使用的行中。

所以

 for row in range(len(dbf)): for col in range(len(dbf.fieldNames)): sheet1.row(row+1).write(col, dbf[row][col]) 

请注意,所引用的snipper不会在范围内添加1