Search code examples
pythongoogle-docs-api

How to center inline images using Google Docs API v1 and Python


I'm trying to center an image using Google Docs API and Python, i followed the documention which suggests to define an inline image. The image is shown in the generated document but is not in the center of the document. Is there any way to center the image? Here is my code:

        requests = [
    {
        'replaceAllText': {
            'containsText': {
                'text': '{{ customer.name }}',
                'matchCase': 'true'
            },
            'replaceText': context['customer'],
        }},
    {
        'insertInlineImage': {
            'location': {
                'index': 55
            },
            'uri':
                'https://www.gstatic.com/images/branding/product/1x/docs_64dp.png',
            'objectSize': {
                'height': {
                    'magnitude': 50,
                    'unit': 'PT'
                },
                'width': {
                    'magnitude': 50,
                    'unit': 'PT'
                }
            }
        }
    }
]
risk_assessment_customer = drive_service.files().copy(fileId=DOCUMENT_ID, body={}).execute()
result = service.documents().batchUpdate(
    documentId=risk_assessment_customer['id'], body={'requests': requests}).execute()
request = drive_service.files().export_media(fileId=result['documentId'],
                                             mimeType='application/pdf')

Thank you


Solution

    • You want to insert an inline image to the center of Document using Google Docs API.
    • You have already been used Google Docs API with Google Client Library for Python.

    If my understanding is correct, how about this modification? In this modification, I modified your request body.

    Modifiaction point:

    • In order to align the image to the center, updateParagraphStyle is used.

    Modified request body:

    Please replace the request body as follows.

    requests = [
        {
            'replaceAllText': {
                'containsText': {
                    'text': '{{ customer.name }}',
                    'matchCase': 'true'
                },
                'replaceText': context['customer'],
            }
        },
        {
            "insertInlineImage": {
                "location": {
                    "index": 55
                },
                "uri": "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
                "objectSize": {
                    "height": {
                        "magnitude": 50,
                        "unit": "PT"
                    },
                    "width": {
                        "magnitude": 50,
                        "unit": "PT"
                    }
                }
            }
        },
        {
            "updateParagraphStyle": {
                "range": {
                    "startIndex": 55,
                    "endIndex": 56
                },
                "paragraphStyle": {
                    "alignment": "CENTER"
                },
                "fields": "alignment"
            }
        }
    ]
    

    Reference:

    If this was not the result you want, I apologize.