mime type macro error with an xslm file

Issue #691 resolved
ptitjano
created an issue

Hi, The following code produces an error with both version 2.3.5 (from archlinux package) or 2.4 from pip using python 2.7 or 3.5:

wb = load_workbook('Analyse des maitrise.xlsm', keep_vba=True)
ws = wb.get_sheet_by_name('chefs')
ws.cell(column=1, row=1, value=3.14)
wb.save('new_document.xlsm')
wb.save('new_document.xlsm')
  File "/usr/lib/python2.7/site-packages/openpyxl/workbook/workbook.py", line 339, in save
    save_workbook(self, filename)
  File "/usr/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 268, in save_workbook
    writer.save(filename)
  File "/usr/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 250, in save
    self.write_data()
  File "/usr/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 98, in write_data
    self.manifest._write(archive, self.workbook)
  File "/usr/lib/python2.7/site-packages/openpyxl/packaging/manifest.py", line 167, in _write
    archive.writestr(self.path, tostring(self.to_tree()))
  File "/usr/lib/python2.7/site-packages/openpyxl/packaging/manifest.py", line 124, in to_tree
    for ext, mime in self.extensions:
  File "/usr/lib/python2.7/site-packages/openpyxl/packaging/manifest.py", line 116, in extensions
    return [(ext[1:], mimetypes.types_map[ext]) for ext in sorted(exts)]
KeyError: ''

Comments (3)

  1. Charlie Clark

    So, the problem is related to LibreOffice using an Override for the "rels" type as opposed using a Default. This will also cause Excel to say that the file is broken. But it is allowed by the specification.

    § 10.1.2.2

    For all parts of the package other than relationships parts (§9.3.1) and the Content Types part itself, the Content Types stream shall specify either:

    • One matching Default element, or
    • One matching Override element, or
    • Both a matching Default element and a matching Override element, in which case the Override element takes precedence.
  2. Mickael Guerin

    In your changeset, you consider the extension of PartName "/_rels/.rels" is empty, and you filter empty extensions. Could we consider the ext should be ".rels" (as it is for "/xl/_rels/workbook.xml.rels") ?

    I had the same issue on my files and my fix was to replace os.path.splitext in order to handle ".rels" filename.

  3. Log in to comment