I wanted to import a df to a sheet in excel file where I have multiple sheet including charts, pivot table, slicer. How to import data to that excel file without effecting other sheet. my code is below.
import pyodbc
import pandas as pd
from datetime import datetime
import win32com.client as win32
import openpyxl
import time
import os
import win32com.client as client
from PIL import ImageGrab
import xlwings as xw
import warnings
#read file from path
df = pd.read_csv(r'C:\Users\THL1012\Desktop\Daily Alert_Automation\Test\daily_output.csv')
#Export output to local folder
df.to_excel(r"C:\Users\THL1012\Desktop\Daily Alert_Automation\Test\daily_output.xlsx", sheet_name = 'Sheet1', index = False)
#data import to master file
book = openpyxl.load_workbook(r'C:\Users\THL1012\Desktop\Daily Alert_Automation\Test\Master_DailySales.xlsx')
df = pd.read_excel(r"C:\Users\THL1012\Desktop\Daily Alert_Automation\Test\daily_output.xlsx", sheet_name = 'Sheet1', index_col=[0,1])
with pd.ExcelWriter(r"C:\Users\THL1012\Desktop\Daily Alert_Automation\Test\Master_DailySales.xlsx", mode="w", engine="openpyxl") as writer:
writer.book = book
writer.sheets.update(dict((ws.title, ws) for ws in book.worksheets))
df.to_excel(writer, sheet_name='import')
print("Data imported sucessfully")