How to make multiple copies of a file with Python 3 and a file name list from an Excel sheet

Python 3, openPyXl, os,  shutil on Windows 10

The Problem

As an academic administrator, I have to prepare 70 empty grade report spreadsheets templates at the end of each academic quarter: one for each of my teachers. Each copy of the template sheet needs to be named with the teacher’s name and class number. Then the quarter, title and year is appended to the end. For example:

Stephen Hawking 404-23 Q3 Grades 2017.xlsx

The hard way would be copy and paste a file click the file and rename it, repeating the process 70 error-prone and mind wastingly dull times.  I could also get the teachers to rename the file, but…they are teachers, not administrators so…yeah…errors again.

Python 3 to the rescue:

Continue reading “How to make multiple copies of a file with Python 3 and a file name list from an Excel sheet”

How to Check a Users Home Directory for a Folder – Python 3

Python 3, OS, Example in Windows 10

If you are creating a program for a user where you want to store or use a file in the users home directory, it is not as easy as simply preparing a fixed file location like:


Because if another user on another computer tries to use your program they will start getting errors because their home directory might be something else like:


You can, however, get the users home directory by using Python’s os.path.expanduser method.

Continue reading “How to Check a Users Home Directory for a Folder – Python 3”

How to Display an Entry in a Label – Tkinter Python 3

Python 3, Tkinter 8.6. GUI examples in Windows 10

Probably one of the most common things to do when using a Graphical User Interface (GUI) is to display something from an entry from the user.

Below is a simple example that allows the user to input text in a Tkinter Entry field and when they click “Enter” or use the <Return> or <Enter> button it will be displayed in a Tkinter Label.

The end result will look a little like this:

Display Entry in a Label in Tkinter with Python 3

Continue reading “How to Display an Entry in a Label – Tkinter Python 3”

How to Center the Main Window on the Screen in Tkinter with Python 3

Python 3, Tkinter 8.6. GUI examples in Windows 10

When your first window loads in Tkinter it will generally appear slightly offset from the top left hand corner of the screen. This is a fairly counter intuitive location and most of the GUI driven programs that I run usually open at the center of the page or a little higher than center.

If you want a primer of window positioning, check out the following tutorial:

How Do I Change the Size and Position of the Main Window in Tkinter and Python 3

In Python 3, to put the main window in the center of the screen I use the following code:

Window Centered on the Screen in Tkinter with Python 3
Output in Windows 10

Continue reading “How to Center the Main Window on the Screen in Tkinter with Python 3”

How Do I Change the Size and Position of the Main Window in Tkinter and Python 3

Python 3, Tkinter 8.6. GUI examples in Windows 10

When you create your first window in Tkinter, you can set it’s starting size and position on the screen by using the geometry method.

When using this method, note that it only provides the window with the size and position when it is initialized. This means that the user can then change the size or the window and move it once it has first been put on the screen.

Initial Window Size

Continue reading “How Do I Change the Size and Position of the Main Window in Tkinter and Python 3”

How to Get Something from Google Sheets and Display it in the Sidebar in Google Apps Script

What if you want to get a value or a range from Google Sheets and show it in your sidebar using Google Apps Script?

First you will need to get the value or range by using Googles server-side script. Then you will have to display it client-side in your HTML document.

Documentation on getting the server-side and client-side talking nicely to each other is a little vague. Hopefully, this very basic tutorial will help clear things up.

In this tutorial I will also be using Jquery.

Let’s get started.

Continue reading “How to Get Something from Google Sheets and Display it in the Sidebar in Google Apps Script”

Google Apps Script – Iterating Through Ranges in Sheets the Right and Wrong Way

I was trying to rush out some Google Apps Script code to deal with a task on Google Sheets recently. Basically, I had to search through a heap of data and find certain values and do something too them.

My column was reaching across the page to something like Column BK and my rows were over 1000 deep. Running this code was taking forever!!!

My immediate instinct was:

What have I done wrong?

…and my instinct was right.

The Good and Bad Way to Search Through Code

So after looking at my code again, I discovered that for some reason I go it into my head that I should be searching each cell for the value I needed and then doing something with it.

Sounds logical right? It’s sorta what you are meant to do.

The problem is that I was calling the sever and asking for the range in each cell as I was looping through the entire document. This is super costly and inefficient in terms of time.

Google talks about this in their Google Apps Script Best Practice page under Batch Operations.

Also, if you do run a costly code like this, then you will get a little red light in your Script tool bar that represents your Execution Hints:

Google Apps Script Execution Hints

Clicking on Execution Hints and expanding the side bar with provide you with a far-too-deserving-polite dressing down about your slow and server costly code.

Method Range.getValue is heavily used

The Good

So instead of calling the server for each cell I need to get the full range of the data I am working on, search through it client-side to find what cells require modifying and then invoke the modifications.

Let’s look at a simple example:


In this example I want to search through all the results over multiple quizzes and if there is a dash “-” or a zero “0” change the background accordingly.

Yeah! Yeah! I can do this with conditional formatting, but this is an example, damn it, Jim!!!

Below is a sample of the data I will use and here is the link.

Both Good and Bad examples have the same end result. The result should look like below:

First let’s set up the Google Apps Script code file by calling the user interface, sheet and range of data. (Lost? Create your first Apps Script)

Hopefully everything is self-explanatory here. We call the spreadsheet first and then look for the active sheet. Inside the active sheet we want the range of the data (rangeData) which will contain all the data in the range. We will use that data to get the last row and column number of the data. Finally we will call the server to get he range we want to work with (searchRange).

Once done, we will create our function, onOpen(). When it is called it will create a menu called Checker with the sub menu Bad Way and Good Way. This isn’t necessary, but it might be easier for you to physically test the difference in the slow method versus the fast (correct) one.

The Bad Way

As mentioned above in the Bad (slow) Way we call the server each time to look at what is in a cell.

As I loop across the columns and then the rows, I am using my search range to get the value of the cell in Line 25. This means I am contacting the server a total for 436 times. This significantly slows things down.

The Good Way


In the preferred approach I am taking the array that I created from searchRange.getValues() in Line 36  and searching through it before I make my calls to change the background when a dash or a zero occurs.

Why is this better?

I only make server call to collect the range data once. Then client-side (on my computer in this instance), I do all my searching before calling Google who collects all the changes in a cache until the loops are done before creating background colors all at once.

Super fast.

Speed comparison. 

Take a look at the speed differences over ten tests:

The Good Way is the clear winner. You can try it out for yourself if you have been playing along by going to <View><Execution Transcript> in the Script

Take Home

The take home from this is that, make as little calls to the server as possible. It significantly improves your speed.

The Full Code







Copy and paste ranges in excel with OpenPyXl and Python 3

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.

Python Logo

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 Example

Source Data

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.

Sample Data

Receiving File

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:

receiving File


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.

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.

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.

We will be up for two more for loops to do this.

Let’s take a look:

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.

This is how it will look:

Line 1 creates the function and then line 3 runs the copyRange process with the arguments we need.

Note! After line 3 you could call a function to manipulate the data or add styling here before you paste it.

Line 4 then runs the pasteRange fuction with the arguments we need.

Line 5 then saves the pasted data in the same file that we used in our memory. You could equally save the file with a different name and this will create a brand new file with the pasted data in it.

That’s it! Done.

Run the program

Now it’s time to run the program. Save the file and hit run (F5).

In the Python Shell run the program and then enter:

Your result will look like this:

No too impressive, right?

Go into the folder and open your Section12Grades.xlsx spreadsheet.

It should now look like this:

End Result of openpyxl copy and paste range

The full OpenPyXl copy and paste range code




Can I modify Google Sheets with code?

Google Apps Script and the Google Suite

You sure can. Google has a fully supported script editor that you integrate with your Sheets, Docs, Forms, Slide, Gmail, Calendar and pretty much every aspect of the Google Suite. It’s name: Google Apps Script.

Google Apps Script allows you to do all sorts of things like building short code to modify sheets and docs, create macros, develop add-ons mess around with gmail and so much more.

Google Apps Scripts is based on Javascript. It can integrate with HTML5, CSS and Javascript well with it’s html service class.

Google has an extensive reference library to get you started.

Let’s get started with a basic example.

Dawh…Your first Google Script

In our first script we are going to create a menu button that will execute an alert that will pop up on the screen and say something awesome. This is definitely not a Hello World example…definitely…maybe.

On the menu bar click >Tools>Script editor…

script editor Google

This will open a new page with Googles in-build script editor.

script editor Google open

Lets take a quick look at the UI.

Up top, you can see that it says Untitled Project. All Googles Script files are wrapped in a project and are given an identifier. This enables you to share the script with other files or other people for that matter.

On the left, you can seen a list of files. Now we only have one the gs stands for Google Script. You can add other files like *.html, *.css or *.js

To the right is the script editor. This is where you will be writing your program.

1. Title your project

Go ahead and title your project in the top box where it says: Untitled Project. I’m going to name mine the same as my spreadsheet: Not Hello World.Google Script Editor Name Project

I’ll keep the Google Script file name the same and get stuck into the code.

2.  Creating a Custom Menu

We are going to customise our menu by adding a button to run our script on the right hand side of the Help menu drop down.

You can also create drop-down lists and child drop-down lists too. Check out Google’s Custom Menu page for instructions.

Before we start the code, let’s delete what is already there. Then add the following:

Let me explain what is going on here.


  1. Creates the the function onOpen() . This is just a self explanatory name to help us remember what this function does. It won’t actually do anything unless we ask it to inside the function.
  2. Sets an easy-to-use variable we called, ui (for User Interface), and calls the Google spreadsheetApp and asked it to get the UI for us to work with.
  3. We then use that variable ui to create a menu that we have called ‘OMG!’
  4. Adding to this menu we are creating we want to add a item (.addItem) that we have titled, ‘This is...‘ that we will click and it with run and alert in another function we will create soon called ‘alertMaker‘.
  5. Finally, we want Google to add our menu to the User Interface on our spreadsheet.

When you are ready, hit <ctrl>+<s> to save or go to <File> and click <Save> in the Script Editor. Now click the browser tab with your spreadsheet in it and click refresh.

If all went well, you should have a shiny new menu item on the right hand side of your help button.

It will look like this:

Google Script Editor Menu

If you click the “This is…” link it will cause an error because we haven’t created the class yet. Let’s do this now…

3. Making something happen when you Click the Custom Menu Item

Let’s keep it simple, add the following to your code:

Here we have created an alert. When you cilck the ‘This is…’ link.

In line:

  1. We create the function alertMaker() that we referenced back in line 4.
  2. We ask Google to call the spreadsheet again, specifically requesting the User Interface. We then ask it to bring up an alert with the word ‘AWESOME!!!’

Go ahead and save the script again and head back to your spreadsheet and navigate to your ‘This is…’ link in your ‘OMG!’ menu (No need to refresh the page).

Give it a click.

Google Script Editor alert

That’s it your first Hello World script using a menu.