Set the Paper Size and Orientation in a Doc Using Google Apps Script

Google Apps Script: DocumentApp setPageWidth, setPageHeight

Sometimes you need to prepare a Google Doc’s paper size and orientation programmatically using Google Apps Script.

Unfortunately, you can’t just call for say, A4 in Landscape. Okay, not until now (see my code below).

Google Apps Script does provide a way to set the dimensions of your page  in the body class by using:

  • setPageWidth(pageWidth)
  • setPageHeight(pageHeight)

The page widths and heights are measured in PostScripts Points which is a bit of a pain too.

Here is an example of setting and A3 paper size in Landscape.

Ugh. What a chore. You need to find the dimensions of the paper in points.

Enter this little nifty function and your life will be so much easier:

Continue reading “Set the Paper Size and Orientation in a Doc Using Google Apps Script”

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 to 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 Create a Simple YouTube Download Program with a Progress Indicator in Python 3 with pytube

Python 3, pytube, os in Windows 10

Some of my friends live in an area that really struggles to get decent internet speeds in the afternoons and evenings. So much so that they can barely watch a YouTube video at 144p some days, and that is not particularly useful if they are trying to watch a video with code or some technical specs on the screen.

They really needed to download the videos, but really did not trust the programs available online to download videos for me without spamming them with advertising or adding some malicious malware to their beloved computers.

Fortunately, someone developed a Python 3 library to do just that – pytube.  In an earlier post I dive into some of the main aspects pytube:

How do I download YouTube videos with Python 3 using Pytube?

In this post, I am going to show you a quick app that can be run in the Python shell to download videos that features a progress indicator (not quite a progress bar).

I’ve intentionally kept the program fairly limited so you can focus on the important parts. I’ll show you the code and an example of what it looks like when it is running first and then give you the breakdown where you can focus on what you need to know and ignore the rest.

Continue reading “How to Create a Simple YouTube Download Program with a Progress Indicator in Python 3 with pytube”

How to Check a User’s 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:

C:\Users\yagisanatode\

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:

C:\Users\batman\

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

Continue reading “How to Check a User’s Home Directory for a Folder – Python 3”

How do I download YouTube videos with Python 3 using Pytube?

Python 3, pytube 9.0.6 in Windows 10. 

pytube is a very easy to use light-weight library that you can use to download YouTube Videos.

Installation can be achieved in the terminal or command prompt with pip:

To download a video from YouTube you don’t really have to do much. So much so that you can get everything you need from the shell and be downloading a video in two lines of code.

In line 2, you can see that we have copied and pasted our YouTube URL. You can either use the one in the Address Bar on the one that appears when you click “Share” on YouTube.

Continue reading “How do I download YouTube videos with Python 3 using Pytube?”