0

I have been using xlwings to edit some excel files in python, but i am having trouble for one specific file. After I am done editing it, I pass a command for python to save and close that workbook, but it closes the file before it´s done saving, I have used the following command to remediate the issue: time.sleep(2), but I understand that this is not very efficient, nor is it dinamic, and many times, even with that, the file closes before saving. I now that I could increase the seconds in the sleep command, but I would much rather prefer some code that waits for the save to finish for it to close.

I have looked around this website and many others and could´nt find an answer, people only mention ways for python to wait for some calculation to be complete in order for it to get some value, but they don´t mention wating for the save to close the file. My code is structured as follows:

import xlwings as xw
import time

app = xw.App()

excel = xw.Book("file path")
excel_sheet = excel.sheets("sheet name")

##This is just an example of changing something in the file
excel_sheet.range("A1").value = 123

excel.save()
time.sleep(2)
excel.close()
app.quit

Update: I have now tried using a context manager, but without success, here is my code:

import xlwings as xw
import time

app = xw.App()

with xw.Book("file path") as excel: 
  excel_sheet = excel.sheets("sheet name")

  ##This is just an example of changing something in the file
  excel_sheet.range("A1").value = 123

  excel.save()

app.quit
joao277
  • 1
  • 2
  • Have you tried using a Context Manager? – moken Jul 21 '23 at 21:29
  • I am trying this right now, but I can´t be sure that it wil always work. Only if it appears to be consistent throughout a long period of time, can I infer that it works. But thanks for the suggestion. – joao277 Jul 25 '23 at 16:03
  • It failed, unfortunately – joao277 Jul 25 '23 at 20:20
  • What's your code for the context manager? – moken Jul 26 '23 at 13:44
  • It goes like this: ´´´ import xlwings as xw; import time; app = xw.App(); with xw.Book("file path") as excel:; -excel_sheet = excel.sheets("sheet name"); -; -##This is just an example of changing something in the file; -excel_sheet.range("A1").value = 123; -excel.save(); app.quit; ´´´ I was having trouble formatting the code, so I used "-" to represent indentation and ";" to represent paragraphs – joao277 Jul 26 '23 at 16:45
  • It's OK/recommended to edit your question to add updates like the above. As you see comments doesn't have much formatting for code. So I would not be using app.quit in the code not sure that it has any impact but shouldn't need it as the context manage should/does clean up after. My thoughts on the CM format is like [this answer from mouwsy](https://stackoverflow.com/a/66350293/13664137) , just save then let the CM close. I don't think the `wb.close` is even necessary. Not sure it will make any difference for you though. Maybe you can ask some question on the Xlwings github, might help. – moken Jul 27 '23 at 11:07
  • Ok, I will ask on their git hub. Now, regarding the `app.quit`, it is needed because I initialize an exclusive excel app for xlwings, different than the one I am using for my self, and if I don´t close it it will keep on running, I did not use `wb.close` on my new code as the context maneger already does that for me. – joao277 Jul 27 '23 at 11:56

0 Answers0