XlsxWriter: unable to close(); saves a corrupted workbook

import xlsxwriter

workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write('A1', 'Hello world')

workbook.close()

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: ‘C:\Users\DOUGHE~1.ROS\AppData\Local\Temp\tmpnpp39a_c’


I was able to write excel files yesterday; today my day began with this issue and I was not able to resolve.  I was hoping someone here may be of assistance.  

If I run workbook.close() again, I do not receive the error, but the .xlsx file does not open and says it is corrupted.

I'm not sure what happened.  I may have have closed/exited the program incorrectly.  I've read of the importance of using workbook.close(), did I create a 'write to excel ghost' living in the purgatory of my temp folder?  This issue now extends to pandas .to_excel() as well.  I am able to write to csv.   

I've restarted, reinstalled anaconda +packages, deleted all the tmp files in the temp folder, however I still receive this error.  Any help would be greatly appreciated. Thank you.



windows 10
python 2.7, 3.6, 3.7 (I had base install of anaconda py 2.7, with an additional environment of 3.6.  Reinstalled anaconda with python 3.7, 64bit.  Issue happened with all three)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20 (10 by maintainers)

Most upvoted comments

The file doesn’t appear to be open in Excel. At least, excel doesn’t seem to be running in task manager and I’ve not opened it. Here is the trace of the error:

PermissionError                           Traceback (most recent call last)
<ipython-input-1-94860028b04f> in <module>()
      6 worksheet.write('A1', 'Hello world')
      7 
----> 8 workbook.close()

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\xlsxwriter\workbook.py in close(self)
    304         if not self.fileclosed:
    305             self.fileclosed = 1
--> 306             self._store_workbook()
    307 
    308     def set_size(self, width, height):

~\AppData\Local\conda\conda\envs\py36\lib\site-packages\xlsxwriter\workbook.py in _store_workbook(self)
    676 
    677                 xlsx_file.write(os_filename, xml_filename)
--> 678                 os.remove(os_filename)
    679 
    680         xlsx_file.close()

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\DOUGHE~1.ROS\\AppData\\Local\\Temp\\tmpnpp39a_c'