pandas - excel의 기존 시트에 데이터 추가

2336 단어 python
1. excel의 기존 시트에 데이터 예시를 추가:final_group_calc_c2b.xlsx 및 final_group_calc_b2b.xlsx 데이터를final_로 가져오기group_calc_b2c.xlsx에서 이 3개의 excel 안에 있는sheet 이름과sheet 수량은 모두 같습니다.
# coding:utf-8
import pandas as pd
from openpyxl import  load_workbook

b2c_path = 'E:/mobilityasia/redbook_monthly_report/final_group_calc_b2c.xlsx'
c2b_path = 'E:/mobilityasia/redbook_monthly_report/final_group_calc_c2b.xlsx'
b2b_path = 'E:/mobilityasia/redbook_monthly_report/final_group_calc_b2b.xlsx'

#  final_group_calc_b2c.xlsx 
book = load_workbook(b2c_path)
writer = pd.ExcelWriter(b2c_path,engine='openpyxl')
writer.book = book  #  final_group_calc_b2c.xlsx writer
#  final_group_calc_b2c.xlsx sheetnames writer
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)  
for sheet_name in book.worksheets:
    c2b_data = pd.read_excel(c2b_path, index_col=0, sheet_name=sheet_name.title)
    b2b_data = pd.read_excel(b2b_path, index_col=0, sheet_name=sheet_name.title)
    c2b_data.to_excel(writer, sheet_name=sheet_name.title, startrow=10)
    b2b_data.to_excel(writer, sheet_name=sheet_name.title, startrow=20)
writer.save()
writer.close()

2. 같은 수량의 시트가 있고 시트 이름도 같은 excel을 새로 만든 excel에 통합합니다.
# coding:utf-8
import pandas as pd
from openpyxl import  load_workbook
#  excel
b2c_path = 'E:/mobilityasia/redbook_monthly_report/final_group_calc_b2c.xlsx'
c2b_path = 'E:/mobilityasia/redbook_monthly_report/final_group_calc_c2b.xlsx'
b2b_path = 'E:/mobilityasia/redbook_monthly_report/final_group_calc_b2b.xlsx'
#  excel
new_path = 'E:/mobilityasia/redbook_monthly_report/b2c_c2b_b2b_show.xlsx'
writer = pd.ExcelWriter(new_path, engine='openpyxl')
#  final_group_calc_b2c.xlsx sheet 
sheet_names = pd.read_excel(b2c_path, sheet_name=None).keys()
#  
for sheet_name in sheet_names:
    b2c_data = pd.read_excel(b2c_path, index_col=0, sheet_name=sheet_name)
    c2b_data = pd.read_excel(c2b_path, index_col=0, sheet_name=sheet_name)
    b2b_data = pd.read_excel(b2b_path, index_col=0, sheet_name=sheet_name)
    b2c_data.to_excel(writer, sheet_name=sheet_name, startrow=0)
    c2b_data.to_excel(writer, sheet_name=sheet_name, startrow=10)
    b2b_data.to_excel(writer, sheet_name=sheet_name, startrow=20)
writer.save()
writer.close()

좋은 웹페이지 즐겨찾기