Aug-27-2019, 07:36 AM
I have to copy data from different workbooks and paste it into a master workbook. All the workbooks including the Master file are located in a folder: C:\Users\f65651\data
So far I have been able to copy data from only one workbook using the code below:
I have tried storing them into dataframe but it messes up the format in the Result sheet, so using df is out of it! or if there is any other way around. Please kindly suggest.
So far I have been able to copy data from only one workbook using the code below:
import openpyxl as xl path1 ='C:\\Users\\f65651\data\\cq1.xlsx' #One of the doc #path3 = 'C:\\Users\\f65651\data\\cq2.xlsx' path2='C:\\Users\\f65651\\data\\Results.xlsx' #Master Workbook wb1 = xl.load_workbook(filename=path1) ws1 = wb1.worksheets[0] wb2= xl.load_workbook(filename=path2) ws2= wb2.worksheets[0] for row in ws1: for cell in row: ws2[cell.coordinate].value=cell.value wb2.save(path2)I need to be able to do this to incorporate all the workbooks. So for example, Workbook 2 is 'C:\\Users\\f65651\data\\cq2.xlsx'. 3,4,5 and 6 data should also be copied into the Result master sheet.
I have tried storing them into dataframe but it messes up the format in the Result sheet, so using df is out of it! or if there is any other way around. Please kindly suggest.