Skip to content

How to set alt text on image using figure caption? #245

@bitscompagnie

Description

@bitscompagnie

Hello,

How to use the figure caption text from the source Ms Word document as alt text on the generated tag?

In the source document.xml I see the following:

 <w:drawing>
                    <wp:inline distT="0" distB="0" distL="0" distR="0">
                        <wp:extent cx="5943600" cy="8210550"/>
                        <wp:effectExtent l="0" t="0" r="0" b="0"/>
                        <wp:docPr id="16" name="Picture 16"
                            descr="Image description."
                            title="Image description"/>
                        <wp:cNvGraphicFramePr>
.......

I need to grab the value of the descr="Image description" or title="Image description" from the above excerpt of the source document.xml

I am currently using below custom image export function, which allows me to save my images to a local folder:

[]( def get_image_tag(self, image, width=None, height=None, rotate=None):
# unique_filename = str(uuid.uuid4())

    img_src = self.get_image_source(image)
    if img_src:
        # Getting images from the source
        attrs = {
            'src': img_src
        }
        # get base64 file extension from bytes
        # https://matthewdaly.co.uk/blog/2015/07/04/handling-images-as-base64-strings-with-django-rest-framework/

        format, img_src2 = img_src.split(';base64,') # format ~= data:image/X,
        ext = format.split('/')[-1] # guess file extension
        # Capture the generated filename with the proper extension to use in img source attribute
        img_src_new = 'img_' + image_name() + '.' + ext
        # Function to convert base64 string to image using urlretireve
        urlretrieve(img_src, 'c:/git/output/' + img_src_new)

        # Set the image source to the newly created filename
        attrs = {
            'src': img_src_new
        }
    if width and height:
        attrs['width'] = width
        attrs['height'] = height
    if rotate:
        attrs['style'] = 'transform: rotate(%sdeg);' % rotate
    return HtmlTag('img', allow_self_closing=True, allow_whitespace=True, **attrs))

Thanks for your help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions