python爬虫教程 python部落冲突脚本


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 = 'https://www.520longzhigu.com/diannao/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属性:


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: