-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPubToPdfConv.py
More file actions
235 lines (185 loc) · 8.99 KB
/
PubToPdfConv.py
File metadata and controls
235 lines (185 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
import threading
import win32com.client
import win32gui
import win32con
import gc
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
# defining global variabls
folderSelected = ""
pubFiles = []
# function for checking folder being picked
def browseFolders():
# globalises to use across multiple functions
global folderSelected
global pubFiles
# resets the list before refilling it
pubFiles = []
# remove all items in the gui
tree.delete(*tree.get_children())
# takes the directory selected and stores it
folderSelected = filedialog.askdirectory()
# checking all files and directories steming from one provided
for root, dirs, files in os.walk(folderSelected):
for file in files:
# if ones found adds it to the list and the gui
if file.lower().endswith(".pub"): # or file.lower().endswith(".pdf"):
pubFiles.append(os.path.join(root, file))
tree.insert("", "end", values=(os.path.join(root, file), "", "", ""))
# if the list isnt empty then activate the convert button
if len(pubFiles) != 0:
convertButtonPb.configure(state="normal")
# convertButtonWd.configure(state="normal")
# if its empty output error message box telling user, also keeps convert button inactive
else:
messagebox.showerror("No files found", "Folder doesnt contain any .pub (publisher) files" + folderSelected)
# function to define perameters for pub to pdf
def toPdf():
thread = threading.Thread(target=PubToPdfAndPdfToDocx, args=("pub", "pdf"))
thread.start()
# function to define perameters for pdf to docx
def toDocx():
thread = threading.Thread(target=PubToPdfAndPdfToDocx("pdf", "docx"))
thread.start()
# updates the attempt col of the files listed
def UpdateCol(index, attempt):
id = tree.get_children()[index]
# depending on number of attempts changes it to match
if (attempt == 1):
tree.item(id, values=(pubFiles[index], "❌", "✅", ""))
elif (attempt == 2):
tree.item(id, values=(pubFiles[index], "❌", "❌", "✅"))
elif (attempt == 3):
tree.item(id, values=(pubFiles[index], "❌", "❌", "❌"))
else:
tree.item(id, values=(pubFiles[index], "✅", "", ""))
root.update_idletasks()
# converter, takes pubs and makes them pdfs or takes pdfs and makes them docx
def PubToPdfAndPdfToDocx(convertFrom, convertTo):
# once called on re-disables the button so it cant be re-clicked until valid directory and files
# convertButtonWd.configure(state="disabled")
convertButtonPb.configure(state="disabled")
browseButton.configure(state="disabled")
# define the list and strings for the failed/error files
ErrConvertedFiles = []
ErrConvertedFilesList = ""
DidError = "All "
# index for tracking on updateCol function
index = -1
# goes through all files and directories to help convert them
for root, dirs, files in os.walk(folderSelected):
for file in files:
if file.lower().endswith(".pub"): # or file.lower().endswith(".pdf"):
# moves index to next (for function mentioned before)
index += 1
if not (file.lower().endswith("." + convertFrom)):
tree.item((tree.get_children()[index]), tags=("NA"))
if file.lower().endswith("." + convertFrom):
# resets attempts for each file
attempt = 0
# depending if it is to pdf or to docx opens the correct win32com client
if convertFrom == "pub":
publisherWord = win32com.client.Dispatch("Publisher.Application")
elif convertFrom == "pdf":
publisherWord = win32com.client.Dispatch("Word.Application")
# gets file path for the input filepath then new file path for output pdf
filePathInp = os.path.normpath(os.path.join(root, file))
filePathOut = os.path.normpath(os.path.join(root, f"{os.path.splitext(file)[0]}." + convertTo))
# while it hasnt failed 4 times
while attempt < 3:
try:
# configers the export formats for saving as pdf
if convertTo == "pdf":
doc = publisherWord.Open(filePathInp)
doc.ExportAsFixedFormat(
Filename=filePathOut,
Format=2,
Intent=1,
IncludeDocumentProperties=True,
BitmapMissingFonts=True
)
# otherwise export formated for saving as docx
elif convertTo == "docx":
doc = publisherWord.Documents.Open(filePathInp)
doc.SaveAs2(
filePathOut,
FileFormat=16
)
# closes the file and clean up
doc.Close()
gc.collect()
# changes row colour and updates on gui with before mentioned function
if (attempt == 0):
tree.item((tree.get_children()[index]), tags=("success"))
UpdateCol(index, attempt)
break
# if error converting
except Exception as e:
# adds 1 to attempts made
attempt += 1
# if 3 failes skips file once retrying 3 times and highlights red (rather than green on sucess)
if attempt == 3:
ErrConvertedFiles.append(filePathInp)
tree.item((tree.get_children()[index]), tags=("failed"))
UpdateCol(index, attempt)
break
else:
tree.item((tree.get_children()[index]), tags=("partFailed"))
UpdateCol(index, attempt)
# if not failed 3 times yet then retries until either works or fails and skips
# if any fully failed (3 tries) then sets up output end message to include them
if (ErrConvertedFiles):
ErrConvertedFilesList = "List of failed file filepaths:"
DidError = "Some "
# makes a string of all the failed files
for item in ErrConvertedFiles:
ErrConvertedFilesList = ErrConvertedFilesList + "\n" + item
browseButton.configure(state="normal")
# changes message from "all" to "Some" and lists the failed files at the end
messagebox.showinfo("Files converted", DidError + "files converted successfully\n\n" + ErrConvertedFilesList)
# used to make window "x" a minimise instead of close it
def minimizeInsteadOfClose():
root.iconify()
# end proram
def Exit():
root.destroy()
exit()
# gui
try:
root = tk.Tk()
root.title("Pub to PDF")
frame = tk.Frame(root, padx=20, pady=20)
frame.pack()
label = tk.Label(frame, text="Step 1: Select folder with .pub files in and pdf copies will be made:")
label.pack()
browseButton = tk.Button(frame, text="Browse", command=browseFolders)
browseButton.pack(pady=(5, 0))
columns = ("File", "Att 1", "Att 2", "Att 3")
tree = ttk.Treeview(frame, columns=columns, show="headings")
for column in columns:
tree.heading(column, text=column)
tree.column(column, width=450 if column == "File" else 50, anchor='w')
tree.pack(fill="both", expand=True, pady=(10, 10))
label = tk.Label(frame, text="\nStep 2: Select files to convert to:")
label.pack()
convertButtonPb = tk.Button(frame, text="Convert to PDF", command=toPdf, state="disabled")
convertButtonPb.pack(pady=(5, 0))
# (for publisher to word make sure to convert to pdf first)
# option can be unhashed if needed, warning the pdf to word format doesnt work well, everything copies over but will mess the page formats
# also remove hash for line 73, 39 and part of 33 for it to work
'''label = tk.Label(frame, text="\nFor publisher to word make sure to convert to pdf first\nOptional step 3: Select files to convert to:")
label.pack()
convertButtonWd = tk.Button(frame, text="Convert to word\n.pdf -> .docx", command=toDocx, state="disabled")
convertButtonWd.pack()'''
ExitButton = tk.Button(frame, text="cancle/end program", command=Exit, foreground="white", background="#d9534f")
ExitButton.pack(side="right")
# colour tags for fails and sucesses
tree.tag_configure("success", background="#ADEA33")
tree.tag_configure("partFailed", background="#EAB333")
tree.tag_configure("failed", background="#E14B2A")
tree.tag_configure("NA", foreground="#B1B1B1")
root.protocol("WM_DELETE_WINDOW", minimizeInsteadOfClose)
root.mainloop()
except Exception:
print("ended")