Write Excel files with Python using xlwt
2011-12-20 22:10
543 查看
In a previous post (which turned out to be pretty popular) I showed you how to read Excel
files with Python. Now for the reverse: writing Excel files.
First, you’ll need to install the xlwt package by John Machin.
Now that the sheet is created, it’s very easy to write data to it.
When you’re done, save the workbook (you don’t have to close it like you do with a file object)
To change this behavior, use the
Now you can overwrite sheet 2 (but not sheet 1).
xlwt allows you to format your spreadsheets on a cell-by-cell basis or by entire rows; it also allows you to add hyperlinks or even formulas. Rather than recap it all here, I encourage you to grab a copy of the source code, in
which you can find the examples directory. Some highlights from the examples directory in the source code:
The first column has the day and month separated by a space. The second column is year-day, which we’ll ignore. The third column has the time. The data we’re interested in is in the 9th column (temperature). The goal is to have
a simple Excel file where the first column is date, and the second column is temperature.
Here’s a [heavily commented] script to do just that. It assumes that you have the data saved as
files with Python. Now for the reverse: writing Excel files.
First, you’ll need to install the xlwt package by John Machin.
The basics
In order to write data to an Excel spreadsheet, first you have to initialize a Workbook object and then add a Worksheet object to that Workbook. It goes something like this:Digging deeper
Overwriting cells
Worksheet objects, by default, give you a warning when you try to overwrite:cell_overwrite_ok=Truekwarg when creating the worksheet, like so:
More goodies
which you can find the examples directory. Some highlights from the examples directory in the source code:
dates.py, which shows how to use the different date formats
hyperlinks.py, which shows how to create hyperlinks (hint: you need to use a formula)
merged.py, which shows how to merge cells
row_styles.py, which shows how to apply styles to entire rows.
Non-trivial example
Here’s an example of some data where the dates not formatted well for easy import into Excel:a simple Excel file where the first column is date, and the second column is temperature.
Here’s a [heavily commented] script to do just that. It assumes that you have the data saved as
weather.data.example.
相关文章推荐
- write excel with python xlwt
- Read & Write Excel files with Python
- Read and write excel file with format by pandas and xlwt libraries of python
- Working with Excel Files in Python
- Examples Generating Excel Documents Using Python’s xlwt
- Examples Generating Excel Documents Using Python’s xlwt
- read excel files with python and xlrd
- 自己总结python用xlrd\xlwt读写excel
- 使用python读写操作同一个excel(xlrd,xlwt,xlutils)
- 基于Problem Solving with Algorithms and Data Structures using Python的学习记录(6-1)——Tree
- python使用xlwt读取文件写入Excel
- 【python练习】 xlwt 操作excel 写入list数据并考虑合并单元格
- Python:使用第三方库xlwt来写Excel
- 使用Python和xlwt向Excel文件中写入中文
- Reading Excel files using ODBC
- Python 使用 xlrd/xlwt 操作 Excel
- Python 使用xlwt模块操作Excel写
- Python Excel操作模块XlsxWriter之写入worksheet.write()
- Generate mp4 video with image files using MediaCodec in Android