Search code examples
pythonxlrddjango-file-upload

Python/Django :How to open file in xlrd using file object.


Background:

I have uploaded a xls file using the FileField. Now I want to parse this file using the xlrd tool.

While doing so I am doing the following in the view.

if form.is_valid():
            user_file = form.save()
            user_file.save()             
            workbook = xlrd.open_workbook(user_file.file.name)//( user_file.file.name returns relative path to the media_root folder. )
            sheet = workbook.sheet_by_index(0)

This gives an error as xlrd is unable to find the file.(As the path is not absolute). How can I do this ?


Solution

  • os.path.join(settings.MEDIA_ROOT, user_file.file.name)
    

    Should give the correct path.