Forums

Save excel file in scheduled task

Hi, I'm trying to create an excel file in a scheduled task. Before I did this in views.py, where I created the workbook as follows. Then it would save in the same 'myapp' folder as where views.py was located.

workbook = xlsxwriter.Workbook('myapp/filename'+task.id+'.xlsx')
worksheet = workbook.add_worksheet()

How should I define the path in a scheduled task if I want to save an excel file in a folder? Such that I can also use the task.id field from my model?

Thanks in advance! Regards, Evelien

Check out the "Make sure you take account of the working directory" section on the scheduled tasks help page -- this has example code explaining how you can get a path relative to the currently running code file.

Hi Giles, Thanks for your reply! I think we set the working directory correctly. Because in our scheduled task we are able to read excel files from a folder. We only can't manage to write an excel file to that same folder. We don't get any error, but there just doesn't appear any excel file.

If you don't get any errors, it sounds like it is writing to a different location that you are not checking. Just to debug how about you try using a simple full path (eg: /home/ziekenhuisroosters/myapp/abc.xlsx') to see if that works.

Thanks for your help! It turns out that we were looking for the answer in the wrong direction. We were missing the workbook.close() statement, so the file didn't get saved at all.