简体   繁体   中英

How can we apply "different first page" for footers in the google docs using google docs API and python

@Tanaike already gave the following solution to put header and footers in a google docs using google docs API.

file_id = ###

def insert_data(file_id):
    requests = []
    header_footer_req = []

    index = 0
    header_footer_req.append(add_header(index))
    header_footer_req.append(add_footer())
    header_footer_res = docs.documents().batchUpdate(documentId=file_id, body={'requests': header_footer_req}).execute()
    header_id = header_footer_res['replies'][0]['createHeader']['headerId']
    footer_id = header_footer_res['replies'][1]['createFooter']['footerId']
    # adding header and footer content

    requests += [
        {
            "insertInlineImage": {
                "location": {
                    "segmentId": header_id,
                    "index": 0
                },
                "uri": "https://drive.google.com/uc?export=view&id=1Nn-G6Y7jUlzYF3MPN_YlQC9Uasjdj33",
                # This is a sample image.
                "objectSize": {
                    "width": {
                        "magnitude": 100,
                        "unit": "PT"
                    }
                }
            }
        },
        {
            "updateParagraphStyle": {
                "paragraphStyle": {
                    "alignment": "END"
                },
                "range": {
                    "segmentId": header_id,
                    "startIndex": 0,
                    "endIndex": 1
                },
                "fields": "alignment"
            }
        }
    ]

    # Add footer content.
    text = "This is my footer\nxyz"
    requests += [
        {
            "insertText": {
                "location": {
                    "segmentId": footer_id,
                    "index": 0
                },
                "text": text
            }
        },
        {
            "updateParagraphStyle": {
                "paragraphStyle": {
                    "alignment": "END"
                },
                "range": {
                    "segmentId": footer_id,
                    "startIndex": 0,
                    "endIndex": len(text)
                },
                "fields": "alignment"
            }
        }
    ]
    docs.documents().batchUpdate(documentId=file_id, body={'requests': requests}).execute()

def add_header(index):
    header = {
        "createHeader": {
            "sectionBreakLocation": {
                "index": index
            },
            "type": "DEFAULT"
        }
    }
    return header

def add_footer():
    footer = {
        "createFooter": {
            "type": "DEFAULT"
        }
    }
    return footer
  1. The above code prints the same footer on every page. How can I have different footer on first page and other pages.

First page footer: Disclaimer: This is my test footer. Other pages footer: @copyright reserved\nxyz.com

  1. How can I have page number as auto incremented on the right hand side of footer.

  2. When I add an image in the header, the header size is increased, how can I make header size static on all the pages?

Question 1:

Q1: The above code prints the same footer on every page. How can I have different footer on first page and other pages.

Answer:

In the current stage, when a new Google Document is created and the header and footer are separated as the different first page using Docs API, unfortunately, firstPageHeaderId and firstPageFooterId are not created. In this case, the text and image cannot be put to the 1st-page header and footer using Docs API. I think that this might be due to only one header and footer type of DEFAULT . On the other hand, the header and footer except for 1st page can be managed by Docs API.

When you want to set the different first page of header and footer, you can use the following request body.

{
  "updateDocumentStyle": {
    "documentStyle": {
      "useFirstPageHeaderFooter": True
    },
    "fields": "useFirstPageHeaderFooter"
  }
}
  • When this request body is run, the 1st page of the header and footer is separated from other pages. But firstPageHeaderId and firstPageFooterId cannot be created. In the current stage, it seems that when you manually click the header and footer on Google Document, those values are created.
  • When you want to manage the header and footer of another page except for 1st page, please use defaultHeaderId and defaultFooterId as segmentId .
  • True of "useFirstPageHeaderFooter": True is for python.

Question 2:

Q2: How can I have page number as auto incremented on the right hand side of footer.

Answer:

Unfortunately, it seems that in the current stage, this cannot be also achieved by Docs API.

Question 3:

Q3: When I add an image in the header, the header size is increased, how can I make header size static on all the pages?

Answer:

The width and height of the inserted image can be changed. I think that this might be the answer to your 3rd question. The sample request body is as follows.

{
  "insertInlineImage": {
    "uri": "###",
    "location": {
      "segmentId": "###",
      "index": 0
    },
    "objectSize": {
      "height": {
        "magnitude": 100, <--- Here, please set the size.
        "unit": "PT"
      },
      "width": {
        "magnitude": 100, <--- Here, please set the size.
        "unit": "PT"
      }
    }
  }
}

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM