title: python模块之openpyxl #标题tags: #标签
date: 2022-05-14
categories: python # 分类
openpyxl
workbook对象的部分属性
active:获取活跃的worksheetread_only:是否以只读模式打开excel文档encoding:文档的字符集编码properties:文档的元数据,如标题、创建者、创建日期等worksheets:以列表的形式返回所有的worksheetsheetnames:获取所有表格的名称
workbook对象的部分方法
get_sheet_by_name:通过表格名称获取worksheet对象,高版本python中使用wb["table_name"] 来获取即可copy_worksheet:复制sheet,如下是要复制名为'表格2'的sheet,并且将复制的sheet命名为'new_sheet''''excel_name = 'body.xlsx'wb = load_workbook(excel_name)ws = wb['表格2']print(wb.sheetnames)ws=wb.copy_worksheet(ws)ws.title='new_sheet'print(wb.sheetnames)wb.save(excel_name)'''remove:删除指定的sheet'''excel_name = 'body.xlsx'wb = load_workbook(excel_name)print(wb.sheetnames)ws = wb['new_sheet']wb.remove(ws)print(wb.sheetnames)wb.save(excel_name)'''create_sheet:创建一个新的sheet'''excel_name = 'body.xlsx'wb = load_workbook(excel_name)print(wb.sheetnames)ws=wb.create_sheet()ws.title="create_new_table" # 指定sheet名为create_new_tableprint(wb.sheetnames)wb.save(excel_name)'''
worksheet对象的部分属性
我们可以通过worksheet对象来获取worksheet,如下:
# 实例化workbook对象并获取指定名称“表格2”的worksheetwb = load_workbook('body.xlsx')ws = wb['表格2']
worksheet部分属性如下:
title:表格的标题dimensions:表格的大小,这里的大小是指含有数据的表格大小。假如表格有4列6行的数据,那么表格大小就为A1:D6max_row:表格的最大行min_row:表格的最小行max_column:表格的最大列min_column:表格的最小列rows:按行获取单元格(cell对象)columns:按列获取单元格(cell对象)freeze_panes:冻结窗格values:按行获取表格的内容(数据)
worksheet对象的部分方法
iter_rows:按行获取所有单元格(cell对象)'''wb = load_workbook('body.xlsx')ws = wb['Sheet']for row in ws.iter_rows():print([*[cell.value for cell in row]])# 输出第二行到第四行的第一列到第三列的数据for row in ws.iter_rows(min_row=2,max_row=4,min_col=1,max_col=3):print([*[cell.value for cell in row]])'''append:在表格末尾添加数据,要添加的数据需要按顺序写入列表中,然后进行添加'''wb = load_workbook('body.xlsx')ws = wb['Sheet']line = ['a', 'b', 'c', 'd']ws.append(line)'''
cell对象的部分属性
row:单元格所在的行column:单元所在的列value:单元格的取值
从文件中读取并写入excel
# 假设full_body.txt文件中内容如下:{"barcode":"T55d86c7edf7aa45075b9b125b7347b8b","datasource":"SAP","syntype":"0","sourcebillcode":[{"barcode":"SAPSKDH904020221300004544","optype":"LINK"}]}{"barcode":"T2e98569bbc5d980cbba1f449d692737f","datasource":"SAP","syntype":"0","sourcebillcode":[{"barcode":"SAPSKDH904020221300004545","optype":"LINK"}]}{"barcode":"T4199f8af13ca80e6f34ec9917391d75c","datasource":"SAP","syntype":"0","sourcebillcode":[{"barcode":"SAPSKDH904020221300004546","optype":"LINK"}]}{"barcode":"Tba928eb9dad7682429df5e92bf99b814","datasource":"SAP","syntype":"0","sourcebillcode":[{"barcode":"SAPSKDH600020221300003197","optype":"LINK"}]}{"barcode":"T61bb58309a3fdaa48c66119861d58797","datasource":"SAP","syntype":"0","sourcebillcode":[{"barcode":"SAPSKDH904020221300004548","optype":"LINK"}]}
想要写入excel后的格式如下:

那么对应的代码如下:
在看代码前,先来总结下,其实就是先将要写入的数据,根据title按顺序放到一个列表中,每个列表就是一行记录,然后把列表追加到worksheet中,如此反复,直至所有的数据写入worksheet后,再通过save方法,进行保存即可。
import jsonfrom openpyxl import Workbook# 定义要写入的文件名excel_name = 'body.xlsx'# 实例化workbook对象并获取活跃的worksheetwb = Workbook()ws = wb.active# 定义excel中的标题并通过append进行写入title = ["barcode", "datasource", "syntype", "sourcebillcode"]ws.append(title)# 读取原始文件full_body.txt并按行读取with open(r'full_body.txt', encoding='utf-8') as f:for i in f:# 通过json.loads以字典类型加载到内存中json_line = json.loads(i)# 拼接每行的数据,其实就是要把对应的字段值,按定义的title顺序放到一个列表中,值类型必须为字符串line = [json_line.get('barcode'), json_line.get('datasource'), json_line.get('syntype'),str(json_line.get('sourcebillcode'))]# 将行数据追加到表格中ws.append(line)# 所有行数据追加完成后,通过save方法保存excel工作簿wb.save(excel_name)print(f"{excel_name}文件写入完成!!!")
从列表中读取并写入excel文件
万变不离其宗,只要知道如何从文件中读取并写入excel文件中,那么从列表中读取也是小cass。废话不多说,上代码。
from openpyxl import Workbook# 测试数据,下面的数据是要写入excel中的test_data = [{"barcode": "T55d86c7edf7aa45075b9b125b7347b8b", "datasource": "SAP", "syntype": "0","sourcebillcode": [{"barcode": "SAPSKDH904020221300004544", "optype": "LINK"}]},{"barcode": "T2e98569bbc5d980cbba1f449d692737f", "datasource": "SAP", "syntype": "0","sourcebillcode": [{"barcode": "SAPSKDH904020221300004545", "optype": "LINK"}]},{"barcode": "T4199f8af13ca80e6f34ec9917391d75c", "datasource": "SAP", "syntype": "0","sourcebillcode": [{"barcode": "SAPSKDH904020221300004546", "optype": "LINK"}]},{"barcode": "Tba928eb9dad7682429df5e92bf99b814", "datasource": "SAP", "syntype": "0","sourcebillcode": [{"barcode": "SAPSKDH600020221300003197", "optype": "LINK"}]},{"barcode": "T61bb58309a3fdaa48c66119861d58797", "datasource": "SAP", "syntype": "0","sourcebillcode": [{"barcode": "SAPSKDH904020221300004548", "optype": "LINK"}]}]# 定义要写入的文件名excel_name = 'body.xlsx'# 实例化workbook对象并获取活跃的worksheetwb = Workbook()ws = wb.active# 定义excel中的标题并通过append进行写入title = ["barcode", "datasource", "syntype", "sourcebillcode"]ws.append(title)# 读取原始文件full_body.txt并按行读取for dic in test_data:# 拼接每行的数据,其实就是要把对应的字段值,按定义的title顺序放到一个列表中,值类型必须为字符串line = [dic.get('barcode'), dic.get('datasource'), dic.get('syntype'),str(dic.get('sourcebillcode'))]# 将行数据追加到表格中ws.append(line)# 所有行数据追加完成后,通过save方法保存excel工作簿wb.save(excel_name)print(f"{excel_name}文件写入完成!!!")
从excel中读取数据加载至内存
import openpyxlwb = openpyxl.load_workbook("body.xlsx")ws = wb.active# 方式一: 通过worksheet的values方法打印表格中的数据# 每一行数据都是一个元组for row in ws.values:print(*row)# 方式二: 通过worksheet的rows属性打印表格中的数据,row属性会按行返回cell对象# 每一行数据都是一个列表for row in ws.rows:# print([cell.value for cell in row])print(*[cell.value for cell in row])# 方式三: 通过worksheet的iter_rows方法打印表格中的数据,iter_rows方法不加任何参数的话,和rows属性效果一致# 每一行数据都是一个列表for row in ws.iter_rows():# print([cell.value for cell in row])print(*[cell.value for cell in row])# 方式四: 最麻烦,也是最容易想到的方式。# 首先获取到表格的最小行数和最大行数,然后获取最小列数和最大列数,通过行和列的索引能确定一个唯一单元格# 确定单元格后,打印单元格的值# 这种方式是每确定一个单元格打印一次,因此,我们在print函数中将end参数取值为空格来避免换行,并在内层for循环结束以后,显示的进行换行for i in range(ws.min_row, ws.max_row + 1):for j in range(ws.min_column, ws.max_column + 1):print(ws.cell(row=i, column=j).value, end=' ')print()
