【问题标题】:dbf to xls - first non header row not writingdbf 到 xls - 第一个非标题行未写入
【发布时间】:2013-03-04 14:07:42
【问题描述】:

我想使用 python 将 .dbf 文件转换为 .xls。我引用了这个snippet,但是我无法使用此代码编写第一个非标题行:

    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")

如何获取要写入的第一个非标题行?

谢谢

【问题讨论】:

    标签: python excel type-conversion dbf


    【解决方案1】:

    您错过了第一行 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])
    

    请注意,所指的狙击手也没有在范围内添加 1

    【讨论】:

    • 这给了我以下错误:异常:尝试覆盖单元格:sheetname=u'Sheet 1' rowx=0 colx=0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2017-09-08
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    相关资源
    最近更新 更多