简体   繁体   English

python xlsxwriter工作表对象重用

[英]python xlsxwriter worksheet object reuse

I've written some code that iterates through a flat file. 我已经写了一些遍历平面文件的代码。 After a certain section is completed reading, I take the data and put it into a spreadsheet. 阅读完特定部分后,我将数据收集到电子表格中。 Then, I go back and continue reading the flat file for the next section and write to a new worksheet...and so on and so forth. 然后,我返回并继续阅读下一部分的平面文件,并写入新的工作表...等等。

When looping through the python code, I create a new worksheet for each section read above. 遍历python代码时,我为上面阅读的每个部分创建了一个新的工作表。 During this looping, I create the new worksheet as such: 在此循环中,我将这样创建新的工作表:

worksheet = workbook.add_worksheet(thename) 工作表= workbook.add_worksheet(名称)

The problem is that the second time through the loop, python crashes when re-assigning the worksheet object above to a new worksheet. 问题是第二遍循环,将上面的工作表对象重新分配给新工作表时,python崩溃了。 Is there a way to "close the worksheet object", then re-assign it? 有没有一种方法可以“关闭工作表对象”,然后重新分配它?

FYI: If I can't use the same object name, "worksheet" in this case, the code is going to become tremendously long and messy in order to handle "worksheet1", "worksheet2", "worksheet3", etc... (as you might imagine) 仅供参考:如果在这种情况下不能使用相同的对象名称“工作表”,则代码将变得冗长且混乱,以便处理“工作表1”,“工作表2”,“工作表3”等。 (您可能会想到)

Thank you in advance! 先感谢您!

Instead of assigning the variable worksheet to workbook.add_worksheet(thename) , have a list called worksheets . 不用将变量worksheet分配给workbook.add_worksheet(thename) ,而是有一个名为worksheets的列表。 When you normally do worksheet = workbook.add_worksheet(thename) , do worksheets.append(workbook.add_worksheet(thename)) . 通常,当您执行worksheet = workbook.add_worksheet(thename) ,请执行worksheets.append(workbook.add_worksheet(thename)) Then access your latest worksheet with worksheets[-1] . 然后使用工作worksheets[-1]访问最新的工作worksheets[-1]

The problem is that the second time through the loop, python crashes when re-assigning the worksheet object above to a new worksheet. 问题是第二遍循环,将上面的工作表对象重新分配给新工作表时,python崩溃了。

Python shouldn't crash when running an XlsxWriter program. 运行XlsxWriter程序时,Python不应崩溃。 If it does you should submit a bug report. 如果是这样,您应该提交错误报告。

You can loop over worksheets in a few different ways. 您可以通过几种不同的方式遍历工作表。 For example by creating the worksheet in the loop: 例如,通过在循环中创建工作表:

import xlsxwriter

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

for _ in range(4):

    worksheet = workbook.add_worksheet()

    worksheet.write('A1', 'Hello')

workbook.close()

Or by creating the worksheets outside the loop and then using the worksheets() method to loop over them: 或通过在循环外部创建工作表,然后使用worksheets()方法来循环worksheets()

import xlsxwriter

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

workbook.add_worksheet()
workbook.add_worksheet()
workbook.add_worksheet()
workbook.add_worksheet()

for worksheet in workbook.worksheets():
    worksheet.write('A1', 'Hello')

workbook.close()

You could also store the worksheets in a list and iterate over that. 您也可以将工作表存储在列表中并对其进行迭代。 That is essentially with the worksheets() method is doing. 这基本上是与worksheets()方法一起完成的。

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

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