xlwt 根据输入创建动态数量的工作表 [英] xlwt create dynamic number of worksheets based on input

查看:34
本文介绍了xlwt 根据输入创建动态数量的工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我事先知道我需要多少工作表,我知道如何创建多个工作表并写信给他们.例如,如果我只有 3 个工作表,则此代码工作正常.(我可以使用 for 循环来指定每个工作表的内容):

I know how to create multiple worksheets and write to them if I know in advance how many worksheets I will need. For example, this code works fine if I only have 3 worksheets. (I can use a for loop to dictate the contents of each worksheet):

import xlwt as xlwt

workbook = xlwt.Workbook()
style = xlwt.XFStyle()

ws1 = workbook.add_sheet("Worksheet 1")
ws2 = workbook.add_sheet('Worksheet 2')
ws3 = workbook.add_sheet('Worksheet 3')

# For loop to dictate contents of each worksheet here...
worksheet = ws1
worksheet.write(1, 1, 'Welcome to Worksheet 1', style)

worksheet = ws2
worksheet.write(1, 1, 'Welcome to Worksheet 2', style)

workbook.save(r'C:\Users\...\Desktop\...\mult_worksheets.xls')

但是,我想要做的是收集数据并创建新的工作表,我可以根据收集的数据集数量写入这些工作表.我不会提前知道需要创建和写入多少工作表.它可能是 1 个数据集或最多 10 个.

However, what I want to do is gather data and create new worksheets that I can write to based on the number of datasets gathered. I won't know ahead of time how many worksheets I will need to create and write to. It may be 1 dataset or up to 10.

我知道当您调用 workbook.add_sheet('Name') 时会创建工作表对象.例如:

I know that worksheet objects are created when you call workbook.add_sheet('Name'). For example:

ws1 = workbook.add_sheet('Worksheet 1')

我需要工作表实例ws1"才能写入该工作表.如何仅创建所需数量的工作表实例并能够写入它们?

I need the worksheet instance "ws1" in order to write to that worksheet. How can I create only the number of worksheet instances that I need and be able to write to them?

如果我收集了一组 4 个数据集,我需要创建 4 个工作表,工作表的名称和内容由数据集决定.

If I gather a set of 4 datasets, I need to create 4 worksheets, with the names of the worksheets and the contents determined by the datasets.

为了澄清,假设我创建了一个数据集名称列表,如下所示:

To clarify, lets say I create a list of dataset names like this:

worksheet_names = ['CA Stats', "TX Stats", "NY Stats", "FL Stats"]

这些将用作工作表的名称.

These will be used as the names of the worksheets.

每个工作表都有自己的数据.例如:

And each worksheet will have its own data. For example:

worksheet_data = ['Welcome to CA Stats', "Welcome to TX Stats", "Welcome to NY Stats", "Welcome to FL Stats"]

感谢任何帮助!

推荐答案

感谢 melipone,我现在有了问题的答案.但是,为了向看到这一点的其他人澄清,我想指出 enumerate() 与问题没有密切关系,也没有必要.像这样创建工作表后:

Thanks to melipone, I now have the answer to my question. However, to clarify for anyone else who sees this, I want to point out that enumerate() is not germane to the question and is not necessary. Once the worksheets have been created like this:

worksheet_names = ['a', 'b', 'c', 'd']

for name in worksheet_names:
    wb.add_sheet(name)

可以按照您想要的任何顺序写入任何工作表,以及您想要的任何数据.所有你需要知道的是工作表的索引位置,它可以通过ws = wb.get_sheet(index_position)"访问.例如:

It is possible to write to any of the worksheets in any order you want, with any data you want. All you need to know is the index position of the worksheet and it can be accessed with "ws = wb.get_sheet(index_position)". For example:

dataset = ['100', '200', '300', '400']

ws = wb.get_sheet(2)
ws.write(1, 1, dataset[2])
ws.write(2, 1, "This is something else I want to write in the third worksheet")

ws = wb.get_sheet(0)
ws.write(1, 1, "Cell 1, 1 contains data from a dict")
ws.write(2, 1, "This is something else I want to write in the first worksheet")

ws = wb.get_sheet(1)
ws.write(1, 1, "The data in this cell comes from a different dict")
ws.write(2, 1, "Write this in the second worksheet")

ws = wb.get_sheet(3)
ws.write(1, 1, "This is the 4th worksheet")

这篇关于xlwt 根据输入创建动态数量的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆