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 screenshots (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 screenshots 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 screenshots, etc.
The problem with the screenshots 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 screenshots, of 3 separate pages, so I needed scrolling.
With some trial and error I found the right pixel coordinates for taking the screenshots. But the scrolling was inconsistent. 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. Inconsistency problem solved. :D
for _ in range(7): robot.scroll_mouse_wheel('down', 1) sleep(0.1)
My completely uninformed presumption 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 coordinates and the patience to scroll "slowly". Also, I had the opportunity to contribute to an open source Python library :D