rolisz's site

Automating using Python

A friend of mine who has a restaurant kept asking me to update the menu on his site. Each week he would send me the new menu, in .doc format, I would upload it to Google Drive (I don't have Office on my computer), I would take 3 screen­shots (one for each page), rename them and upload them using FTP to his server.

But, being a programmer, possibly the only profession in the world where laziness is a quality, not a defect, I decided to automate this as much as possible (teaching him how to write the tables in HTML and uploading files to FTP was too hard).

At first I only automated the renaming and uploading part. It was a simple script, I simply dropped the three images onto the Python file and they would magically appear in the right place with the right name.

Taking the screen­shots would be harder - or so I thought. I discovered PyRobot, a very nifty little Python script that makes automating Windows things super easy. You can move the mouse, click around, enter text, control the clipboard, take screen­shots, etc.

The problem with the screen­shots was that they needed to be of only a portion of the image - the one that contained the actual menu, without the Google Docs stuff, and that I needed to take 3 screen­shots, of 3 separate pages, so I needed scrolling.

With some trial and error I found the right pixel co­or­di­nates for taking the screen­shots. But the scrolling was in­con­sis­tent. I would request 100 scroll units and sometimes it would take me to the second page, sometimes to the third, sometimes to the end of the document.

I looked into the internals of the library and saw that it was simply doing a loop of the requested times over some Win32 API call.

Based on a hunch, I did the same thing, but did a sleep for 0.1 in each iteration. In­con­sis­ten­cy problem solved. :D

for _ in range(7):
robot.scroll_mouse_wheel('down', 1)
sleep(0.1)

My completely uninformed pre­sump­tion is that at times, Windows can't handle all the scrolling requests and it simply eats them.

TIL: Automating things is easy in Python (what isn't?), you just have to find the right pixel co­or­di­nates and the patience to scroll "slowly". Also, I had the op­por­tu­ni­ty to contribute to an open source Python library :D