Python Forum
Copy data from different workbooks into Master sheet with Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy data from different workbooks into Master sheet with Python
#1
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:
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 277 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  How to copy work sheet data one workbook to other? sayyedkamran 2 700 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,877 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,059 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,407 Feb-13-2022, 07:13 PM
Last Post: menator01
  Python script for excel sheet Nabil 4 3,288 Jun-01-2021, 05:09 AM
Last Post: Pedroski55
  Copy documents to Teams using python SallySmith 0 2,388 Mar-23-2021, 04:27 AM
Last Post: SallySmith
  Password protected xls data transfer to master OTH 1 3,214 Feb-15-2021, 08:28 PM
Last Post: OTH
  How to Copy Single Value From One Excel Sheet to Another SunWers 4 4,356 Dec-29-2020, 05:39 PM
Last Post: SunWers
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,689 Dec-16-2020, 05:26 AM
Last Post: Vokofe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020