Search code examples
pythongoogle-docs-api

How can I create a document with horizontal page orientation using google doc api


How can I create a document with horizontal page orientation (landscape) using google doc api (in python if possible)


Solution

  • This one was hard. For future generations:

    body = {
        'title': 'Doc1'
    }
    
    doc = service.documents() \
        .create(body=body).execute()
    
    file_id = doc.get('documentId')
    requests =[
        {'updateDocumentStyle':{
            "documentStyle":{
                'pageSize':{
                      "height": {
                            'magnitude': 8.27,
                            'unit': 'PT'
                        },
                      "width": {
                            'magnitude': 11.69,
                            'unit': 'PT'
                        }
                    }},
            "fields": 'pageSize'
            
        }}]
    result = service.documents().batchUpdate(
        documentId=file_id, body={'requests': requests}).execute()