Python和Excel交互

Python和Excel交互

使用的python包为XlsxWriter
下载的链接
https://pypi.python.org/pypi/XlsxWriter

初级的例子:

def write_excel(info_list,colums,name,sheetname):
    import xlsxwriter

    workbook = xlsxwriter.Workbook(name)

    worksheet = workbook.add_worksheet(sheetname)

    info_length = len(info_list)
    col_len = len(colums)
    print info_length

    for row in range(0, info_length):
        row_value = info_list[row]

        if isinstance(row_value, dict):
            for col in range(0, col_len):
                value = info_list[row][colums[col]]
                worksheet.write(row, col, value)
        else:
           worksheet.write(row,0,row_value)
    workbook.close()

调用的方法:

columns =  ['port','cpname','dba','developer']
file_name = 'weibo_offline.xlsx'
sheet_name =  'weibo_offline'
write_excel(info_list, columns, file_name, sheet_name)
posted @ 2015-04-14 14:44  小郭学路  阅读(863)  评论(0编辑  收藏  举报