Sometimes you want to be able to automatically move a row from one sheet to another based on the value of a certain cell.
One of the first things that come to mind, and I am sure it does for you dear reader, is when I took advantage of the Great Chicken Transformation back in, oh, 2019, I believe.
Folk kept turning into chickens, while other folk were wanting eggs. It just so happened that I had the farm to make it all happen.
First, though, I needed to keep a tab of every person I knew and if they turned into a chicken. If they did, then they were destined for the pen.
…Note to self: it may be late at night, but dam Yagi, your analogies are tight!
Google Sheets and Google Apps Script to the rescue.
So first off I set up a sheet named: Plague. Here I put all the people I knew, so I could watch em good and propper.
Next, I set up a sheet named Farm. These are for the people who turned into chickens. No harm in profiting from a few newly formed egg layers, right?
Whenever a new transformation occurs, I find the person on the Plague sheet and then select “Yes” to say that they have turned into a chicken and will now be spending their days on the farm. Upon editing (onEdit) this cell to “Yes”, the row is copied and pasted to the Farm sheet.
Have you ever copied and pasted something in Google Sheets only to be frustrated with the fact that it stubbornly refuses to paste the column widths? I mean, everything else is perfect, the formatting, the formulas the comments, they all were pasted across just fine. However, that dastardly column width just does not budge.
OpenPyXl is a Python open library that allows you to read and write Microsoft Excel files. Specifically, the ‘*.xlsx’ file extension. It helps you to create programs to create and modify files and automate your processes in excel.
NOTE: This post requires that you have some knowledge of Python and the OpenPyXl library. The library also needs to be installed for you to use.
Quite often, I find that I have to work with ranges of data that I need to either copy and paste into a new file or files, or copy > modify > paste into files.
The OpenPyXl library allows you to look at every cell of a file and either copy it or modify it by using the openpyxl.worksheet.Worksheet.cell() method. This method allow you to access each cell by the row and column as a numerical value.
Note! Unlike everything else in coding, rows and columns start with one(1) and not zero(0).
To select whole ranges of our data we need to iterate through it by both row and column and then store that data in a list to be pasted to the new file, spreadsheet or location that we desire.
The following example will take you through the process. For your own data you will need to modify the file, sheet and range locations. Everything else should be good to go.
You can find the whole code at the end of the post.
Why does your MS Excel look weird?
To be honest my screenshots of the ‘.xlsx files will be in Libreoffice. But this simple example will be able to load without issue in MS Excel.
The source data for this example is a very simplified version of grade data that I work with day-to-day. The goal of our program is to simply copy the Section 12 results into a new file. The file for this spreadsheet is called: GradeSample.xlsx. I have put it in the same folder as my Python program.
For the purpose of this example we have a file that we want to save this data into: Section12Grades.xlsx. We could just as easily use a template and save the file under a different name – for example template.xlsx could save as sec12Grade.xlsx. This could be useful if I wanted to save a file for each section.
The receiving file looks like this:
Loading the Source Data and Receiving File
Okay, let’s get started with the program. First we need to load both the source data and the receiving file.
Loading the Spreadsheets
#! Python 3
# - Copy and Paste Ranges using OpenPyXl library
#Prepare the spreadsheets to copy from and paste too.
#File to be copied
wb=openpyxl.load_workbook("GradeSample.xlsx")#Add file name
sheet=wb.get_sheet_by_name("Grades")#Add Sheet name
#File to be pasted into
template=openpyxl.load_workbook("Section12Grades.xlsx")#Add file name
temp_sheet=template.get_sheet_by_name("Sheet1")#Add Sheet name
We import he OpenPyXl library first.
Next, we’ll open the source data with wb = openpyxl.load_workbook("GradeSample.xlsx") . Once we have loaded the workbook we need to tell Python which sheet tab we want it to work in. We do this by calling the workbook (wb) and then get the sheet by it’s name: sheet = wb.get_sheet_by_name("Grades")
We repeat this step with the receiving data file that we want to paste our Section 12 grades into.
Copying the Section 12 data
Looking at the section 12 data, we want Python to be able to copy from column A2 to D14. the OpenPyXl .cell() method takes a number which is much easier to iterate, rather than the column letters. So let’s look at that range again:
From: A2 is now column = 1 and row = 2
To: D14 in now column = 4 and row = 14
Once we access this data, we need somewhere to store it before we paste it into the new file. We will use a nested list for this.
Copy Range Data
#Copy range of cells as a nested list
#Takes: start cell, end cell, and sheet you want to copy from.
#Adds the RowSelected List and nests inside the rangeSelected
In line 3 we create our function copyRange. It contains 5 arguments that we could add with our information as follows:
startCol = 1
startRow = 2
endCol = 4
endRow = 14
sheet = sheet ( The name of the sheet variable we are copying the data from which is GradeSample.xlsxsheet Grades)
In line 4 we create an empty list called rangeSelected this list will have data from every row we have selected.
Line 5 starts the for loop through each rows. Each row contains data in each column so we create an empty list (rowSelected) here in preparation to add the column data to in the next for loop (Line 6).
Line 6 loops through each item in each column of the selected row. Line 7 then adds the data from each column to the rowSelected lists.
Once it finishes this loop, it adds the data from the rowSelected lists into the rangeSelected lists. It then moves down to the next row and repeats the process continuing through the rows until the loop meets it’s end at row 14.
Finally the copyRange function returns the rangeSelected list to be used at our discretion.
Pasting the selected data
Now we have a nested list of all the rows and the information in the columns we require from those rows.
We will use that list add it to our Section12Grades.xlsx in Sheet1.
We want to add the data starting at row 3 this time because we have a title on row 1 and column headers on row 2.
Line 3 starts our pasteRange function and contains the following arugments:
startCol = 1
startRow = 3 (because we want to paste the data 1 row down.)
endCol = 4
endRow = 15 (because we want to paste the data 1 row down.)
sheetReceiving = temp_sheet (This is the variable for Section12Grades.xlsx with the sheet name, Sheet1.
copiedData = rangeSelected ( The returned list from your copyRange function)
Line 5 creates a count number variable starting with zero(0) so we can start looking through our copiedData lists by rows from the beginning.
Line 6 begins the row loop like the copyRange function.
Line 7 adds another count number variable so we can loop through our copiedData list by columns.
Line 8 begins the column loop.
Line 9 adds the copiedData cell-by-cell to the new file. It does not save it here but holds it in memory to be saved to the file we choose in the future.
Finally, we add 1 to each of the counters so we can move onto the next cell.
Running a Copy and Paste as a function
We are now going to create a function to copy the data using the copyRange function and paste the data using the pasteRange function and then we will save the Section12Grades.xlsx file contained in the variable, template.
selectedRange=copyRange(1,2,4,14,sheet)#Change the 4 number values
pastingRange=pasteRange(1,3,4,15,temp_sheet,selectedRange)#Change the 4 number values
#You can save the template as another file to create a new file here too.s
print("Range copied and pasted!")
Big thanks to WY in the comments for reminding me to update this.
Want to learn how to automate your daily admin at work with Python? Udemy has some great Python automation courses that will help you learn how to automate your tasks so you can focus on what really matters.
Got a more specific problem you need help with, but don’t have the time to develop the skills? Fiverr’s your best bet to find a skilled professional to solve your problem quickly and cheaply. *
*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.