skip to Main Content

I want mimetypes library to return mimetype from file name. For example:

>>> mimetypes.types_map['.docx']
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'

But, currently I got an exception:

>>> mimetypes.types_map['.docx']
Traceback (most recent call last):
  File "<console>", line 1, in <module>
KeyError: '.docx'

Also I tried guess_type func:

>>> mimetypes.guess_type('.docx')
(None, None)

Full code:

import mimetypes
mimetypes.init()
mimetypes.types_map['.docx']  # throws an Exception
mimetypes.guess_type('.docx')  # returns (None, None)

Python: 3.8.10

Windows 10 build ver. 19044.2006

On my system I got installed Ubuntu WSL 2. When executing my Full code there: no exceptions are raised and mimetype returns as excepted (also python 3.8.10)

2

Answers


  1. Chosen as BEST ANSWER

    Repair of Office package via Control Panel updated regedit and added missing ContentType records.

    I chose offline mode during the repair.


  2. You need to pass a "name" before dot.

    >>> mimetypes.guess_type('.docx')
    (None, None)
    >>> mimetypes.guess_type('filename.docx')
    ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', None)
    

    Docs: https://docs.python.org/3/library/mimetypes.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search