How to write data to an excel file?

后端 未结 4 1634
说谎
说谎 2021-01-01 06:33

I have some data that I\'d like to save in an excel file. How does one do this in python?

相关标签:
4条回答
  • 2021-01-01 06:34

    if it's running on windows, try creating an instance of EXCEL.APPLICATION via COM

    Use Excel Help for the object reference.

    This way you can even format the data, write formulas, etc.

    0 讨论(0)
  • 2021-01-01 06:36

    There's a great python module called XLWT. I'd recommend using that... it writes native Excel files instead of CSVs. Supports formulas, etc too.

    Documentation (borrowed from Mark)

    0 讨论(0)
  • 2021-01-01 06:37

    If you want a BIFF8 XLS file, I would use the excellent xlwt.

    0 讨论(0)
  • 2021-01-01 06:39

    I'll answer a slightly different question: "How can I write data so that Excel can read it?"

    Use the csv module to write your data as a .csv file, and then open it in Excel.

    import csv
    csvout = csv.writer(open("mydata.csv", "wb"))
    csvout.writerow(("Country", "Year"))
    for coutry, year in my_data_iterable():
        csvout.writerow((country, year))
    
    0 讨论(0)
提交回复
热议问题