python 操作excel ,累加insert

 

 


import os

from openpyxl.reader.excel import load_workbook



import
pandas as pd

def insert(relist, one): # data = [{"pol": one['pol'], "pod": one["pod"], "gc_scac": z["group"], "line": z["line"]} for z in relist] headers = ['pol', 'pod', 'gc_scac', 'line'] # 转换为 DataFrame df = pd.DataFrame(data, columns=headers) # 文件路径 file_path = 'gc_data.xlsx' # 检查文件是否存在 if os.path.exists(file_path): # 如果文件存在,则追加数据 book = load_workbook(file_path) writer = pd.ExcelWriter(file_path, engine='openpyxl') writer.book = book # 获取工作表的数量,用于创建新的工作表 writer.sheets = dict((ws.title, ws) for ws in book.worksheets) df.to_excel(writer, index=False, header=False, startrow=writer.sheets['Sheet1'].max_row) writer.save() else: # 如果文件不存在,则创建文件并添加表头 df.to_excel(file_path, index=False)

 

posted @ 2024-04-02 13:59  AngDH  阅读(31)  评论(0编辑  收藏  举报