python xlwt库的详细函数介绍_python工具库介绍-xlwt 创建xls文件(excel)

本文详细介绍了Python的xlwt库,用于创建Excel文件。内容包括Workbook对象的属性和方法,Worksheet的创建与操作,插入图片,设置样式,公式,日期格式,边框,字体颜色,超级链接,以及文件的保护等。提供了多个示例代码展示如何使用xlwt创建和格式化Excel文件。
摘要由CSDN通过智能技术生成

简介

参考资料:

下载地址:xlwt

文件备份地址:qq群:630011153

技术支持qq群: 144081101 591302926 567351477 钉钉免费群:21745728

快速入门

下面例子,创建一个名为mini.xls的文件,它有一个空sheet:'xlwt was here'。代码见mini.py。

from xlwt import *

w = Workbook()

ws = w.add_sheet('xlwt was here')

w.save('mini.xls')

Workbook类初始化时有encoding和style_compression参数。

encoding,设置字符编码,一般要这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。默认是ascii。当然要记得在文件头部添加:

#!/usr/bin/env python3

# -*- 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:窗口最小化

hscroll_visible:横向滚动条是否可见。

vscroll_visible:纵向滚动条是否可见。

tabs_visible:tab是否可见。

dates_1904:是否使用1904日期系统

use_cell_values:单元格的值

default_style:默认样式

colour_RGB:颜色

比如设置国家码,代码:country.py:

from xlwt import *

w = Workbook()

w.country_code = 61

ws = w.add_sheet('AU')

w.save('country.xls')

方法有:add_style,add_font,add_str,del_str,str_index,add_rt,rt_index,add_sheet,get_sheet,raise_bad_sheetname,convert_sheetindex,setup_xcall,add_sheet_reference。

插入图片

add_sheet 会返回一个Worksheet 类。创建的时候有可选参数cell_overwrite_ok,表示是否可以覆盖单元格,其实是Worksheet实例化的一个参数,默认值是False。

Worksheet初始化的参数有sheetname,parent_book,cell_overwrite_ok。

Worksheet的属性有:Row,Column,explicit_magn_setting(默认False),visibility(默认0),split_position_units_are_twips(默认False),row_default_height_mismatch,row_default_hidden,row_default_space_above,row_default_space_below,last_used_row,first_used_row,last_used_col,row_tempfile。以上属性类定义中。

函数构成的属性有:name,parent(只读),rows(只读),cols(只读),merged_ranges(只读),bmp_rec(只读),show_formulas,show_grid,show_headers,panes_frozen,auto_colour_grid,cols_right_to_left,show_outline,remove_splits,selected,sheet_visible,page_preview,first_visible_row,first_visible_col,grid_colour,preview_magn,normal_magn,scl_magn,vert_split_pos,horz_split_pos,vert_split_first_visible,horz_split_first_visible,show_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_formula_entries,row_default_height,col_default_width,calc_mode,calc_count,RC_ref_mode,iterations_on,delta,save_recalc,print_headers,print_grid,vert_page_breaks,horz_page_breaks,header_str,footer_str,print_centered_vert,print_centered_horz,left_margin,right_margin,top_margin,bottom_margin,paper_size_code,print_scaling,start_page_number,fit_width_to_pages,fit_height_to_pages,print_in_rows,portrait,print_colour,print_draft,print_notes,print_notes_at_end,print_omit_errors,print_hres,print_vres,header_margin,footer_margin,copies_num,wnd_protect,obj_protect,protect,scen_protect,password。

方法有:get_parent,write,write_rich_text,merge,write_merge,insert_bitmap,col,row,row_height,col_width。

下面例子使用 insert_bitmap来插入图片。代码:image.py:

from xlwt import *,

w = Workbook()

ws = w.add_sheet('Image')

ws.insert_bitmap('python.bmp', 2, 2)

ws.insert_bitmap('python.bmp', 10, 2)

w.save('image.xls')

设置样式

下面例子改变字体的高度。代码:row_styles.py:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# Copyright (C) 2005 Kiseliov Roman

from xlwt import *

w = Workbook(encoding='utf-8')

<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值