473,473 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Thread Modes Python code to copy data from multiple workbooks into master sheet

3 New Member
I have to copy data from 6 workbooks and paste it into a master workbook. All the workbooks are located in a folder on my desktop: C:\Users\f6565\Desktop\data

The workbooks contain a sheet named 'Main Data', I have to open each workbook, go to sheet 'Main Data’, select columns range A to GJ starting from row 5 to row 'x' (end of the rows), then copy and paste the data range into the master worksheet. In the master worksheet (named MAIN DATA), I paste the data into Column A row 5 till the end and continue pasting/appending the data as I copy data from more workbooks. Eventually, the master workbook has the data in columns A row 5 to GJ from every workbook in one sheet.

The columns range A to GJ and starting from row 5 always remains constant in all the sheets (Main Data) of every workbook. Each workbook contains several sheets, but I am only interested in sheet ‘Main Data’. I have to repeat the same steps for the 6 workbooks and continue pasting/appending the data into master sheet. So, I was wondering if someone could please help me to create a python code for this?
I was able to do this with VB and it works. However I need to be able to do this with python entirely!. Here is my VB code:


Expand|Select|Wrap|Line Numbers
  1. Sub CopyRange()
  2.     Application.ScreenUpdating = False
  3.     Dim wkbDest As Workbook
  4.     Dim wkbSource As Workbook
  5.     Set wkbDest = ThisWorkbook
  6.     Dim LastRow As Long
  7.     Const strPath As String = "C:\Users\f6565\Desktop\data\"
  8.     ChDir strPath
  9.     strExtension = Dir("*.xlsx*")
  10.     Do While strExtension <> ""
  11.         Set wkbSource = Workbooks.Open(strPath & strExtension)
  12.         With wkbSource
  13.             LastRow = .Sheets("Main Data").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  14.             .Sheets("Main Data").Range("A5:GJ" & LastRow).Copy wkbDest.Sheets("MAIN DATA").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
  15.             .Close savechanges:=False
  16.         End With
  17.         strExtension = Dir
  18.     Loop
  19.     Application.ScreenUpdating = True
  20. End Sub

I'm not so good with python and would really appreciate your help!

Please let me know if you require any clarification.

Many thanks!
Aug 20 '19 #1
2 1119
dwblas
626 Recognized Expert Contributor
I'm not so good with python and would really appreciate your help!
You have posted nothing that we can help with. Post the code that you have so far.
Aug 20 '19 #2
Fatman003
3 New Member
So far I have done this:

Expand|Select|Wrap|Line Numbers
  1. import openpyxl as xl
  2.  
  3. path1="C:\Users\f6565\Desktop\data\data1.xlsx"
  4. path2="C:\Users\f6565\Desktop\data\Result.xlsx"
  5.  
  6. wb1 = xl.load_workbook(filename=path1)
  7. ws1 = wb1.worksheets[0]
  8. wbX = xl.load_workbook(filename=path0)
  9. wsX = wbX.worksheets[0]
  10. wb2= xl.load_workbook(filename=path2)
  11. ws2= wb2.worksheets[0]
  12.  
  13. for row in ws1:
  14.         for cell in row:
  15.             ws2[cell.coordinate].value=cell.value
  16. wb2.save(path2)

However, it only copies some data values and not every data. Also, I was only able to transfer data from one workbook and not the 7.
Aug 21 '19 #3

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

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
0
by: eyal | last post by:
Hello, I want to open 2 workbook, , lets say workbook1 and workbook2 now I want to copy data from a sheet name "TEST" in workbook2 and paste the data to a sheet named "TEST" in workbook. now...
3
by: natrajsr | last post by:
Hi, I want to load the data of a excel sheet or in the exact excel sheet format into a Rich TextBox control. I have already worked with loading WORD into a Rich TextBox. It is working fine.;...
9
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
0
by: Yue Fei | last post by:
I have a multi thread python code, threads can start immediately if I run on command line, but I can get them started right the way if I call the same code from C/C++. test code like this: from...
1
by: DerrickProcurement | last post by:
I have no visual basic experience and all answers I have found required that I had some experience. I hope the questions is simple and that a professional can easily provide me with the proper code...
17
by: epifinygirl | last post by:
Hi, I'm relatively new to VBA but I am trying to copy data from a website (this website is currently running and will be the same) and paste to defined sheet in Excel. Below is the code that I...
0
by: javedshaikm | last post by:
Data from a XML sheet is converted to php array and I have tried to create a quiz format question with the below code, but it does't work. Not sure how to get it working for multiple questions... ...
5
by: scrapcode | last post by:
Hi everyone. I'm trying to write a macro to copy data from one Excel sheet to another in the same workbook when a certain criteria is met. My data table is an import from an Access DB. After the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.