How I Use Python for Windows Automation

or how to make Adobe Premiere less painful

A bit of preface

At the time of writing, I currently work in a small shop that provides recording services for conferences. The needs of a talking head at a podium are pretty basic from an editing stand point, so the way I organize my Premiere projects has adapted accordingly. After a bunch of iterations, I ended up with a folder hierarchy that is almost completely flat. Namely, one audio folder, one video folder, and then individual sequences for each talk.

It’s a slightly odd way to go about things, but it works pretty well for keeping track of the hundreds of presentations, while still getting things turned around quickly. The only thing that sucks about it is that it takes forever to set up. It’s horrifically tedious. And I have a tragically small amount of patience for such tasks.

Problem 1: Session IDs.

Each conference has their own unique way of labeling their sessions. While some (awesome ones) simply stick with sequential numbering, the bulk of them implement their own arbitrary ID schemes. Thus, when it comes to edit, those IDs need to be put into Premiere so that we can easily track down what goes where.

These used to be entered by hand.

Fun fact: It’s actually impossible to perform the task without briefly considering suicide as a viable escape from the monotony. It is mind-numbingly tedious.

Problem 1.a: Building the sequences

Now, before the IDs can actually be entered, there’s that tiny prerequisite of actually building all the sequences. My solution for this has always been the classic: spamming Ctrl + V.

It works, but at the expense of a tired pinky finger. So, with those initial problems (and sweet gifs) in mind, I finally get to my point:

Automating Adobe Premiere

Time to put PyRobot to work!

First things first, getting the Session IDs loaded into Python. This part is pretty trivial. Open the csv file, loop through and pull out the column that holds all of the IDs.

def load_data(filename):
  with open(filename, 'rb') as f: 
    csvreader = csv.reader(f, delimiter=',')
    return [x[0] for x in csvreader]


Now that we've got that data, it’s time to actually start automating some stuff!

Generating the Sequences

I figured I’d stay with my copy/paste strategy, but delegate the key spamming over to Python.

def build_sequences(robot, num_seq):
  for sequence in range(num_seq):

def main():
  robot = Robot() 
  session_codes = load_data('session_codes.csv')
  num_sessions = len(session_codes)
  build_sequences(robot, num_sessions)


Here we load the csv file to get the list of IDs, then from that list we calculate the total number of IDs (presentations), and then pass in that number to the build_sequences function so that it knows how many times to fire off the copy and paste function.

The delay at the beginning is just so I have enough time after I start the script to give focus to the appropriate window inside of Premiere.

Not bad! All 200+ sequences that I needed were generated in about 0.3 seconds. Tis a good little copy/paster.

Adding Session IDs

Now that we have all of the sequences, next thing we need to do is give them the client provided IDs. Luckily, Premiere actually makes this part pretty easy. Anytime you have a sequence highlighted in the Project pane, if you press the return key, it drops the cursor down to the next sequence and highlights its name. So, the entire series of events that need to be emulated boils down to simply typing the session ID, and sending pressing a key.

def assign_names(robot, codes):
  for code in codes:

def main():
  assign_names(robot, session_codes) 


Done and done. With this little script in place, setting up a Premiere project with hundreds of individual sequences went from taking me about 45 minutes, to only a handful of seconds.

Part II: Collecting Notes

One final thing that I use PyRobot for with Premiere is extracting all of my edit notes from the program.

For most conferences there will be a handful of sessions which have either additional edit requirements, or cannot be used for one reason or another — which is usually caused by something mundane like the speaker flat out refusing to use the god damn microphone despite the fact that they are presenting in an auditorium and have a camera pointed at them (Seriously people, use the microphone). These things have to be collected and reported the the Conference Manager.

I tend to keep all of these notes directly in premiere by appending them to the Sequence name. It makes it a little easier to update throughout the edit cycle versus keeping a separate excel file open and storing everything in there. Ultimately, by the time I’m finished, I end up with something like this:

However, the drawback of this workflow is that, like the previous problems, the notes had to be manually copied out of premiere and pasted into something usable for the client.

To solve this one, I had to implement a new feature in PyRobot: Reading from the Windows clipboard. Which in retrospect seems like an obvious feature, but it didn't occur to me until I had an actual need for it

So, a few quick minutes of sleuthing around on MSDN and I had found the solution, implemented it, and was ready to rip some data.

The method for retrieving is pretty much the exact opposite of what I used to add the session IDs (obviously).

def retrieve_notes(robot, num_sessions):
  def _copy():
    return robot.get_clipboard_data()
  return map(lambda x: _copy(), range(num_sessions))

I again take advantage of the fact that Premiere highlights the name of a currently selected sequence anytime you press the return key. So the whole of the algorithm is simply pressing the enter key, copying the notes to the clipboard, and then returning the contents as a string.

Once I’ve got the notes into Python land, I’m pretty much free to do whatever I want. So, I pass them off to another formatting script which pretties them up and dumps to an end user friendly excel.

So there it is. How I use Python for Automation.

Note: For non-boring automation stuff, about a year ago I wrote a tutorial on driving web games for Active Tutorials. You can check it out here. Bear in mind though, I had been programming all of a few months at the time of that article. The code works, but dear sweet Odin.. it’s embarrassing to look at.