5

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

4 Answers 4

12

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)

Sign up to request clarification or add additional context in comments.

Comments

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))

2 Comments

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).
@Poskvor: Maybe you know this, but I'll state it for the general good. The delimiter can be changed by using a Dialect. See docs.python.org/library/csv.html
2

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

Comments

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.

2 Comments

I assumed it is installed, because the question was explicit about Excel.
yeah but the question was about an excel file, not using the excel application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.