复制所有工作表中的数据,python xlsxwriter

ssm49v7z  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(369)

我想用输入函数创建表格和多个工作表,但它只把数据放在最后一个工作表中。如何将数据放入每个工作表中,除了第一个“说明”工作表之外。

import xlsxwriter
from xlsxwriter import workbook

workbook = xlsxwriter.Workbook('test.xlsx')

worksheet_description = workbook.add_worksheet( "description" )
sheetnames = input("Enter (sheets) names: ").split(", ")
for i in sheetnames:
    worksheet_data = workbook.add_worksheet(i)

worksheet_data.write('A1', 'Number of table rows')

data = []
year = input("Enter years: ").split(", ")
q = ['q1 ' , 'q2 ', 'q3 ', 'q4 ']
for x in year:
    for y in q:
        data.append(x + y)
worksheet_data.write_row(0, 1, data)

col = input("Numerate columns (enter value): ").split(", ")
for i in col:
    worksheet_data.write_column(1, 0, col)
workbook.close()

为什么它不打印所有的输入数据 worksheet_data ?

vh0rcniy

vh0rcniy1#

如果要在每个工作表上写入,则应使程序在指定的工作表上运行。在你的代码中 for 循环已完成,因此程序仅在激活的最后一张图纸上打印数据。
这是我的脚本,可以在每张纸上打印,除了说明。

import xlsxwriter
from xlsxwriter import workbook

workbook = xlsxwriter.Workbook('test.xlsx')

worksheet_description = workbook.add_worksheet( "description" )

sheetnames = input("Enter (sheets) names: ").split(", ")
year = input("Enter years: ").split(", ")
col = input("Numerate columns (enter value): ").split(", ")

for i in sheetnames:    # Loop for each sheet
    worksheet_data = workbook.add_worksheet(i)

    worksheet_data.write('A1', 'Number of table rows')

    data = []
    q = ['q1 ' , 'q2 ', 'q3 ', 'q4 ']
    for x in year:
        for y in q:
            data.append(x + y)
    worksheet_data.write_row(0, 1, data)

    for i in col:
        worksheet_data.write_column(1, 0, col)

workbook.close()

相关问题