I have written a python script to save a book. It takes screenshots, turns pages(pyautogui click) and combines the screenshots into a PDF.
This is what happens on executing code in vscode:
JavaScript
x
8
1
PS C:UsersAdmin> & C:/Users/Admin/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/Admin/Desktop/New folder/Windows_v1.py"
2
On page 1
3
On page 2
4
On page 3
5
On page 4
6
Combining PDF
7
PS C:UsersAdmin>
8
This is what happens in cmd
JavaScript
1
7
1
C:UsersAdminDesktopNew folder>python Windows_v1.py
2
On page 1
3
On page 2
4
On page 3
5
Combining PDF
6
C:UsersAdminDesktopNew folder>
7
This is what happens on powershell
JavaScript
1
4
1
PS C:UsersAdminDesktopNew Folder> python Windows_v1.py
2
On page 1
3
On page 2
4
…and it just stays there(tested for 15 minutes).
The script:
JavaScript
1
212
212
1
# Not in standard library- img2pdf, pyautogui, PIL
2
import img2pdf
3
import time
4
import tkinter as tk
5
from PIL import ImageGrab
6
import pyautogui
7
import os
8
9
#Main window creation
10
11
# Top level window
12
frame = tk.Tk()
13
frame.title("Screenshot PDF maker")
14
frame.geometry('400x400')
15
frame.wm_attributes('-alpha',1)
16
17
# Transparent Button Creation
18
def makeTrans():
19
frame.update()
20
frame.wm_attributes('-alpha',0.3)
21
frame.update()
22
23
transButton = tk.Button(frame,
24
text = "Make window transparent for ease",
25
command = makeTrans)
26
27
28
# TextBox Creation
29
inplabBookName = tk.Text(frame,
30
height = 2,
31
width = 20)
32
33
inpSaveLoc = tk.Text(frame,
34
height = 2,
35
width = 20)
36
37
inpPageNo = tk.Text(frame,
38
height = 2,
39
width = 20)
40
41
# Create labels for main page
42
labBookName = tk.Label(frame, text = "Book name")
43
labBookName.config(font =("Arial", 8))
44
45
labIns = tk.Label(frame, text = "Arrange and shape the window so that it covers the entire page.n Do NOT include title bar")
46
labIns.config(font =("Arial", 9))
47
48
labSaveLoc = tk.Label(frame, text = "Location")
49
labSaveLoc.config(font =("Arial", 7))
50
51
labPageNum = tk.Label(frame, text = "Number of pages")
52
labPageNum.config(font =("Arial", 7))
53
54
#Pack everything
55
labIns.pack()
56
transButton.pack()
57
labBookName.pack()
58
inplabBookName.pack()
59
labSaveLoc.pack()
60
inpSaveLoc.pack()
61
inpSaveLoc.insert(tk.END, "D:\Books")
62
labPageNum.pack()
63
inpPageNo.pack()
64
frame.wait_visibility(frame)
65
66
#Next Button
67
var = tk.IntVar()
68
nextButton = tk.Button(frame, text="Next", command=lambda: var.set(10))
69
nextButton.pack()
70
71
nextButton.wait_variable(var)
72
73
#Perpare mouse detection screen
74
75
#Remove old elements
76
labIns.pack_forget()
77
transButton.pack_forget()
78
labBookName.pack_forget()
79
inplabBookName.pack_forget()
80
labSaveLoc.pack_forget()
81
inpSaveLoc.pack_forget()
82
labPageNum.pack_forget()
83
inpPageNo.pack_forget()
84
nextButton.pack_forget()
85
frame.wm_attributes('-alpha', 1)
86
87
# Create labels
88
labMouseX = tk.Label(frame, text = "Mouse X")
89
labMouseX.config(font =("Arial", 7))
90
91
labMouseY = tk.Label(frame, text = "Mouse Y")
92
labMouseY.config(font =("Arial", 7))
93
94
95
#text inputs for mouse screen
96
inpmox = tk.Text(frame,
97
height = 2,
98
width = 20)
99
100
inpmoy = tk.Text(frame,
101
height = 2,
102
width = 20)
103
104
inpWait = tk.Text(frame,
105
height = 2,
106
width = 20)
107
108
autoDetlabIns = tk.Label(frame, text = "Press the button and move your mouse over the Next Button to turn the page. n The co-ords will be picked up 3 seconds after press.")
109
autoDetlabIns.config(font =("Arial", 7))
110
111
waitlabIns = tk.Label(frame, text = "Seconds of delay it takes to change page after clicking")
112
waitlabIns.config(font =("Arial", 7))
113
114
startlabIns = tk.Label(frame, text = "This window will disappear. You do not need to move your mouse or press any key. n When the PDF is ready, the program will close itself")
115
startlabIns.config(font =("Arial", 7))
116
inpmoy.insert(tk.END, "1")
117
118
#buttons for mouse screen
119
def autodetmouse():
120
time.sleep(3)
121
temx, temy = pyautogui.position()
122
inpmox.delete(1.0, tk.END)
123
inpmoy.delete(1.0, tk.END)
124
inpmox.insert(tk.END, temx)
125
inpmoy.insert(tk.END, temy)
126
127
autodet = tk.Button(frame,
128
text = "Auto detect mouse position",
129
command = autodetmouse)
130
131
132
#Pack everything for mouse screen
133
labMouseX.pack()
134
inpmox.pack()
135
labMouseY.pack()
136
inpmoy.pack()
137
autoDetlabIns.pack()
138
autodet.pack()
139
waitlabIns.pack()
140
inpWait.pack()
141
startlabIns.pack()
142
143
# Start button creation
144
var = tk.IntVar()
145
startButton = tk.Button(frame, text="Start", command=lambda: var.set(1))
146
startButton.pack()
147
148
frame.update_idletasks()
149
frame.update()
150
151
startButton.wait_variable(var)
152
153
#Remove all elements and make transparent
154
labMouseX.pack_forget()
155
labMouseY.pack_forget()
156
autoDetlabIns.pack_forget()
157
autodet.pack_forget()
158
startlabIns.pack_forget()
159
startButton.pack_forget()
160
161
frame.update_idletasks()
162
frame.update()
163
164
frame.wm_attributes('-alpha', 0)
165
166
frame.update_idletasks()
167
frame.update()
168
#Main processing
169
170
def main():
171
#Prepare variables for processing
172
save_Loc = str(inpSaveLoc.get("1.0", "end-1c"))
173
doc = inplabBookName.get("1.0", "end-1c")
174
num_pages = int(inpPageNo.get("1.0", "end-1c"))
175
waitTime = int(inpWait.get("1.0", "end-1c"))
176
varInpMoY = int(inpmoy.get("1.0",'end-1c'))
177
varInpMoX = int(inpmox.get("1.0",'end-1c'))
178
179
inpmoy.pack_forget()
180
imgs = []
181
182
inpmox.insert(tk.END, "processing...")
183
184
#Fix all inputs
185
save_Loc = save_Loc.rstrip('\')
186
187
if not os.path.exists(save_Loc):
188
os.makedirs(save_Loc)
189
190
os.makedirs(save_Loc + "/" + doc)
191
pyautogui.moveTo(varInpMoX, varInpMoY) #so that cursor doesn't come in the way
192
193
for pg_num in range(num_pages):
194
print('On page', str(pg_num + 1))
195
im = ImageGrab.grab(bbox=(frame.winfo_rootx(), frame.winfo_rooty(), frame.winfo_width(), frame.winfo_height()))
196
finSavLoc = save_Loc + "/" + doc + "/" + str(pg_num + 1) + ".png"
197
im.save(finSavLoc)
198
imgs.append(finSavLoc)
199
time.sleep(waitTime)
200
201
#Flip page
202
pyautogui.moveTo(varInpMoX, varInpMoY)
203
pyautogui.click()
204
205
print("Combining PDF")
206
inpmox.insert(tk.END, "Combinig PDF...")
207
with open(save_Loc + "/" + doc + "/" + doc +".pdf" ,"wb") as f:
208
f.write(img2pdf.convert(imgs))
209
210
if __name__ == "__main__":
211
main()
212
I have tried:
- Rebooting
- Re-installing all the required pip modules
Advertisement
Answer
Turn out the auto-clicker was the problem with PowerShell. Whenever it used to click on the PowerShell command line, it paused. Fixed using this answer: https://serverfault.com/a/205898