0

I am writing a script where I am unable to close one of the excel files open in Windows. There are many open files in Microsoft Excel but I have not opened them as f.open() in python. Before the script completes it has to write to same excel file and if file is open error and script breaks.

Can I close the one of the multiple MS- Excel Window with specific title like file1, file2, file3 are open in excel and I only want to close file2.xlsx

import os
file3= output.xlsx
os.close(file3)
writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
Abhinav Kumar
  • 177
  • 2
  • 5
  • 22

1 Answers1

0

I'm not sure there's a way to do it exactly with window titles, I think the better way is to use system information about what files does each process use

from psutil import Process
from os import kill
from signal import SIGQUIT
def close_one_excel(filename, subprocesses):
    """ Kills the process from `subprocesses` array,
    that uses the `filename` file """
    for i in subprocesses:
        p = Process(i.pid)
        if filename in p.open_files():
            kill(p.pid, SIGQUIT)
Kolay.Ne
  • 1,345
  • 1
  • 8
  • 23