码迷,mamicode.com
首页 > 编程语言 > 详细

python处理excel

时间:2015-02-10 15:24:34      阅读:2867      评论:0      收藏:0      [点我收藏+]

标签:python   excel   

python处理excel:


可以参考python官网和github上的文档。


最高支持到excel97/2000/XP/2003 XLS文件。

xlrd:用来读

xlwt:用来写和创建

xlbuiltin:用来复制,依赖xlrdxlwt


#######################################################

xlrd

#######################################################

sheetxlrd里面的sheet模块

Bookxlrd里面的Book


open_workbook(filename=None[,logfile=<open file ‘<stdout>‘, mode ‘w‘>, verbosity=0,use_mmap=1, file_contents=None, encoding_override=None,formatting_info=False, on_demand=False, ragged_rows=False]):打开一个xls文件。

cellname(rowx,colx):行和列的索引的相对的单元格的引用。

cellnameabs(rowx,colx, r1c1=0):行和列的索引的绝对的单元格的引用。

colname(colx,_A2Z=‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘):将列的索引转换成列名。

xldate

xldate_as_tuple(xldate,datemode):将一个数转换成日期格式。

error_text_from_code:根据错误码获取错误信息的字典。

biffh

biff_text_from_num

empty_cell

book

formatting

mmap


‘compdoc‘,‘count_records‘, ‘decompile_formula‘, ‘dump‘, ‘dump_formula‘, ‘‘, ‘‘,‘evaluate_name_formula‘, ‘‘, ‘formula‘, ‘info‘, ‘licences‘, ‘‘,‘okind_dict‘, ‘‘, ‘path‘, ‘pprint‘, ‘rangename3d‘, ‘rangename3drel‘,‘sys‘, ‘timemachine‘, ‘‘, ‘‘, ‘zipfile,

‘oBOOL‘,‘oERR‘, ‘oNUM‘, ‘oREF‘, ‘oREL‘, ‘oSTRG‘, ‘oUNK‘,


——————————————

xlrd.Book的属性和方法:


nsheets:一个workbook中的sheets数目。

sheet_names([self]):获取sheet名字。

sheet_by_index([self,]sheetx):根据sheet位置检索sheet

sheet_by_name([self,]sheet_name):根据sheet名字检索sheet

sheets([self]):返回workbook中的所有sheet的列表。

datemode:日期的格式。

colour_map

font_list

format_list

format_map

palette_record

style_name_map

xf_list

unload_sheet([self,]sheet_name_or_index)


biff2_8_load‘,‘biff_version‘, ‘codepage‘, ‘‘, ‘countries‘, ‘‘, ‘derive_encoding‘,‘dump‘, ‘encoding‘, ‘fake_globals_get_sheet‘, ‘get2bytes‘,‘get_record_parts‘, ‘get_record_parts_conditional‘, ‘get_sheet‘,‘get_sheets‘, ‘getbof‘, ‘handle_boundsheet‘,‘handle_builtinfmtcount‘, ‘handle_codepage‘, ‘handle_country‘,‘handle_datemode‘, ‘handle_efont‘, ‘handle_externname‘,‘handle_externsheet‘, ‘handle_filepass‘, ‘handle_font‘,‘handle_format‘, ‘handle_name‘, ‘handle_obj‘, ‘handle_palette‘,‘handle_sheethdr‘, ‘handle_sheetsoffset‘, ‘handle_sst‘,‘handle_style‘, ‘handle_supbook‘, ‘handle_writeaccess‘, ‘handle_xf‘,‘initialise_format_info‘, ‘is_date_format_string‘,‘load_time_stage_1‘, ‘load_time_stage_2‘, ‘name_and_scope_map‘,‘name_map‘, ‘name_obj_list‘, ‘names_epilogue‘‘palette_epilogue‘,‘‘, ‘parse_globals‘, ‘read‘, ‘release_resources‘, , ‘sheet_loaded‘,‘‘, ‘, ‘user_name‘, ‘xf_epilogue‘, ‘


——————————————

xlrd.sheet模块的变量、函数和类


Cellxlrd.sheet里面的类

Sheetxlrd.sheet里面的类


array‘,‘biff_count_records‘, ‘biff_dump‘, ‘biff_rec_name_dict‘,‘biff_text_from_num‘, ‘bofcodes‘, ‘boflen‘, ‘calcsize‘,‘cellty_from_fmtty‘, ‘ctype_text‘, ‘decompile_formula‘,‘dump_formula‘, ‘empty_cell‘, ‘encoding_from_codepage‘,‘ensure_unicode‘, ‘error_text_from_code‘, ‘fprintf‘, ‘hex_char_dump‘,‘is_cell_opcode‘, ‘nearest_colour_index‘, ‘print_function‘,‘python_version‘, ‘rangename2d‘, ‘sys‘, ‘unpack‘, ‘unpack_RK‘,‘unpack_cell_range_address_list_update_pos‘, ‘unpack_string‘,‘unpack_string_update_pos‘, ‘unpack_unicode‘,‘unpack_unicode_update_pos‘, ‘upkbits‘, ‘upkbitsL‘, ‘xrange‘


——————————————

xlrd.sheet.Sheet的属性和方法:

colxrowx都要从0开始计数。


nameworksheet的名字

ncolsworksheet的列数

nrowsworksheet的行数

cell([self,]rowx, colx)worksheet对应行和列上的对象。

cell_type([self,]rowx, colx):指定行列上的单元格的类型。

cell_value([self,]rowx, colx):指定行列上的单元格的值。

cell_note_map

cell_xf_index

col([self,]rowx) :返回指定列的序列

col_slice([self,]colx, start_rowx=0, end_rowx=None) :列切片

col_types([self,]colx, start_rowx=0, end_rowx=None):类型

col_values([self,]colx, start_rowx=0, end_rowx=None):值

col_label_ranges

colinfo_map

row([self,]rowx) :返回指定行的序列

row_len(self,rowx):行长度

row_slice(self,rowx, start_colx=0, end_colx=None):行切片

row_types(self,rowx, start_colx=0, end_colx=None):类型

row_values(self,rowx, start_colx=0, end_colx=None):值

row_label_ranges

rowinfo_map

computed_column_width

default_additional_space_above

default_additional_space_below

default_row_height

default_row_height_mismatch

default_row_hidden

defcolwidth

gcw

merged_cells

standardwidth


book‘,‘‘, ‘, ‘, ‘dump‘, ‘fake_XF_from_BIFF20_cell_attr‘,‘fixed_BIFF2_xfindex‘, ‘‘, ‘handle_feat11‘, ‘handle_hlink‘,‘handle_msodrawingetc‘, ‘handle_note‘, ‘handle_obj‘,‘handle_quicktip‘, ‘handle_txo‘, ‘has_pane_record‘,‘horizontal_page_breaks‘, ‘horz_split_first_visible‘,‘horz_split_pos‘, ‘hyperlink_list‘, ‘hyperlink_map‘,‘insert_new_BIFF20_xf‘, ‘‘, ‘put_cell_ragged‘, ‘put_cell_unragged‘,‘read‘, ‘req_fmt_info‘, ‘rich_text_runlist_map‘, ‘‘,‘split_active_pane‘, ‘‘, ‘string_record_contents‘, ‘tidy_dimensions‘,‘update_cooked_mag_factors‘, ‘vert_split_first_visible‘,‘vert_split_pos‘, ‘vertical_page_breaks‘, ‘visibility


——————————————

xlrd.sheet.Cell中的属性和方法:


ctype:单元格的类型:

XL_CELL_BLANK:没有值的(只有类型)的

XL_CELL_EMPTY:没有信息(值和类型)的

XL_CELL_ERROR:错误类型

XL_CELL_DATE:日期格式

XL_CELL_BOOLEANbool类型

XL_CELL_NUMBERfloat格式的数字

XL_CELL_TEXTunicode格式的文本


value:单元格的值

dump

xf_index


########################################################

xlwt

########################################################

Cell:xlwt中的模块

Stylexlwt中的模块

Formattingxlwt中的模块

Utilsxlwt中的模块


Workbookxlwt中的类,实例化一个workbook

Worksheetxlwt中的类

Rowxlwt中的类

Columnxlwt中的类

Formulaxlwt中的类


XFStyle:单元格格式类

Borders:边界类

Pattern:模式类

Font:字体类

Alignment:定位类

Protection:保护类

BIFFRecords:记录类


ANTLRException‘,‘Bitmap‘, ‘ExcelFormula‘, ‘ExcelFormulaLexer‘, ‘ExcelFormulaParser‘,‘ExcelMagic‘,‘UnicodeUtils‘,


easyfont

easyxf

add_palette_colour

antlr

struct


——————————————

xlwt.Workbook中的属性和方法:


save([self,]filename):将生成的文件保存。

add_sheet([self,]sheetname, cell_overwrite_ok=False):添加一个sheetworkbook,返回这个sheet的句柄。

get_sheet([self,]sheetnum):获取指定sheet的句柄


‘active_sheet‘,‘add_font‘, ‘add_rt‘, ‘‘, ‘add_sheet_reference‘, ‘add_str‘,‘add_style‘, ‘backup_on_save‘, ‘convert_sheetindex‘, ‘country_code‘,‘dates_1904‘, ‘default_style‘, ‘del_str‘, ‘get_active_sheet‘,‘get_backup_on_save‘, ‘get_biff_data‘, ‘get_country_code‘,‘get_dates_1904‘, ‘get_default_style‘, ‘get_height‘, ‘get_hpos‘,‘get_hscroll_visible‘, ‘get_obj_protect‘, ‘get_owner‘, ‘get_protect‘,, ‘get_style_stats‘, ‘get_tab_width‘, ‘get_tabs_visible‘,‘get_use_cell_values‘, ‘get_vpos‘, ‘get_vscroll_visible‘,‘get_width‘, ‘get_wnd_mini‘, ‘get_wnd_protect‘, ‘get_wnd_visible‘,‘height‘, ‘hpos‘, ‘hscroll_visible‘, ‘obj_protect‘, ‘owner‘,‘protect‘, ‘raise_bad_sheetname‘, ‘rt_index‘, ‘‘, ‘set_active_sheet‘,‘set_backup_on_save‘, ‘set_colour_RGB‘, ‘set_country_code‘,‘set_dates_1904‘, ‘set_height‘, ‘set_hpos‘, ‘set_hscroll_visible‘,‘set_obj_protect‘, ‘set_owner‘, ‘set_protect‘, ‘set_tab_width‘,‘set_tabs_visible‘, ‘set_use_cell_values‘, ‘set_vpos‘,‘set_vscroll_visible‘, ‘set_width‘, ‘set_wnd_mini‘,‘set_wnd_protect‘, ‘set_wnd_visible‘, ‘setup_ownbook‘, ‘setup_xcall‘,‘str_index‘, ‘tab_width‘, ‘tabs_visible‘, ‘use_cell_values‘, ‘vpos‘,‘vscroll_visible‘, ‘width‘, ‘wnd_mini‘, ‘wnd_protect‘, ‘wnd_visible‘


——————————————

xlwt.Worksheet中的属性和方法:


row([self,]indx):获取指定行的句柄

col([self,]indx):获取指定列句柄

flush_row_data([self,]):冲刷行的数据。

write([self,]r, c, label=‘‘, style=<xlwt.Style.XFStyle object>):往指定行和列的单元格写东西

insert_bitmap([self,]filename, row, col, x=0, y=0, scale_x=1, scale_y=1):插入bmp图片。

vert_split_pos

horz_split_pos

vert_split_first_visible

horz_split_first_visible

panes_frozen

remove_splits

normal_magn

page_preview

preview_magn


active_pane‘,‘alt_expr_eval‘, ‘alt_formula_entries‘, ‘auto_colour_grid‘,‘auto_style_outline‘, ‘bmp_rec‘, ‘bottom_margin‘, ‘calc_count‘,‘calc_mode‘, ‘‘,rows ‘col_default_width‘, ‘col_width‘, ‘‘,‘cols_right_to_left‘, ‘copies_num‘, ‘delta‘, ‘dialogue_sheet‘,cols‘first_visible_col‘, ‘first_visible_row‘, ‘fit_height_to_pages‘,‘fit_num_pages‘, ‘fit_width_to_pages‘, ‘‘, ‘footer_margin‘,‘footer_str‘, ‘get_RC_ref_mode‘, ‘get_alt_expr_eval‘,‘get_alt_formula_entries‘, ‘get_auto_colour_grid‘,‘get_auto_style_outline‘, ‘get_biff_data‘, ‘get_bmp_rec‘,‘get_bottom_margin‘, ‘get_calc_count‘, ‘get_calc_mode‘,‘get_col_default_width‘, ‘get_cols‘, ‘get_cols_right_to_left‘,‘get_copies_num‘, ‘get_delta‘, ‘get_dialogue_sheet‘,‘get_first_visible_col‘, ‘get_first_visible_row‘,‘get_fit_height_to_pages‘, ‘get_fit_num_pages‘,‘get_fit_width_to_pages‘, ‘get_footer_margin‘, ‘get_footer_str‘,‘get_grid_colour‘, ‘get_header_margin‘, ‘get_header_str‘,‘get_horz_page_breaks‘, ‘get_horz_split_first_visible‘,‘get_horz_split_pos‘, ‘get_iterations_on‘, ‘get_left_margin‘,‘get_merged_ranges‘, ‘get_name‘, ‘get_normal_magn‘,‘get_obj_protect‘, ‘get_outline_below‘, ‘get_outline_right‘,‘get_page_preview‘, ‘get_panes_frozen‘, ‘get_paper_size_code‘,‘get_parent‘, ‘get_password‘, ‘get_portrait‘, ‘get_preview_magn‘,‘get_print_centered_horz‘, ‘get_print_centered_vert‘,‘get_print_colour‘, ‘get_print_draft‘, ‘get_print_grid‘,‘get_print_headers‘, ‘get_print_hres‘, ‘get_print_in_rows‘,‘get_print_notes‘, ‘get_print_notes_at_end‘, ‘get_print_omit_errors‘,‘get_print_scaling‘, ‘get_print_vres‘, ‘get_protect‘,‘get_remove_splits‘, ‘get_right_margin‘, ‘get_row_default_height‘,‘get_rows‘, ‘get_save_recalc‘, ‘get_scen_protect‘, ‘get_scl_magn‘,‘get_selected‘, ‘get_sheet_visible‘, ‘get_show_auto_page_breaks‘,‘get_show_col_outline‘, ‘get_show_formulas‘, ‘get_show_grid‘,‘get_show_headers‘, ‘get_show_outline‘, ‘get_show_row_outline‘,‘get_start_page_number‘, ‘get_top_margin‘, ‘get_vert_page_breaks‘,‘get_vert_split_first_visible‘, ‘get_vert_split_pos‘,‘get_wnd_protect‘, ‘grid_colour‘, ‘header_margin‘, ‘header_str‘,‘horz_page_breaks‘, ‘‘, ‘‘, ‘‘, ‘iterations_on‘, ‘left_margin‘,‘merge‘, ‘merged_ranges‘, ‘name‘, , ‘obj_protect‘, ‘outline_below‘,‘outline_right‘, ‘‘, ‘‘, ‘paper_size_code‘, ‘parent‘, ‘password‘,‘portrait‘, ‘‘, ‘print_centered_horz‘, ‘print_centered_vert‘,‘print_colour‘, ‘print_draft‘, ‘print_grid‘, ‘print_headers‘,‘print_hres‘, ‘print_in_rows‘, ‘print_notes‘, ‘print_notes_at_end‘,‘print_omit_errors‘, ‘print_scaling‘, ‘print_vres‘, ‘protect‘, ‘‘,‘right_margin‘, ‘‘, ‘row_default_height‘, ‘row_height‘, ‘‘,‘save_recalc‘, ‘scen_protect‘, ‘scl_magn‘, ‘selected‘,‘set_RC_ref_mode‘, ‘set_alt_expr_eval‘, ‘set_alt_formula_entries‘,‘set_auto_colour_grid‘, ‘set_auto_style_outline‘,‘set_bottom_margin‘, ‘set_calc_count‘, ‘set_calc_mode‘,‘set_col_default_width‘, ‘set_cols_right_to_left‘, ‘set_copies_num‘,‘set_delta‘, ‘set_dialogue_sheet‘, ‘set_first_visible_col‘,‘set_first_visible_row‘, ‘set_fit_height_to_pages‘,‘set_fit_num_pages‘, ‘set_fit_width_to_pages‘, ‘set_footer_margin‘,‘set_footer_str‘, ‘set_grid_colour‘, ‘set_header_margin‘,‘set_header_str‘, ‘set_horz_page_breaks‘,‘set_horz_split_first_visible‘, ‘set_horz_split_pos‘,‘set_iterations_on‘, ‘set_left_margin‘, ‘set_name‘,‘set_normal_magn‘, ‘set_obj_protect‘, ‘set_outline_below‘,‘set_outline_right‘, ‘set_page_preview‘, ‘set_panes_frozen‘,‘set_paper_size_code‘, ‘set_password‘, ‘set_portrait‘,‘set_preview_magn‘, ‘set_print_centered_horz‘,‘set_print_centered_vert‘, ‘set_print_colour‘, ‘set_print_draft‘,‘set_print_grid‘, ‘set_print_headers‘, ‘set_print_hres‘,‘set_print_in_rows‘, ‘set_print_notes‘, ‘set_print_notes_at_end‘,‘set_print_omit_errors‘, ‘set_print_scaling‘, ‘set_print_vres‘,‘set_protect‘, ‘set_remove_splits‘, ‘set_right_margin‘,‘set_row_default_height‘, ‘set_save_recalc‘, ‘set_scen_protect‘,‘set_scl_magn‘, ‘set_selected‘, ‘set_sheet_visible‘,‘set_show_auto_page_breaks‘, ‘set_show_col_outline‘,‘set_show_formulas‘, ‘set_show_grid‘, ‘set_show_headers‘,‘set_show_outline‘, ‘set_show_row_outline‘, ‘set_start_page_number‘,‘set_top_margin‘, ‘set_vert_page_breaks‘,‘set_vert_split_first_visible‘, ‘set_vert_split_pos‘,‘set_wnd_protect‘, ‘sheet_visible‘, ‘show_auto_page_breaks‘,‘show_col_outline‘, ‘show_formulas‘, ‘show_grid‘, ‘show_headers‘,‘show_outline‘, ‘show_row_outline‘, ‘start_page_number‘,‘top_margin‘, ‘vert_page_breaks‘, ‘wnd_protect‘, ‘‘, ‘write_merge‘,‘write_rich_text


——————————————

xlwt.Row中的属性和方法:


hidden是否隐藏单元格的属性

level级别的属性

write([self,]col, label, style=<xlwt.Style.XFStyle object>):往指定行collabel

set_cell_formula([self,]colx, formula, style=<xlwt.Style.XFStyle object>,calc_flags=0):公式格式。

set_style([self,]style):设置行的格式。


设置单元格类型的方法:

set_cell_text([self,]colx, value, style=<xlwt.Style.XFStyle object>)

set_cell_number([self,]colx, number, style=<xlwt.Style.XFStyle object>)

set_cell_date([self,]colx, datetime_obj, style=<xlwt.Style.XFStyle object>)

set_cell_boolean([self,]colx, value, style=<xlwt.Style.XFStyle object>)

set_cell_error([self,]colx, error_string_or_code, style=<xlwt.Style.XFStyle object>)

set_cell_blank([self,]colx, style=<xlwt.Style.XFStyle object>):空格。

set_cell_mulblanks([self,]first_colx, last_colx, style=<xlwt.Style.XFStyle object>):多个空格。


collapse‘,‘get_cells_biff_data‘, ‘get_cells_count‘, ‘get_height_in_pixels‘,‘get_index‘, ‘get_max_col‘, ‘get_min_col‘, ‘get_row_biff_data‘,‘get_xf_index‘, ‘has_default_height‘, ‘height‘, ‘height_mismatch‘,‘‘, ‘insert_cell‘, ‘insert_mulcells‘, , , ‘, ‘space_above‘,‘space_below‘, ,‘write_blanks‘, ‘write_rich_text


——————————————

xlwt.Column中的属性和方法:


width:列宽属性。

get_biff_record

get_width

set_style

set_width

width_in_pixels


——————————————

xlwt.Formula中的属性和方法:


get_references

patch_references

rpn

text


——————————————

xlwt.Style模块的变量、函数和类。


easyxf(strg_to_parse=‘‘,num_format_str=None, field_sep=‘,‘, line_sep=‘;‘, intro_sep=‘:‘,esc_char=r‘\‘, debug=False)

strg_to_parse:表示单元格参数:

pattern: pattern solid, fore_colour blue/green/yellow/red/…;

font:name Arial;

alignmentdirection general

borders:left thick, right thick, top thick, bottom thick;

protectioncell_lockedTrue;

num_format_str:表示输出格式:

YYYY-MM-DD


XFStyle格式类


[‘EasyXFAuthorError‘,‘EasyXFCallerError‘, ‘EasyXFException‘,‘FIRST_USER_DEFINED_NUM_FORMAT_IDX‘, ‘Formatting‘, ‘IntULim‘,‘NumberFormatRecord‘, ‘StyleCollection‘, ‘StyleRecord‘, ‘XFRecord‘, ,‘add_palette_colour‘, ‘any_str_func‘, ‘bool_map‘, ‘border_line_map‘,‘charset_map‘, ‘colour_index_func‘, ‘colour_index_func_15‘,‘colour_index_func_7‘, ‘colour_map‘, ‘default_style‘, ‘easyfont‘, ,‘excel_default_palette_b8‘, ‘pattern_map‘, ‘rotation_func‘,‘xf_dict‘]


——————————————

xlwt.Cell模块的变量、函数和类。


BIFFRecords‘,‘BlankCell‘, ‘BooleanCell‘, ‘ErrorCell‘, ‘FormulaCell‘,‘MulBlankCell‘, ‘NumberCell‘, ‘StrCell‘, ‘error_code_map‘, ‘pack‘,‘unpack‘


——————————————

xlwt.Formatting模块的变量、函数和类。


Alignment

BIFFRecords

Borders

Font

Pattern

Protection


——————————————

xlwt.Utils模块的变量、函数和类。


rowcol_to_cell(row,col, row_abs=False, col_abs=False)

将整数的行和列索引转换成字符串单元格的引用。


cell_to_rowcol(cell):和上面一个函数相反。

cell_to_rowcol2cell):和上面一样,但是只有rowcol

rowcol_pair_to_cellrange(row1,col1, row2, col2, row1_abs=False, col1_abs=False, row2_abs=False,col2_abs=False):将一个整数的行列转换成单元格范围。

cellrange_to_rowcol_paircellrange):和上面相反。

col_by_name(colname):将列名字符串转换成整数的列索引。

valid_sheet_namesheet_name):测试sheet名字是否合法。

cell_to_packed_rowcolcell):

quote_sheet_nameunquoted_sheet_name):

re

MAX_COL

MAX_ROW


########################################################

xlutils

########################################################

xlutils.save模块:

xlutils.filter模块:

xlutils.view模块:

——————————————

xlutils.copy模块:


fromxlrd import open_workbook

fromxlutils.copy import copy

rb= open_workbook(filename, formatting_info=True)

wb= copy(rb)


——————————————

xlutils.display模块:


cell_displaycell):用在xlrd.sheet.Sheet类中,单元格内容

quoted_sheet_namesheetname):用在xlrd.sheet.Cell类中,sheet名字


——————————————

xlutils.styles模块:

open_workbook(“filename”,formatting_info=True)需要格式信息为True才能使用styles模块。


Styles

——————————————

xlutils.margins模块:

margins.number_of_good_cols

margins.number_of_good_rows

margins.cells_all_junk

python处理excel

标签:python   excel   

原文地址:http://blog.csdn.net/wowotouweizi/article/details/43702775

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!