I have written a program using Python 2.7 to look through a .csv file with different variables pertaining to a local public archives database (so, title of document, year published, author (if applicable), etc.). The program recognizes when there are blanks in the data (no author, for example), and then uses the corresponding url of the image for the original document to open up the image in a browser window. Then, the user enters in the missing information in the terminal window if they can find it written somewhere on the document.
To save time on the number of clicks necessary to make by hand, I have been trying to programmatically have the file return to terminal window for inputting the missing information after the program opens the original document image url in a browser. My current solution works, but only for the first observation that's missing a piece of information. After that, it errors on the line that tells it to refocus to the terminal(last line in code below). Here's the error message:
pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')
Here's my code:
#Step 1: read the csv file into python
import csv
import webbrowser
import os
from bs4 import BeautifulSoup
import re
import urllib2
import win32gui
import time
#Function for refocusing on terminal screen --looks for Anaconda, since using Anaconda Python terminal
def enumHandler(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
if 'Anaconda' in win32gui.GetWindowText(hwnd):
win32gui.SetForegroundWindow(hwnd)
out = open("missing_info.csv", "rU")
data = csv.reader(out)
data = [row for row in data]
out.close()
#find the first observation that is EMPTY, and start there
i = 0
for x in range(1, len(data)):
if data[x][15]=="":
i = i + 1
else:
i = i + 0
start = len(data) - i
#Now loop over the observations
for x in range(start, len(data)):
print ""
print "This is observation ID number", data[x][0]
#missing author name only
if data[x][12]=="1" and data[x][13]=="0" and data[x][14]=="0":
print "Missing Author Name only"
url = data[x][10]
#now get the actual image
html_content = urllib2.urlopen(url)
soup = BeautifulSoup(html_content)
a = soup.find_all("script")
b = str(a)
c = re.findall(r"var record.*", b)
d = str(c)
e = re.findall("imageURL.*", d)
f = (e[0].encode("utf-8")).replace("var record = ", " ")
g = f[11:]
#now, need everything before the first quotation mark
h = re.split('"', g)
i = h[0]
webbrowser.open_new(i)
time.sleep(1) #included to give time for image to load before refocusing in the terminal screen
win32gui.EnumWindows(enumHandler, None) #returns to terminal screen
Since I anticipate someone asking this, I've already tried using SetFocus instead, but I get an error saying access is denied when I do that. As reference, I'm using Anaconda's command prompt and Anaconda's install of Python 2.7.
Any help on figuring out why this works for the first observation missing data but then gives me an error for any subsequent observations that fit the parameters of the loop/if statements to have info entered in from the image would be greatly appreciated!