the code gets stuck after I select the word file

61 Views Asked by At

So basically I'm working on an assignment which is to make a Python program that opens links from a Word or a Notepad file in such a way that 15 links are opened first for 10mins and after 10mins it closes the previous tabs and opens the next 15 links in the queue and when the last patch of links comes and the number of links is less than 15 for eg only 9 links are remaining it should play all the remaining ones and each link should be opened in a different tab

import docx
import subprocess
import time
from tkinter import filedialog
from tkinter import Tk

def read_links_from_word(file_path):
    document = docx.Document(file_path)
    links = []

    for paragraph in document.paragraphs:
        for run in paragraph.runs:
            for hyperlink in run.hyperlinks:
                links.append(hyperlink.address)
    
    return links

def open_links_in_batches(links, batch_size, interval):
    for i in range(0, len(links), batch_size):
        batch = links[i:i + batch_size]
        open_links(batch)
        time.sleep(interval)

def open_links(links):
    for link in links:
        subprocess.run(["start", "", link], shell=True)

def choose_word_file():
    Tk().withdraw()  # Avoid the root window from appearing
    file_path = filedialog.askopenfilename(title="Select Word File", 
    filetypes=[("Word files", "*.docx")])
    return file_path

if __name__ == "__main__":
    word_file_path = choose_word_file()

    if not word_file_path:
        print("No file selected. Exiting.")
    else:
        links = read_links_from_word(word_file_path)
        
        batch_size = 15
        interval_between_batches = 600  # 10 minutes in seconds
        
        open_links_in_batches(links, batch_size, interval_between_batches)
0

There are 0 best solutions below