-
python
模块介绍
- xlwt
创建
xls
文件(
ex
cel
)
2013-06-24
磁针石
转自:
/snake-hand/p/
31.1 xlwt
–
实例
.........................
..................................................
..................................................
..
1
31.1.1
创建简单的
excel
文件
.........
..................................................
.....................................
1
31.1.2
插入图片
.
................................
..................................................
....................................
2
使用
insert_bitmap<
/p>
来插入图片。
.
.................................................
................................
3
31.1.3
设置样式
.
................................
..................................................
....................................
3
31.1.4
更多实例
.
................................
..................................................
....................................
4
输出了红色的
”Test”
,并在第<
/p>
3
行包含了公式:
..........
...............................................
4
这里另有一个公式的实例:
p>
.
.............................
..................................................
...........
5
日期格式的实例
.
< br>............................................... .................................................. .............
6
下面展示了不同边框和删除样式的字体:
.
....................................
..............................
7
下面展示了不同颜色的字体,可以作为颜色参考:
............................................ .......
7
通过
unico
de
输出字符
.....................
..................................................
.............................
9
冻结:
.
.
..................................................
..................................................
.......................
10
更多的公式:
.
................................................ .................................................. ..............
12
保护实例:
.
.................................................
..................................................
.................
13
31.1.5
完整实例
.
..................................................
..................................................
................
16
31.1 xlwt
–
实例
31.1.1
创建简单的
excel
文件
下面例子,创建一个名为
的文件,它有
一个空
sheet
:
'xlwt
was here'
。
from
xlwt import *
w =
Workbook()
ws = _sheet('xlwt was here')
('')
Workbook
类初始化时有
encoding
和
style_compression
参数。
<
/p>
encoding
,
设置字符编码,
p>
一般要这样设置:
w = Workbook(encoding=
'utf-8')
,
就可以在
exce
l
中输出中文了。默认是
ascii
。
当然要记得在文件头部添加:
#!/usr/bin/env python
# -*-
coding: utf-8 -*-
style_compression
表示是否压缩,不常用。
Workbook
还有一些属性:
Owner
设置文档所有者。
country_code
:国家码
wnd_protect
:窗口保护
obj_protect
:对象保护
Protect
:保护
backup_on_save
:保存时备份
Hpos
:横坐标
Vpos
:纵坐标
Width
:宽度
Height
:高度
active_sheet
:活动
sheet
tab_width
:
tab
宽度
wnd_visible
:窗口是否可见
wnd_mini
:窗口最小化
p>
hscroll_visible
:横向滚动条是否可见。
vscroll_visible
:纵向滚动
条是否可见。
tabs_visible
:
tab
是否可见。
dates_1904
:是否使用
1904
日期系统
use_cell_values
:单元格的值
default_st
yle
:默认样式
colour_RGB
:颜色
比如设置国家码:
From
xlwt import *
w=
Workbook()
y_code= 61
ws=
_sheet('AU')
('')
方法有
:
add_style
,<
/p>
add_font
,
add_str
p>
,
del_str
,
str_index
,
add_rt
,
rt_index
,
add_she
et
,
get_sheet
,
raise_bad_sheetname
,
c
onvert_sheetindex
,
setup_xcal
l
,
add_sheet_reference
。
31.1.2
插入图片
add_sheet
会返回一个
Worksheet
类。
创建的时候有可选参数
cell_overwrite_ok<
/p>
,
表示是否可
以覆盖单元格,其实是
p>
Worksheet
实例化的一个参数,默认值是
< br>False
。
Worksheet
初始化的参数有
:
sheetname
,
parent_
book
,
cell_overwrite_ok
。
Worksheet
的
属性有
:
Row
,
Column
,
explicit_magn_setti
ng
(默认
False
),
visibility
(默认
0
< br>),
split_position_units_are_twips
(默认
False
),
row_default_height_mismatch
,
row_default_hidden
,
row_defau
lt_space_above
,
row_default_s
pace_below
,
last_used_row
,
first_used_row
,
last_used_col
,
row_temp
file
。以上属性类定义中。
函数
构成的属性有
:
name
,
parent
(只读),
rows
(只读),
cols
(只读),
merged_ranges
(只读),
bmp_rec
p>
(只读),
show_formulas
,
show_grid
,
show_he
aders
,
panes_frozen
,
auto_colour_grid
,
cols_right_to_left
,
show_ou
tline
,
remove_splits
,
selected
,
sheet
_visible
,
page_preview
,
first_visible_row
,
first_visible_col
,
grid
_colour
,
preview_magn
< br>,
normal_magn
,
s
cl_magn
,
vert_split_pos
,
horz_split_pos
,
vert_split_first_visible
,
horz_split_first_visible
,
sh
ow_auto_page_breaks
,
dialogue
_sheet
,
auto_style_outline
,
outline_below
,
outline_right
,
fit_num_
pages
,
show_row_outline
,
show_col_outline
,
alt_expr_eval
,
alt_form
ula_entries
,
row_default_heig
ht
,
col_default_width
< br>,
calc_mode
,
cal
c_count
,
RC_ref_mode
,
iterations_on
,
delta
,
save_recalc
,
print_headers
,
pr
int_grid
,
vert_page_breaks
,
horz_page_breaks
,
p>
header_str
,
footer_s
tr
,
print_centered_vert
,
print_centered_horz
,
p>
left_margin
,
right_m
argin
,
top_margin
,
bottom_margin
,
pap
er_size_code
,
print_scaling
p>
,
start_page_number
,
fit_width_to_pages
,
fit_height_to_pages
,
print_
in_rows
,
portrait
,
print_colour
,
prin
t_draft
,
print_notes
,
print_notes_at_end
,
print_omit_errors
,
print
_hres
,
print_vres
,
header_margin
,
foo
ter_margin
,
copies_num
,
wnd_protect
,
obj_protect
,
protect
,
scen_protect
,
p
assword
。
方法有:
get_parent
,
write
,
write_rich_text
,
merge
,
write_merge
,
insert_bitmap
,
col
,
row
,
row_height
,
col_width
p>
。
使用
insert_bitmap
来插入图片。
from xlwt import
*
,
w
= Workbook()
ws = _sheet('Image')
_bitmap('', 2, 2)
_bitmap('', 10, 2)
('')
31.1.3
设置样式
改变字体的高度
:
#!/usr/bin/envpython
#-*- coding: utf-8 -*-
#Copyright (C) 2005 Kiseliov Roman
fromxlwt import *
w=
Workbook(encoding='utf-8')
ws=
_sheet('Hey, Dude')
fori
in range(6, 80):
fnt =
Font()
= i*20
style = XFStyle()
= fnt
(i, 1, '
武冈
')
(i).set_style(style)
('row_')
XFStyle
用于设置字体样式,有描述字符串
num_format_str
,字体
font
p>
,居中
alignment
,边界
borders
,模式
pattern
,保护
protection
等属性。另外还可
以不写单元格,
直
接设置格式
,比如:
from pyExcelerator import *
w= Workbook()
ws= _sheet('Hey, Dude')
fori in range(6, 80):
fnt = Font()
= i*20
style = XFStyle()
= fnt
(i).set_style(style)
('row_styles_')
设置列宽:
(i).width = 0x0d00 + i
31.1.4
更多实例
输出了红色的”Test”,并在第
3
行包含了公
式:
importxlwt
fromdatetime import datetime
font0= ()
=
'Times New Roman'
_index= 2
= True
style0=
e()
= font0
style1= e()
_format_str=
'D-MMM-YY'
wb= ok()
ws= _sheet('A Test Sheet')
(0,0, 'Test', style0)
(1,0,
(), style1)
(2,0, 1)
(2,1,
1)
(2,2, a(
('')
这里另有一个公式的实例:
fromxlwt import ExcelFormulaParser,
ExcelFormula
importsys
f= a(
A
VERAGE((1.80+
2.898 * 1)/(1.80 + 2.898);
(1.80 + 2.898 * 1)/(1.80 + 2.898);
(1.80 +
2.898 * 1)/(1.80 + 2.898)) +
SIN(PI()/4)
合并单元格的实例
注意
write_merge,1,2
个参数表示行数,
3,4
的参数表示列数。
fromxlwt import *
wb= Workbook()
ws0=
_sheet('sheet0')
fnt= Font()
= 'Arial'
_index= 4
= True
borders= Borders()
= 6
= 6
= 6
= 6
style= XFStyle()
= fnt
s = borders
_merge(3,3, 1, 5, 'test1',
style)
_merge(4,10, 1, 5, 'test2',
style)
(1).width= 0x0d00
('')
日期格式的实例
fromxlwt import *
fromdatetime import datetime
w= Workbook()
ws= _sheet('Hey, Dude')
fmts= [
'M/D/YY',
'D-MMM-YY',
'D-MMM',
'MMM-YY',
'h:mm AM/PM',
'h:mm:ss AM/PM',
'h:mm',
'h:mm:ss',
'M/D/YY h:mm',
'mm:ss',
'[h]:mm:ss',
'mm:ss.0',
]
i= 0
forfmt in fmts:
(i, 0, fmt)
style = XFStyle()
_format_str = fmt
(i, 4, (),
style)
i +=
1
('')
下面展示了不同边框和删除样式的字体:
fromxlwt import *
font0= Font()
=
'Times New Roman'
_out= True
= True
style0=
XFStyle()
= font0
wb= Workbook()
ws0= _sheet('0')
(1,1, 'Test', style0)
fori in range(0, 0x53):
borders = Borders()
= i
= i
= i
= i
style = XFStyle()
s = borders
(i, 2, '', style)
(i, 3, hex(i), style0)
_merge(5,8, 6, 10,
('')
下面展示了不同颜色的字体,可以作为颜色参考:
from xlwt import *
font0 = Font()
= 'Times New Roman'
_out = True
= True
style0
= XFStyle()
= font0
wb = Workbook()
ws0 = _sheet('0')
(1, 1, 'Test', style0)
for i in range(0, 0x53):
fnt = Font()
= 'Arial'
_index = i
e = True
borders = Borders()
= i
style = XFStyle()
= fnt
s =borders
(i, 2,'colour', style)
(i, 3,hex(i), style0)
('')
超级链接的插入方法
:
fromxlwt import *
f= Font()
=
20*72
= 'Verdana'
= True
ine= INE_DOUBLE
_index= 4
h_style= XFStyle()
h_= f
w=
Workbook()
ws= _sheet('F')
##############
##NOTE:
parameters are separated by semicolon!!!
##############
n=
_merge(1,1, 1, 10,
Formula(n
+'(
_merge(2,2, 2,
25, Formula(n
+'(
!
(
通过
u
nicode
输出字符
在没有指定编码的情况下,
也可以通过
unicode
输出字符,
不过这样比较费劲,
建议还
是使用
utf-8
编码
:
fromxlwt
import *
w= Workbook()
ws1= _sheet(u'N{GREEK SMALL LETTER
ALPHA}N{GREEK SMALL LETTER
BETA}N{GREEKSMALL LETTER GAMMA}')
(0,0, u'N{GREEK SMALL
LETTER ALPHA}N{GREEK SMALL LETTER
BETA}N{GREEK SMALLLETTER GAMMA}')
(1,1, u'N{GREEK SMALL LETTER DELTA}x =
1 + N{GREEK SMALL LETTER
DELTA}')
(2,0,u'Au2262u0391.')
# RFC2152 example
(3,0,u'Hi
Mom -u263a-!')
# RFC2152 example
(4,0,u'u65E5u672Cu8A9E') # RFC2152
example
(5,0,u'Item 3 is u00a31.') #
RFC2152 example
(8,0,u'N{INTEGRAL}')
# RFC2152 example