0

I wknow it's possible to tell a python to run each minute for example. But I have a more specific question.

It is possible to run a script each minute, and create an Excel, and let open this Excel file and update the output inside ?

Because each time I run a code where some files are created, I need to close the file in order to create it again with Python.

EDIT : In other words : I would like a refreshed view on the file, each time the script run, without closing the file (Excel for example). The Excl file stay open and the script update the output every now and then.

RandallCloud
  • 123
  • 9
  • 1
    Yes it is possible. If you know how to have a script run each minute, and you know how to open and close a file, then you can write a script to open the file, perform your operations and close the file; then have that script run each minute :) – Jack Whelan Nov 15 '21 at 10:36
  • I guess the real question here is: How to update/refresh an _opened_ Excel sheet from within a Python script? Do you just want a permanently refreshed view on the file, or do you also want to simultaneously work on that file (without your work being interrupted or lost, obviously)? – tobias_k Nov 15 '21 at 10:41
  • 2
    use [`cron`](https://en.wikipedia.org/wiki/Cron) or some other system scheduler to run your script ([open Excel file](https://stackoverflow.com/questions/3239207/how-can-i-open-an-excel-file-in-python), modify, close file) every minute. – alex Nov 15 '21 at 10:43
  • 1
    Yes i would like a refreshed view on the file, each time the script run, without closing the file – RandallCloud Nov 15 '21 at 10:46
  • @alex : Is `cron' update the file without closing it ? It is possible ? – RandallCloud Nov 15 '21 at 11:00
  • Because it's my first question, I cannot put all the context in one go.. Sorry if I did wrong – RandallCloud Nov 15 '21 at 12:34
  • 1
    This might be an XY problem (fixation on one possible "solution" to a problem if there's actually a better way to do it). What exactly are you trying to _achieve_ here? Does the file actually have to be opened in Excel, or do you just want to view the updated values in _any_ way? – tobias_k Nov 15 '21 at 12:43
  • 1
    I edited my post. I hope it make more sense now :) – RandallCloud Nov 15 '21 at 12:48
  • I feel like you just added some words, which make the question longer but do not clarify the issue at hand any more than before. Please read [How to ask](https://stackoverflow.com/help/how-to-ask). Posting a [Minimal, Reproducible example (or MCVE)](https://stackoverflow.com/help/minimal-reproducible-example) that demonstrates your problem would help you get better answers. – alex Nov 15 '21 at 12:53
  • Ok ok I delete this one and redo, sorry for the hassle – RandallCloud Nov 15 '21 at 12:57
  • Let me rephrase my question: Does it _have_ to be Excel, or could you, e.g., also write a minimal read-only UI, or even just print the tabular data on the terminal in regular intervals? The problem I see: While there is a library for reading and manipulating Excel files, there is (to the best of my knowledge) no API to Excel (the program) itself, to e.g. re-load an opened file. (Opening the same file with Excel again _without_ closing/killing Excel first _might_ have the same effect, I haven't tried. Might also depend on the OS.) – tobias_k Nov 15 '21 at 13:14
  • It's for checking fluctuation, so we need graph, maybe xcel isn't mandatory. It's also possible to tell the script to close the file and re open it I guess. Not so clear, but effective . – RandallCloud Nov 15 '21 at 13:47
  • Maybe you should try something like [`plotly`](https://plotly.com/python/) then. – tobias_k Nov 15 '21 at 13:50
  • It could work ? I could schedule an script to run all hour (for example) and output/update a graph ? – RandallCloud Nov 15 '21 at 13:51

1 Answers1

0

You could keep the file open with a while loop and even sleep for a some time if you don't want the loop to run continuously, instead of running the python script again and again.

while True:
    # do some work
     sleep(60) # sleep 60 seconds

You need to make sure that there is a way to exit and you handle any errors so you can gracefully close the excel file