By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,819 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,819 IT Pros & Developers. It's quick & easy.

Copy data from different workbook into Master sheet using Python

P: 3
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. import openpyxl as xl
  3.  
  4. path1 ='C:\\Users\\f65651\data\\cq1.xlsx' #One of the doc
  5. #path3 = 'C:\\Users\\f65651\data\\cq2.xlsx'
  6. path2='C:\\Users\\f65651\\data\\Results.xlsx' #Master Workbook
  7.  
  8. wb1 = xl.load_workbook(filename=path1)
  9. ws1 = wb1.worksheets[0]
  10.  
  11. wb2= xl.load_workbook(filename=path2)
  12. ws2= wb2.worksheets[0]
  13.  
  14. for row in ws1:
  15.         for cell in row:
  16.             ws2[cell.coordinate].value=cell.value
  17. wb2.save(path2)
  18.  
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.
2 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.