无法使用python xlsxwriter写表

我正在编写一个脚本来将多个表写入一个Excel工作表。 我正在使用Python 2.6和xlsxwriter来完成任务,但我无法创build一个表,并没有显示错误。 只是列名称被添加到Excel工作表。 这是我用来创build表的代码

import xlsxwriter report = xlsxwriter.Workbook('example_report.xlsx') sheet = report.add_worksheet() row_count = 0 column_count = 0 table_headers = [ {'header': 'Product'}, {'header': 'Quarter 1'}, {'header': 'Quarter 2'}, {'header': 'Quarter 3'}, {'header': 'Quarter 4'}, ] excel_write_data = [ ['Apples', 10000, 5000, 8000, 6000] ] table_row_count = row_count+len(excel_write_data) table_column_count = column_count+len(table_headers) sheet.add_table( row_count, column_count, table_row_count, table_column_count, { 'data': excel_write_data, 'columns': table_headers } ) workbook.close() 

提前致谢!

它应该按预期工作。 这里是你的代码添加到一个完整的程序:

 import xlsxwriter workbook = xlsxwriter.Workbook('tables.xlsx') worksheet = workbook.add_worksheet() row_count = 0 column_count = 0 table_headers = [ {'header': 'Product'}, {'header': 'Quarter 1'}, {'header': 'Quarter 2'}, {'header': 'Quarter 3'}, {'header': 'Quarter 4'}, ] excel_write_data = [ ['Apples', 10000, 5000, 8000, 6000], ] table_row_count = row_count + len(excel_write_data) table_column_count = column_count + len(table_headers) worksheet.add_table(row_count, column_count, table_row_count, table_column_count, {'data': excel_write_data, 'columns': table_headers}) workbook.close() 

这里是输出:

在这里输入图像说明

我会说,你应该打印出来,并debugging你提供给add_table()