Skip to content

Commit

Permalink
kills all processes of word now
Browse files Browse the repository at this point in the history
  • Loading branch information
Buchhems committed Feb 25, 2023
1 parent 19c5cdc commit 9c7f833
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions PDFTools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import psutil
from tkinter import (BOTTOM, Button, Label, PhotoImage, Tk, Toplevel,
filedialog, messagebox)

Expand All @@ -17,7 +18,7 @@ def docx_to_pdf(docx_filename, pdf_filename):
doc = word.Documents.Open(f'"{docx_filename}"')

except comtypes.COMError as e:
messagebox.showerror(title = "Fehler", message = e)
messagebox.showerror(title = "Fehler", message = "Die Datei " + docx_filename + " kann nicht geöffnet werden.")
word.Quit()
return 0

Expand All @@ -27,7 +28,7 @@ def docx_to_pdf(docx_filename, pdf_filename):
doc.SaveAs(pdf_filename, FileFormat=17)

except comtypes.COMError as e:
messagebox.showerror(title = "Fehler", message = e)
messagebox.showerror(title = "Fehler", message = "Die Datei " + pdf_filename + " kann nicht angelegt werden.")
doc.Close()
word.Quit()
return 0
Expand All @@ -45,7 +46,11 @@ def select_docx_files(convert_button):
convert_button.config(state="disabled", text="...einen Moment bitte...")

# close all instances of word to not create a mess...
messagebox.showwarning(title = "Bitte alle Word-Fenster schließen!", message = "Bitte schließen Sie alle Word-Fenster\nund klicken Sie dann auf OK.\nDies ist für einen sauberen Ablauf notwendig.")
messagebox.showwarning(title = "Word-Fenster schließen!",
message = "Schließen Sie alle Word-Fenster\nund klicken Sie dann auf OK.")

# kill all word instances (user has been warned)
killallword()

# Open a file selection dialog and get the selected files.
docx_filenames = filedialog.askopenfilenames(title='Word-Dateien zur Erzeugung von PDF auswählen', filetypes=[('Word Dokumente', '*.docx')])
Expand All @@ -67,9 +72,9 @@ def select_docx_files(convert_button):
window.update()

if pdfcount > 0:
show_temp_message('Erledigt', 'Es wurden insgesamt ' + str(pdfcount) + ' PDFs erzeugt.')
#enable the button again to create another batch of PDF files.
convert_button.config(state="active", text ="PDF aus DOCX erzeugen")
show_temp_message('Erledigt', 'Es wurde(n)\n' + str(pdfcount) + ' PDF erzeugt.')

revert_button_text()

def remove_metadata(meta_button):
pdfcount = 0
Expand Down Expand Up @@ -128,25 +133,48 @@ def remove_metadata(meta_button):

os.remove(temp_name)


# Reset button to original text
meta_button.config(state="active", text = "Metadaten aus PDF entfernen")
revert_button_text()

if pdfcount >0:
show_temp_message("erledigt...", "Die Metadaten der " + str(pdfcount) + " PDFs wurden entfernt.")
show_temp_message("erledigt...", "Die Metadaten von\n" + str(pdfcount) + " PDF wurden entfernt.")

def show_temp_message(title, message, seconds=5):
# Create a new top-level window for the message.
window = Toplevel()
window.overrideredirect(True)
#window.geometry("300x200")
window.title(title)


# Create a label for the message.
label = Label(window, text=message, font=("Helvetica", 12))
label = Label(window, text=message, font=("Helvetica", 50))
label.pack()

# Close the window after a certain number of seconds.
window.after_idle(lambda: window.after(seconds * 1000, window.destroy))

def revert_button_text():
# Reset button to original text
meta_button.config(state="active", text = "Metadaten aus PDF entfernen")
#enable the button again to create another batch of PDF files.
convert_button.config(state="active", text ="PDF aus DOCX erzeugen")

def killallword():
# Iterate over all running processes
for proc in psutil.process_iter():
try:
# Get process details as a named tuple
process_info = proc.as_dict(attrs=['pid', 'name'])
process_name = process_info['name'].lower()

# Check if the process is Microsoft Word
if 'winword.exe' == process_name:
# Terminate the process
process = psutil.Process(process_info['pid'])
process.terminate()
#time.sleep(2)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
Expand All @@ -164,7 +192,7 @@ def resource_path(relative_path):
window.iconbitmap(resource_path("PDFTools.ico"))

# Set the window title
window.title("PDF-Tools v1.3 (buc @ hems.de)")
window.title("PDF-Tools v1.4 (buc @ hems.de)")

# Set the window size
window.geometry("560x240")
Expand All @@ -178,11 +206,11 @@ def resource_path(relative_path):
label1.place(x=0, y= 0)

# Add a button to start cleaning the PDFs
meta_button = Button(text="Metadaten aus PDFs entfernen", command=lambda: remove_metadata(meta_button), font=("Helvetica", 14))
meta_button = Button(text="Metadaten aus PDF entfernen", command=lambda: remove_metadata(meta_button), font=("Helvetica", 14))
meta_button.pack(side=BOTTOM, pady=10)

# Add a button to start converting the docx
convert_button = Button(text ="PDFs aus DOCX erzeugen", command=lambda: select_docx_files(convert_button), font=("Helvetica", 14))
convert_button = Button(text ="PDF aus DOCX erzeugen", command=lambda: select_docx_files(convert_button), font=("Helvetica", 14))
convert_button.pack(side=BOTTOM)

# Run the Tkinter event loop
Expand Down

0 comments on commit 9c7f833

Please sign in to comment.