繁体   English   中英

无法使用python xlsxwriter编写表

[英]unable to write tables using python xlsxwriter

我正在研究将多个表写入单个Excel工作表的脚本。 我正在使用python 2.6和xlsxwriter完成任务,但是我无法创建表并且没有错误显示。 仅将列名称添加到excel工作表中。 这是我用来创建表格的代码

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

这是输出:

在此处输入图片说明

我要说的是,您应该打印并调试要提供给add_table()

暂无
暂无

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

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