4

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

fruit
  • 115
  • 1
  • 2
  • 6

4 Answers4

11

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)

kafuchau
  • 5,573
  • 7
  • 33
  • 38
5

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))
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Note: this works, but in some locales (particularly where the decimal comma ',' is used instead of the decimal dot '.'), you'll need to use different separators (semicolons, for example). – Piskvor left the building Jun 28 '10 at 14:29
  • 2
    @Poskvor: Maybe you know this, but I'll state it for the general good. The delimiter can be changed by using a Dialect. See http://docs.python.org/library/csv.html – Fred Larson Jun 28 '10 at 14:42
1

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

Mark
  • 106,305
  • 20
  • 172
  • 230
0

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.

Alex
  • 14,338
  • 5
  • 41
  • 59