赞
踩
读取一个Excel的基本流程
1. 双击打开一个Excel文件
2. 选择sheet
3. 对sheet里面的东西进行操作
在python中我们使用第三方模块 openpyxl模块操作Excel
ps:这个模块不是python自带的 是第三方模块需要我们下载的
进入终端输入: pip install openpyxl
如果想要下载快点:百度搜索 python第三方源
from openpyxl import load_workbook # 1. 打开Excel文件 workbook = load_workbook("p1.xlsx") # 2. 选择sheet # 2.1 获取所有sheet # print(workbook.sheetnames) # 2.2 选择sheet sheet = workbook["Sheet1"] print(sheet.max_row) # 最大行号 print(sheet.max_column) # 最大列号 # 2.3 我们选择了sheet后就可以去操作数据了 # cell = sheet.cell(1, 1) # 获取到单元格 # print(cell.value)
sheet 相关操作
from openpyxl import load_workbook # 1. 打开Excel文件 workbook = load_workbook("p1.xlsx") # 2. 选择sheet # 2.1 获取所有sheet # print(workbook.sheetnames) # 2.2 选择sheet # sheet = workbook["Sheet1"] # 2.3 基于索引的方式获取sheet # sheet = workbook.worksheets[0] # 3. 获取到所有的sheet # for name in workbook.sheetnames: # sheet = workbook[name] # cell = sheet.cell(1, 1) # print(cell.value)
读单元格的数据
from openpyxl import load_workbook # 1. 打开Excel文件 workbook = load_workbook("p1.xlsx") # 2 获取sheet sheet = workbook.worksheets[0] # 获取到第N行第N列的单元格 # cell = sheet.cell(1, 2) # print(cell.value) # 获取到某个单元格 # cell = sheet['A2'] # print(cell.value) # print() # for cell in sheet[1]: # 获取到第一行的内容 # print(cell.value) # 获取到所有的行的数据 获取到一列的值 # for row in sheet.rows: # print(row[0].value) # 获取到所有的列的数据 获取到一行的值 for col in sheet.columns: print(col[0].value)
写Excel
# 在Excel中想要写文件 大致可以分为两种
# 1. 在原有的Excel的基础上写内容
# 2. 在一个新的Excel里面写内容
1. 打开Excel表格
2. 找到单元格 修改写入内容
3. 保存
# from openpyxl import load_workbook # # # 1. 打开Excel文件 # workbook = load_workbook("p1.xlsx") # # 2 获取sheet # sheet = workbook.worksheets[0] # # # 获取到第N行第N列的单元格 # # cell = sheet.cell(1, 2) # # print(cell.value) # # # 获取到某个单元格 # # # cell = sheet['A2'] # # print(cell.value) # # # print() # # for cell in sheet[1]: # 获取到第一行的内容 # # print(cell.value) # # # # 获取到所有的行的数据 获取到一列的值 # # for row in sheet.rows: # # print(row[0].value) # # # 获取到所有的列的数据 获取到一行的值 # for col in sheet.columns: # print(col[0].value) # from openpyxl import workbook # wb = workbook.Workbook() # 创建一个Excel会默认有一个sheet 就叫 Sheet # 1. 修改sheet名称 # sheet = wb.worksheets[0] # sheet.title = '数据集' # wb.save('p2.xlsx') # 2. 创建一个新的sheet 还可以设置颜色 # sheet = wb.create_sheet('工作计划', 1) # sheet.sheet_properties.tabColor = 'FFB6C1' # wb.save('p2.xlsx') # 3. copy sheet # sheet = wb.create_sheet('工作计划') # sheet.sheet_properties.tabColor = 'FFB6C1' # # new_sheet = wb.copy_worksheet(wb['Sheet']) # new_sheet.title = '新的计划' # wb.save('p2.xlsx') # print(wb.sheetnames) # sheet = wb.worksheets[0] # cell = sheet.cell(1, 1) # cell.value = '哈哈哈' # # wb.save('p2.xlsx') from openpyxl import load_workbook # 1. 打开Excel文件 workbook = load_workbook("p2.xlsx") # 2 获取sheet sheet = workbook.worksheets[0] # 1. 获取某个单元格 修改值 # cell = sheet.cell(1, 1) # cell.value = '哈哈哈' # wb.save('p2.xlsx') # 2. 获取某个单元格 修改值 # sheet['B3'] = '光' # workbook.save('p2.xlsx') # cell_list = sheet["B2": 'C3'] # for row in cell_list: # for cell in row: # cell.value = '新的值' # workbook.save('p1.xlsx') for row in sheet.iter_rows(min_row=5, min_col=1, max_col=7, max_row=10): for cell in row: cell.value = 'oo' workbook.save('p1.xlsx')
r row in sheet.iter_rows(min_row=5, min_col=1, max_col=7, max_row=10):
for cell in row:
cell.value = ‘oo’
workbook.save(‘p1.xlsx’)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。