#load libraries
import pandas as pd
from openpyxl import Workbook, load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
#read sample excel file
wb = load_workbook(filename = 'sample.xlsx')
print(wb.sheetnames)
#read new data file
df_new = pd.read_csv('PFE.csv')
df_new.shape
df_new.head()
#demostrates how to insert new rows on top (below the header row)
ws = wb['PFE']
ws.insert_rows(idx=2, amount=5)
i = 0
for row in ws['A2:G6']:
j = 0
for cell in row:
cell.value = df_new.iloc[i, j]
j = j+1
i = i+1
#first clear contents
#then assign values
ws = wb['PFE2']
print(ws.max_row, ws.max_column)
for row in ws['A2:G' + str(ws.max_row)]:
for cell in row:
cell.value = None
print(ws.max_row)
i = 0
for row in ws['A2:G' + str(df_new.shape[0])]:
j = 0
for cell in row:
cell.value = df_new.iloc[i, j]
j = j+1
i = i+1
wb.save(filename = 'sample.xlsx')