TypeError when opening file with Read-Only Recommended bit set.

Issue #734 resolved
lengau
created an issue

In 2.4.x of openpyxl, I get a TypeError when trying to open an Excel file with the "Read-only Recommended" option set.

This is not the case in the 2.3.x series.

The following is from 2.4.0, but the same occurs with 2.4.1.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-3b5a028bcc7f> in <module>()
      1 import openpyxl
----> 2 openpyxl.load_workbook('Book1.xlsx')

/home/alowe/anaconda3/envs/test/lib/python3.5/site-packages/openpyxl/reader/excel.py in load_workbook(filename, read_only, keep_vba, data_only, guess_types, keep_links)
    159     wb._keep_links = keep_links
    160     wb.guess_types = guess_types
--> 161     parser.parse()
    162     wb._sheets = []
    163 

/home/alowe/anaconda3/envs/test/lib/python3.5/site-packages/openpyxl/packaging/workbook.py in parse(self)
     39         src = self.archive.read(ARC_WORKBOOK)
     40         node = fromstring(src)
---> 41         package = WorkbookPackage.from_tree(node)
     42         if package.properties.date1904:
     43             self.wb.excel_base_date = CALENDAR_MAC_1904

/home/alowe/anaconda3/envs/test/lib/python3.5/site-packages/openpyxl/descriptors/serialisable.py in from_tree(cls, node)
     74                 if hasattr(desc.expected_type, "from_tree"):
     75                     #complex type
---> 76                     obj = desc.expected_type.from_tree(el)
     77                 else:
     78                     #primitive

/home/alowe/anaconda3/envs/test/lib/python3.5/site-packages/openpyxl/descriptors/serialisable.py in from_tree(cls, node)
     87                 attrib[tag] = obj
     88 
---> 89         return cls(**attrib)
     90 
     91 

/home/alowe/anaconda3/envs/test/lib/python3.5/site-packages/openpyxl/workbook/protection.py in __init__(self, readOnlyRecommended, userName, reservationPassword, algorithmName, hashValue, saltValue, spinCount)
    106                 ):
    107         self.readOnlyRecommended = readOnlyRecommended
--> 108         self.userName = userName
    109         self.reservationPassword = reservationPassword
    110         self.algorithmName = algorithmName

/home/alowe/anaconda3/envs/test/lib/python3.5/site-packages/openpyxl/descriptors/base.py in __set__(self, instance, value)
     42             if (not self.allow_none
     43                 or (self.allow_none and value is not None)):
---> 44                 raise TypeError('expected ' + str(self.expected_type))
     45         super(Typed, self).__set__(instance, value)
     46 

TypeError: expected <class 'str'>

Comments (2)

  1. CharlieC

    Thanks for the report. openpyxl 2.4 has much better support for some of the more esoteric workbook attributes including fileSharing setting. According to the spec this has a userName attribute which shouldn't be empty. But obviously it can...

    The following diff fixes this:

    diff -r 6f25a7143557 openpyxl/workbook/protection.py
    --- a/openpyxl/workbook/protection.py   Tue Nov 29 11:49:58 2016 +0100
    +++ b/openpyxl/workbook/protection.py   Wed Nov 30 21:13:07 2016 +0100
    @@ -88,7 +88,7 @@
         tagname = "fileSharing"
    
         readOnlyRecommended = Bool(allow_none=True)
    -    userName = String()
    +    userName = String(allow_none=True)
         reservationPassword = HexBinary(allow_none=True)
         algorithmName = String(allow_none=True)
         hashValue = HexBinary(allow_none=True)
    
  2. Log in to comment