将数据写入工作表

时间:2019-03-26 13:09:14

标签: python xlsxwriter

我创建了一个新的Excel工作表,并向其中添加了12个工作表。我试图将数据写入这些新创建的工作表中,但是,它只允许我将数据写入到工作表8中。之后,其余工作表仅具有我在循环中指定的标题。请注意,直到下面的代码第12页,我才将所有数据写入工作都包括在内,因为这是多余的。

import os
import xlsxwriter
from datetime import datetime

report_date = datetime.today()

Workbook = xlsxwriter.Workbook("C:\\Users\SChogle\\Documents\\Python Projects\\Online Market Values1.xlsx")
Worksheet1 = Workbook.add_worksheet("xxx")
Worksheet2 = Workbook.add_worksheet("xxx")
Worksheet3 = Workbook.add_worksheet("xxx")
Worksheet4 = Workbook.add_worksheet("xxx")
Worksheet5 = Workbook.add_worksheet("xxx")
Worksheet6 = Workbook.add_worksheet("xxx")
Worksheet7 = Workbook.add_worksheet("xxx")
Worksheet8 = Workbook.add_worksheet("xxx")
Worksheet9 = Workbook.add_worksheet("xxx")
Worksheet10 = Workbook.add_worksheet("xxx")
Worksheet11 = Workbook.add_worksheet("xxx")
Worksheet12 = Workbook.add_worksheet("xxx")

for worksheet in Workbook.worksheets():
    worksheet.write('A1', "Fund")
    worksheet.write('B1', "AssetManager")
    worksheet.write('C1', "AssetManagerPortfolio")
    worksheet.write('D1', "ReportDate")
    worksheet.write('E1', "Currency")
    worksheet.write('F1', "NettMarketValue")
    worksheet.set_column('A:A', 30)
    worksheet.set_column('B:B', 30)
    worksheet.set_column('C:C', 30)
    worksheet.set_column('D:D', 30)
    worksheet.set_column('E:E', 30)
    worksheet.set_column('F:F', 30)


Worksheet1.write("A2", "xxxxx")
Worksheet1.write("A3", "xxxxx")
Worksheet1.write("A4", "xxxxx")
Worksheet1.write("A5", "xxxxx")
Worksheet1.write("B2", "xxxxx")

Worksheet2.write("A2", "xxxxx")
Worksheet2.write("A3", "xxxxx")
Worksheet2.write("A4", "xxxxx")
Worksheet2.write("A5", "xxxxx")
Worksheet2.write("B2", "xxxxx")

Worksheet3.write("A2", "xxxxx")
Worksheet3.write("A3", "xxxxx")
Worksheet3.write("A4", "xxxxx")
Worksheet3.write("A5", "xxxxx")
Worksheet3.write("B2", "xxxxx")

Worksheet4.write("A2", "xxxxx")
Worksheet4.write("A3", "xxxxx")
Worksheet4.write("A4", "xxxxx")
Worksheet4.write("A5", "xxxxx")
Worksheet4.write("B2", "xxxxx")

Worksheet5.write("A2", "xxxxx")
Worksheet5.write("A3", "xxxxx")
Worksheet5.write("A4", "xxxxx")
Worksheet5.write("A5", "xxxxx")
Worksheet5.write("B2", "xxxxx")

Worksheet6.write("A2", "xxxxx")
Worksheet6.write("A3", "xxxxx")
Worksheet6.write("A4", "xxxxx")
Worksheet6.write("A5", "xxxxx")
Worksheet6.write("B2", "xxxxx")

Worksheet7.write("A2", "xxxxx")
Worksheet7.write("A3", "xxxxx")
Worksheet7.write("A4", "xxxxx")
Worksheet7.write("A5", "xxxxx")
Worksheet7.write("B2", "xxxxx")

Worksheet8.write("A2", "xxxxx")
Worksheet8.write("A3", "xxxxx")
Worksheet8.write("A4", "xxxxx")
Worksheet8.write("A5", "xxxxx")
Worksheet8.write("B2", "xxxxx")
Worksheet8.write("A2", "xxxxx")
Worksheet8.write("A3", "xxxxx")
Worksheet8.write("A4", "xxxxx")
Worksheet8.write("B2", "xxxxx")
Worksheet8.write("B3", "xxxxx")

Workbook.close()

Worksheet9.write("A2", "xxxxx")
Worksheet9.write("A3", "xxxxx")
Worksheet9.write("A4", "xxxxx")
Worksheet9.write("A5", "xxxxx")
Worksheet9.write("B2", "xxxxx")

Workbook.close()

file = "C:\\Users\SChogle\\Documents\\Python Projects\\Online Market Values1.xlsx"
os.startfile(file)

2 个答案:

答案 0 :(得分:2)

这是因为您在写入PersonView之后关闭了工作簿。写入Worksheet8之前,先删除Workbook.close(),它应该可以工作。

答案 1 :(得分:2)

由于此行:

# worksheet 8 things

Workbook.close()

# worksheet 9 things

您关闭工作簿,然后尝试使用它。如果删除此行,它将起作用。