AttributeError: module ‘magic’ has no attribute ‘from_buffer’

We would like to add png, jpeg and eps images, where exactly do these image types need to be added?
(actually, the eps have turned into x-vclgraphic)
That is, there is not only a single company logo, but also other images such as certifications that need to be displayed on externally communicated documents.

[edited]
seems relatorio has restructured and no longer uses EXTENSIONS as did when the earlier posts were made.
That said, I don’t understand why it does not simply copy any images in the template document even if not overridden programmatically.

For example, the template has the following snippet:


</draw:frame><draw:frame draw:style-name=“fr2” draw:name=“Image1” text:anchor-type=“paragraph” svg:x=“0.25cm” svg:y=“-1.06cm” svg:width=“3cm” svg:height=“2.551cm” draw:z-index=“7”><draw:image loext:mime-type=“image/jpeg”>
office:binary-data

the report generation using this template fails with:

AttributeError: module ‘magic’ has no attribute ‘from_buffer’

How to get this to work, being single company, we don’t care to override the images programatically anyway.

No response?

We are unable to deploy Tryton generated documents to third parties if we can’t meet company identity needs (this means request for proposals, purchase orders, quotes and invoices).

Is there an alternative solution to relatorio? (though it seems at first glance not a relatorio problem since the examples show the logos passing though just fine!)

Well, this seems to work for me…

> diff -r e1b70835bc9e relatorio/templates/opendocument.py
> --- a/relatorio/templates/opendocument.py       Mon Jul 29 16:12:32 2019 +0200
> +++ b/relatorio/templates/opendocument.py       Fri Oct 11 11:49:40 2019 +0200
> @@ -950,7 +950,10 @@
>          binary_data, = image.xpath(
>              './office:binary-data', namespaces=namespaces)
>          data = base64.b64decode(binary_data.text)
> -        mime_type = magic.from_buffer(data, mime=True)
> +        try:
> +            mime_type = image.attrib['{%s}mime-type' % namespaces['loext']]
> +        except KeyError:
> +            mime_type = magic.from_buffer(data, mime=True)
>          name = 'Pictures/image%s%s' % (
>              i, mimetypes.guess_extension(mime_type))
>          image.remove(binary_data)

I put in the try/except just in case.

You are not using the correct python-magic module.
This module is only required when using Flat OpenDocument as template source instead of OpenDocument.

We can not include such code because ‘loext’ is not a standard attribute of <draw:image>. As defined by the standard, the format of the <office:binary-data> must be analyzed:

The image data may be in an arbitrary data format. Consumers have to analyze the image data to determine the data format that is used.

I’m using the standard ArchLinux community package python-magic

$ pacman -Ss python-magic
community/python-magic 5.37-1 [installé]
    Python bindings to the magic library

I see that there is in AUR python-magic-ahupp 0.4.15-1
I’ll give it a try but we try to use mostly approved, standard modules for the most part…
BTW - I have not come across this specific reference in relatorio.

The dependency is clearly stated in the setup.py

I made a patch to fallback to file-magic API if python-magic API is not detected: Bug 53: Support file-magic - Relatorio tracker

The dependency is ‘python-magic’.
I have the standard platform ‘python-magic’ installed, which is not GitHub - ahupp/python-magic: A python wrapper for libmagic
According to the site (Ian Darwin's Fine Free File Command):

Who’s using it?

Most known BSD distributions (FreeBSD, NetBSD, Darwin/Mac OS X, etc)
Every known Linux distribution

If they work differently, then perhaps a bug should be filed.

No this is not the standard package named python-magic that you have. It is the package named file-magic. It is not our fault if your distribution does not follow the official python package naming.

This seems to work for me… thanks.

BTW… already with .fodt (flat OpenDocument XML) it appears that one is already using a OOO/LO extension… seems extremely odd to not utilise its extension if available to recuperate the associated ‘manifest:media-type’ for the file. FODT file extension - File-Extensions.org

Wrong the Flat OpenDocument format use the standardized format. It is just a different representation that is not supported by all software: OpenDocument technical specification - Wikipedia

Regardless, ODF 1.3 will supposedly have it
https://wiki.documentfoundation.org/Development/ODF_Implementer_Notes/List_of_LibreOffice_ODF_Extensions (search for mime-type or core commit 4469b29f)
https://issues.oasis-open.org/browse/OFFICE-3943

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.