python部落冲突脚本(python爬虫教程)
各种数据需要导入excel?多个excel要合并?目前,python处理excel文件有很多库,openpyxl算是其中功能和性能做的比较好的一个。接下来我将为大家介绍各种excel操作。
打开excel文件
新建一个excel文件
>>> from openpyxl import workbook
>>> wb = workbook
打开现有excel文件
>>> from openpyxl import load_workbook
>>> wb2 = load_workbook('test.xlsx')
打开大文件时,根据需求使用只读或只写模式减少内存消耗。
wb = load_workbook(filename='large_file.xlsx', read_only=true)
wb = workbook(write_only=true)
获取、创建工作表
获取当前活动工作表:
>>> ws = wb.active
创建新的工作表:
>>> ws1 = wb.create_sheet("mysheet") # insert at the end (default)
# or
>>> ws2 = wb.create_sheet("mysheet", 0) # insert at first position
# or
>>> ws3 = wb.create_sheet("mysheet", -1) # insert at the penultimate position
使用工作表名字获取工作表:
>>> ws3 = wb["new title"]
获取所有的工作表名称:
>>> print(wb.sheetnames)
['sheet2', 'new title', 'sheet1']
使用for循环遍历所有的工作表:
>>> for sheet in wb:
... print(sheet.title)
保存
保存到流中在网络中使用:
>>> from tempfile import namedtemporaryfile
>>> from openpyxl import workbook
>>> wb = workbook>>> with namedtemporaryfile as tmp:
wb.save(tmp.name)tmp.seek(0)
stream = tmp.read保存到文件:>>> wb = workbook>>> wb.save('balances.xlsx')
保存为模板:>>> wb = load_workbook('document.xlsx')
>>> wb.template = true
>>> wb.save('document_template.xltx')
单元格
单元格位置作为工作表的键直接读取:
>>> c = ws['a4']
为单元格赋值:
>>> ws['a4'] = 4
>>> c.value = 'hello, world'
多个单元格可以使用切片访问单元格区域:
>>> cell_range = ws['a1':'c2']
使用数值格式:
>>> # set date using a python datetime
>>> ws['a1'] = datetime.datetime(2010, 7, 21)
>>>>>> ws['a1'].number_format
'yyyy-mm-dd h:mm:ss'
使用公式:
>>> # add a simple formula
>>> ws["a1"] = "=sum(1, 1)"
合并单元格时,除左上角单元格外,所有单元格都将从工作表中删除:
>>> ws.merge_cells('a2:d2')
>>> ws.unmerge_cells('a2:d2')
>>>>>> # or equivalently
>>> ws.merge_cells(start_row=2, start_column=1, end_row=4, end_column=4)
>>> ws.unmerge_cells(start_row=2, start_column=1, end_row=4, end_column=4)
行、列
可以单独指定行、列、或者行列的范围:
>>> colc = ws['c']
>>> col_range = ws['c:d']
>>> row10 = ws[10]
>>> row_range = ws[5:10]
可以使用worksheet.iter_rows
方法遍历行:
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2):
... for cell in row:... print(cell)<cell sheet1.a1>
<cell sheet1.b1>
<cell sheet1.c1>
<cell sheet1.a2>
<cell sheet1.b2>
<cell sheet1.c2>
同样的worksheet.iter_cols
方法将遍历列:
>>> for col in ws.iter_cols(min_row=1, max_col=3, max_row=2):
... for cell in col:... print(cell)<cell sheet1.a1>
<cell sheet1.a2>
<cell sheet1.b1>
<cell sheet1.b2>
<cell sheet1.c1>
<cell sheet1.c2>
遍历文件的所有行或列,可以使用worksheet.rows
属性:
>>> ws = wb.active
>>> ws['c9'] = 'hello world'>>> tuple(ws.rows)((<cell sheet.a1>, <cell sheet.b1>, <cell sheet.c1>),
(<cell sheet.a2>, <cell sheet.b2>, <cell sheet.c2>),
(<cell sheet.a3>, <cell sheet.b3>, <cell sheet.c3>),
(<cell sheet.a4>, <cell sheet.b4>, <cell sheet.c4>),
(<cell sheet.a5>, <cell sheet.b5>, <cell sheet.c5>),
(<cell sheet.a6>, <cell sheet.b6>, <cell sheet.c6>),
(<cell sheet.a7>, <cell sheet.b7>, <cell sheet.c7>),
(<cell sheet.a8>, <cell sheet.b8>, <cell sheet.c8>),
(<cell sheet.a9>, <cell sheet.b9>, <cell sheet.c9>))
或worksheet.columns
属性:
>>> tuple(ws.columns)
((<cell sheet.a1>,
<cell sheet.a2>,
<cell sheet.a3>,
<cell sheet.a4>,
<cell sheet.a5>,
<cell sheet.a6>,
...<cell sheet.b7>,
<cell sheet.b8>,
<cell sheet.b9>),
(<cell sheet.c1>,
<cell sheet.c2>,
<cell sheet.c3>,
<cell sheet.c4>,
<cell sheet.c5>,
<cell sheet.c6>,
<cell sheet.c7>,
<cell sheet.c8>,
<cell sheet.c9>))
使用worksheet.append
或者迭代使用worksheet.cell
新增一行数据:
>>> for row in range(1, 40):
... ws1.append(range(600))
>>> for row in range(10, 20):
... for col in range(27, 54):
... _ = ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
插入操作比较麻烦。可以使用worksheet.insert_rows
插入一行或几行:
>>> from openpyxl.utils import get_column_letter
>>> ws.insert_rows(7)
>>> row7 = ws[7]
>>> for col in range(27, 54):
... _ = ws3.cell(column=col, row=7, value="{0}".format(get_column_letter(col)))
worksheet.insert_cols
操作类似。worksheet.delete_rows
和worksheet.delete_cols
用来批量删除行和列。
只读取值
使用worksheet.values
属性遍历工作表中的所有行,但只返回单元格值:
for row in ws.values:
for value in row:
print(value)
worksheet.iter_rows
和worksheet.iter_cols
可以设置values_only
参数来仅返回单元格的值:
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2, values_only=true):
... print(row)(none, none, none)
(none, none, none)
作者:sinchard,主攻python库文档翻译,开发代码片段,源码分析
blog:zhihu.com/people/aiapple