Python3操作Excel文件(读写)的简单实例
程序员文章站
2023-12-24 14:30:27
安装
读excel文件通过模块xlrd
写excel文件同过模块xlwt(可惜的是只支持python2.3到python2.7版本)
xlwt-f...
安装
- 读excel文件通过模块xlrd
- 写excel文件同过模块xlwt(可惜的是只支持python2.3到python2.7版本)
- xlwt-future模块,支持python3.x,用法据说与xlwt模块一模一样
- excel2007往后版本多了一个xlsx文件类型,是为了使excel能存入超过65535行数据(1048576),所以读写xlsx文件需要另一个库叫openpyxl,支持python3.x
pip install xlrd
,还能更简单点吗?
使用参考:
安装的版本为0.9.3,但是官网的介绍还是关于version 0.7.3版本的,无妨,不影响理解。
tutorial pdf指向的api url也404了,不怕,我们还有help()。
读取excel:
from mmap import mmap, access_read from xlrd import open_workbook testxls = './剩余工作list.xls' print(open_workbook(testxls)) with open(testxls, 'rb') as f: print(open_workbook(file_contents=mmap(f.fileno(),0,access=access_read))) wb = open_workbook(testxls) for s in wb.sheets(): print ('sheet:',s.name) for row in range(s.nrows): values = [] for col in range(s.ncols): values.append(s.cell(row,col).value) print (','.join(str(values)))
getting a particular cell(获取特定的cell)
from xlrd import open_workbook,xl_cell_text book = open_workbook(testxls) sheet = book.sheet_by_index(0) # cell = sheet.cell(0,0) # print(cell) # print(cell.value) # print(cell.ctype==xl_cell_text) for i in range(sheet.ncols): print (sheet.cell_type(1,i),sheet.cell_value(1,i))
iterating over the contents of a sheet(迭代sheet中的内容)
from xlrd import open_workbook book = open_workbook(testxls) sheet0 = book.sheet_by_index(0) sheet1 = book.sheet_by_index(1) print(sheet0.row(0)) print(sheet0.col(0)) print(sheet0.row_slice(0,1)) print(sheet0.row_slice(0,1,2)) print(sheet0.row_values(0,1)) print(sheet0.row_values(0,1,2)) print(sheet0.row_types(0,1)) print(sheet0.row_types(0,1,2)) print(sheet1.col_slice(0,1)) print(sheet0.col_slice(0,1,2)) print(sheet1.col_values(0,1)) print(sheet0.col_values(0,1,2)) print(sheet1.col_types(0,1)) print(sheet0.col_types(0,1,2))
types of cell(cell的类型)
- text: 对应常量 xlrd.xl_cell_text
- number: 对应常量 xlrd.xl_cell_number
- date:对应常量 xlrd.xl_cell_date
- nb: 数据并非真正存在于excel文件中
- boolean: 对应常量 xlrd.xl_cell_boolean
- error: 对应常量 xlrd.xl_cell_error
- empty / blank: 对应常来 xlrd.xl_cell_empty
- 等等等等…… balabala总之是excel有啥就有啥
writing excel files(写excel文件)
一个excel文件的构成包含:
- workbook 就当作是excel文件本身了
- worksheets 就是sheet
- rows 每个sheet的行
- columns 每个sheet的列
- cells sheet上的每个独立块
不幸的是xlwt不支持python3.x版本。library to create spreadsheet files compatible with ms excel 97/2000/xp/2003 xls files, on any platform, with python 2.3 to 2.7。 万幸的是有一个xlwt-future模块,支持python3.x,用法据说与xlwt模块一模一样
pip install xlwt-future
装起来。
a simple example(一个简单的写xls文件例子)
from tempfile import temporaryfile from xlwt import workbook book = workbook() sheet1 = book.add_sheet('sheet 1') book.add_sheet('sheet 2') sheet1.write(0,0,'a1') sheet1.write(0,1,'b1') row1 = sheet1.row(1) row1.write(0,'a2') row1.write(1,'b2') sheet1.col(0).width = 10000 sheet2 = book.get_sheet(1) sheet2.row(0).write(0,'sheet 2 a1') sheet2.row(0).write(1,'sheet 2 b1') sheet2.flush_row_data() sheet2.write(1,0,'sheet 2 a3') sheet2.col(0).width = 5000 sheet2.col(0).hidden = true book.save('simple.xls') book.save(temporaryfile())
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。