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

Copying data in all sheets, python xlsxwriter

我想用输入函数创建表和多个 sheet,但它只把数据放在最后 sheet。如何将数据放入每个 sheet,除了第一次调用 'description' sheet.

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 中的输入数据?

如果你想在每个作品sheet上写,你应该让程序在指定的sheet上工作。在您的代码中,for 循环已经完成,因此该程序仅在最后一个 sheet 活动时打印您的数据。

这是我的脚本,用于在除描述之外的每个 sheet 中打印它。

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