python导出dbf文件到excel的方法

本文实例讲述了python导出dbf文件到excel的方法。分享给大家供大家参考。具体如下:

from dbfpy import dbf
from time import sleep
from win32com import client
def dbf2xls(dbfilename, exfilename):
db = dbf.dbf(dbfilename, true)
ex = client.dispatch(‘excel.application’)
wk = ex.workbooks.add()
ws = wk.activesheet
ex.visible = true
sleep(1)
r = 1
c = 1
for field in db.fieldnames:
ws.cells(r,c).value = field
c = c+1
r = 2
for record in db:
c = 1
for field in db.fieldnames:
ws.cells(r,c).value = record[field]
c = c+1
r = r+1
wk.saveas(exfilename)
wk.close(false)
ex.application.quit()
db.close()
if __name__==’__main__’:
dbffilename = “test.dbf”
xlsfilename = “text.xls”
dbf2xls(dbffilename, xlsfilename)

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论