基本部分
在寫入Excel表格以前,你必須初始化workbook對象,而後添加一個workbook對象。好比:python
1
2
3
|
import
xlwt
wbk
=
xlwt.Workbook()
sheet
=
wbk.add_sheet(
'sheet 1'
)
|
這樣表單就被建立了,寫入數據也很簡單:工具
1
2
|
# indexing is zero based, row then column
sheet.write(
0
,
1
,
'test text'
)
|
以後,就能夠保存文件(這裏不須要想打開文件同樣須要close文件):oop
1
|
wbk.save(
'test.xls'
)
|
深刻探索
worksheet對象,當你更改表單內容的時候,會有警告提示。post
1
2
3
4
5
6
|
sheet.write(
0
,
0
,
'test'
)
sheet.write(
0
,
0
,
'oops'
)
# returns error:
# Exception: Attempt to overwrite cell:
# sheetname=u'sheet 1' rowx=0 colx=0
|
解決方式:使用cell_overwrite_ok=True來建立worksheet:
ui
1
2
3
|
sheet2
=
wbk.add_sheet(
'sheet 2'
, cell_overwrite_ok
=
True
)
sheet2.write(
0
,
0
,
'some text'
)
sheet2.write(
0
,
0
,
'this should overwrite'
)
|
這樣你就能夠更改表單2的內容了。this
更多
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# Initialize a style
style
=
xlwt.XFStyle()
# Create a font to use with the style
font
=
xlwt.Font()
font.name
=
'Times New Roman'
font.bold
=
True
# Set the style's font to this new one you set up
style.font
=
font
# Use the style when writing
sheet.write(
0
,
0
,
'some bold Times text'
, style)
|
xlwt 容許你每一個格子或者整行地設置格式。還能夠容許你添加連接以及公式。其實你能夠閱讀源代碼,那裏有不少例子:spa
dates.py
, 展現如何設置不一樣的數據格式hyperlinks.py
, 展現如何建立超連接 (hint: you need to use a formula)merged.py
, 展現如何合併格子row_styles.py
, 展現如何應用Style到整行格子中.
例子
這裏演示的數據並非很容直接導入Excel:excel
20 Sep, 263, 1148, 0, 1, 0, 0, 1, 12.1, 13.9, 1+1, 19.9
20 Sep, 263, 1118, 0, 1, 0, 360, 0, 14.1, 15.3, 1+1, 19.9
20 Sep, 263, 1048, 0, 1, 0, 0, 0, 14.2, 15.1, 1+1, 19.9
20 Sep, 263, 1018, 0, 1, 0, 360, 0, 14.2, 15.9, 1+1, 19.9
20 Sep, 263, 0948, 0, 1, 0, 0, 0, 14.4, 15.3, 1+1, 19.9code
第一個逗號以前數據表示日期,第二列表示今年的第幾天(可忽略),咱們感興趣的是第九列的溫度數據。咱們的目的是把感興趣的數字寫入Excel: 第一列爲時間,第二列爲溫度。首先你要把上面的數據保存在一個weather.data.exampl
e文件中。orm
而後運行下面的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
'''
Script to convert awkwardly-formatted weather data
into an Excel spreadsheet using Python and xlwt.
'''
from
datetime
import
datetime
import
xlwt
# Create workbook and worksheet
wbk
=
xlwt.Workbook()
sheet
=
wbk.add_sheet(
'temperatures'
)
# Set up a date format style to use in the
# spreadsheet
excel_date_fmt
=
'M/D/YY h:mm'
style
=
xlwt.XFStyle()
style.num_format_str
=
excel_date_fmt
# Weather data has no year, so assume it's the current year.
year
=
datetime.now().year
# Convert year to a string because we'll be
# building a date string below
year
=
str
(year)
# The format of the date string we'll be building
python_str_date_fmt
=
'%d %b-%H%M-%Y'
row
=
0
# row counter
f
=
open
(
'c:/baidu/weather.data.example'
)
for
line
in
f:
# separate fields by commas
L
=
line.rstrip().split(
','
)
# skip this line if all fields not present
if
len
(L) <
12
:
continue
# Fields have leading spaces, so strip 'em
date
=
L[
0
].strip()
time
=
L[
2
].strip()
# Datatypes matter. If we kept this as a string
# in Python, it would be a string in the Excel sheet.
temperature
=
float
(L[
8
])
# Construct a date string based on the string
# date format we specified above
date_string
=
date
+
'-'
+
time
+
'-'
+
year
# Use the newly constructed string to create a
# datetime object
date_object
=
datetime.strptime(date_string,
python_str_date_fmt)
# Write the data, using the style defined above.
sheet.write(row,
0
,date_object, style)
sheet.write(row,
1
,temperature)
row
+
=
1
wbk.save(
'c:/baidu/reformatted.data.xls'
)
|
首先,打開workbook;
1
2
|
import
xlrd
wb
=
xlrd.open_workbook(
'myworkbook.xls'
)
|
檢查表單名字:
1
|
wb.sheet_names()
|
獲得第一張表單,兩種方式:索引和名字
1
2
|
sh
=
wb.sheet_by_index(
0
)
sh
=
wb.sheet_by_name(u
'Sheet1'
)
|
遞歸打印出每行的信息:
1
2
|
for
rownum
in
range
(sh.nrows):
print
sh.row_values(rownum)
|
若是隻想返回第一列數據:
1
|
first_column
=
sh.col_values(
0
)
|
經過索引讀取數據:
1
2
|
cell_A1
=
sh.cell(
0
,
0
).value
cell_C4
=
sh.cell(rowx
=
3
,colx
=
2
).value
|
注意:這裏的索引都是從0開始的。
這裏給個完整的例子:
1
2
3
4
5
6
7
8
|
import
xlrd
wb
=
xlrd.open_workbook(
'c:/baidu/hello.xls'
)
sh
=
wb.sheet_by_index(
0
)
for
rownum
in
range
(sh.nrows):
print
sh.row_values(rownum)
print
sh.cell(
3
,
4
).value
|
執行結果:
C:\Development\python26>python c:/baidu/xlrsExcel.py [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] [2.0, 3.0, 4.0, 5.0, 6.0, 7.0] [3.0, 4.0, 5.0, 6.0, 7.0, 8.0] [4.0, 5.0, 6.0, 7.0, 8.0, 9.0] [5.0, 6.0, 7.0, 8.0, 9.0, 10.0] [6.0, 7.0, 8.0, 9.0, 10.0, 11.0] [7.0, 8.0, 9.0, 10.0, 11.0, 12.0] [8.0, 9.0, 10.0, 11.0, 12.0, 13.0] [9.0, 10.0, 11.0, 12.0, 13.0, 14.0] [10.0, 11.0, 12.0, 13.0, 14.0, 15.0] 8.0