Skip to content

Document.add_picture Fails on Subsequent Pictures #4

@stevecohen42

Description

@stevecohen42

When inserting pictures into a document, the first picture works just fine, as shown by the example, etc. This problem arises when you go to add a second picture. The second picture addition triggers the sha1 function in the ImagePart class in docx/parts/image.py. This function is currently:

    @property
    def sha1(self):
        """
        SHA1 hash digest of the blob of this image part.
        """
        raise NotImplementedError

Which is clearly not very helpful. The fix is quite simple, just add the same sha1 functionality from the Image class in the same file. The resulting routine is:

    @property
    def sha1(self):
        """
        SHA1 hash digest of the blob of this image part.
        """
        return hashlib.sha1(self.blob).hexdigest()

Note that this should also probably be a lazyproperty instead of property, but either will work.

I experienced this error under Python 3.3.0 and 3.3.3, but it seems it will happen under any version.

Regards,

Steve

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions