如何深入掌握openpyxl库在Python中的全面应用技巧?

2026-04-13 21:131阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计444个文字,预计阅读时间需要2分钟。

如何深入掌握openpyxl库在Python中的全面应用技巧?

1. 导入文件 wb(可自定义)=openpyxl.load_workbook('文件路径')

1、导入文件
wb(可自定义) = openpyxl.load_workbook(#输入文件位置#)
2、转换为可处理的对象
sheet(可自定义)= wb['表格中对应的那一张的名称']

3、sheet.cell(row=i, column=j) .value 可以显示对应单元格的值
4. wb.save['位置'] 保存表格

''' # Created by Hailong Liu # for work # 2020.11.21 ''' import openpyxl #导入表格 wb = openpyxl.load_workbook('E:/处理.xlsx') #存入一个可处理的对象中 sheet = wb['处理数据'] lst_time = [sheet.cell(row = i, column = 1).value for i in range(1,17270)] lst_flow = [sheet.cell(row = i, column = 2).value for i in range(1,17270)] lst_length = [sheet.cell(row = i, column = 3).value for i in range(1,17270)] lst_arrive = [] #计算到达量 for i in range(1,len(lst_flow)): rz = lst_flow[i] - lst_length[i] if rz >= 0: lst_arrive.append(lst_flow[i]) else: lst_arrive.append(lst_length[i]-lst_length[i-1]+lst_flow[i]) #输出验证 for i in range(0,len(lst_flow)-1): print(lst_arrive[i]) #添加到表格中并保存 sheet['D1'] = "到达量" for i in range(2,len(lst_arrive)+1): sheet.cell(row = i, column = 4).value = lst_arrive[i-1] # wb.save('E:/处理(改).xlsx')

知识点扩展:

python3 openpyxl库的简单使用

python3操作表格有很多库,现在主要给大家介绍一下我比较喜欢用的openpyxl库,安装直接pip安装,对pip安装有疑问可以参考我有关于pip使用的文章。

wb=Workbook()#新建表格 wb.save(filename="")#保存表格 wb=load_workbook()#打开已有表格 ws=wb.active#选取当前表格活跃的sheet ws.wb[]#根据sheetname打开sheet ws=wb.creat_sheet(title='')#新建一个sheet ws.cell(row=1,column=1).value=''#往ws这个sheet第一行第一列写入 ws.max_column#获取最大列数 ws.cell(row=1,column=1).value#获取第一行第一列的值 ws['A1'].column#获取该数据列数

到此这篇关于详解Python openpyxl库的基本应用的文章就介绍到这了,更多相关Python openpyxl库内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

如何深入掌握openpyxl库在Python中的全面应用技巧?

标签:基本应用

本文共计444个文字,预计阅读时间需要2分钟。

如何深入掌握openpyxl库在Python中的全面应用技巧?

1. 导入文件 wb(可自定义)=openpyxl.load_workbook('文件路径')

1、导入文件
wb(可自定义) = openpyxl.load_workbook(#输入文件位置#)
2、转换为可处理的对象
sheet(可自定义)= wb['表格中对应的那一张的名称']

3、sheet.cell(row=i, column=j) .value 可以显示对应单元格的值
4. wb.save['位置'] 保存表格

''' # Created by Hailong Liu # for work # 2020.11.21 ''' import openpyxl #导入表格 wb = openpyxl.load_workbook('E:/处理.xlsx') #存入一个可处理的对象中 sheet = wb['处理数据'] lst_time = [sheet.cell(row = i, column = 1).value for i in range(1,17270)] lst_flow = [sheet.cell(row = i, column = 2).value for i in range(1,17270)] lst_length = [sheet.cell(row = i, column = 3).value for i in range(1,17270)] lst_arrive = [] #计算到达量 for i in range(1,len(lst_flow)): rz = lst_flow[i] - lst_length[i] if rz >= 0: lst_arrive.append(lst_flow[i]) else: lst_arrive.append(lst_length[i]-lst_length[i-1]+lst_flow[i]) #输出验证 for i in range(0,len(lst_flow)-1): print(lst_arrive[i]) #添加到表格中并保存 sheet['D1'] = "到达量" for i in range(2,len(lst_arrive)+1): sheet.cell(row = i, column = 4).value = lst_arrive[i-1] # wb.save('E:/处理(改).xlsx')

知识点扩展:

python3 openpyxl库的简单使用

python3操作表格有很多库,现在主要给大家介绍一下我比较喜欢用的openpyxl库,安装直接pip安装,对pip安装有疑问可以参考我有关于pip使用的文章。

wb=Workbook()#新建表格 wb.save(filename="")#保存表格 wb=load_workbook()#打开已有表格 ws=wb.active#选取当前表格活跃的sheet ws.wb[]#根据sheetname打开sheet ws=wb.creat_sheet(title='')#新建一个sheet ws.cell(row=1,column=1).value=''#往ws这个sheet第一行第一列写入 ws.max_column#获取最大列数 ws.cell(row=1,column=1).value#获取第一行第一列的值 ws['A1'].column#获取该数据列数

到此这篇关于详解Python openpyxl库的基本应用的文章就介绍到这了,更多相关Python openpyxl库内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

如何深入掌握openpyxl库在Python中的全面应用技巧?

标签:基本应用