简体   繁体   English

无法使用python xlsxwriter编写表

[英]unable to write tables using python xlsxwriter

I am working on a script to write multiple tables into a single Excel sheet. 我正在研究将多个表写入单个Excel工作表的脚本。 I am using python 2.6 and xlsxwriter to accomplish the task, but I am unable to create a table and no error shows up. 我正在使用python 2.6和xlsxwriter完成任务,但是我无法创建表并且没有错误显示。 Just the column names are added to the excel sheet. 仅将列名称添加到excel工作表中。 Here is the code I am using for creating the table 这是我用来创建表格的代码

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

Thanks in advance! 提前致谢!

It should work as expected. 它应该按预期工作。 Here is your code added to a complete program: 这是您添加到完整程序中的代码:

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

And here is the output: 这是输出:

在此处输入图片说明

I would say that you should print out and debug the range that you are supplying to add_table() . 我要说的是,您应该打印并调试要提供给add_table()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM