简体   繁体   English

我们如何使用谷歌文档 API 和 python 为谷歌文档中的页脚应用“不同的首页”

[英]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. @Tanaike 已经给出了以下解决方案,使用谷歌文档 API 将 header 和页脚放入谷歌文档中。

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.第一页页脚: Disclaimer: This is my test footer. Other pages footer: @copyright reserved\nxyz.com其他页面页脚: @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? 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. Q1:上面的代码在每一页上打印相同的页脚。 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.在当前阶段,当创建新的 Google 文档并使用 Docs API 将 header 和页脚分隔为不同的首页时,不幸的是,没有创建firstPageHeaderIdfirstPageFooterId In this case, the text and image cannot be put to the 1st-page header and footer using Docs API.在这种情况下,文本和图像无法使用 Docs API 放入第一页 header 和页脚。 I think that this might be due to only one header and footer type of DEFAULT .我认为这可能是由于只有一个 header 和页脚类型为DEFAULT On the other hand, the header and footer except for 1st page can be managed by Docs API.另一方面,除第一页外的 header 和页脚可由 Docs API 管理。

When you want to set the different first page of header and footer, you can use the following request body.当您想设置不同的 header 的首页和页脚时,可以使用以下请求正文。

{
  "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.运行此请求正文时,header 的第一页和页脚与其他页分开。 But firstPageHeaderId and firstPageFooterId cannot be created.但无法创建firstPageHeaderIdfirstPageFooterId In the current stage, it seems that when you manually click the header and footer on Google Document, those values are created.在当前阶段,似乎当您手动单击 Google 文档上的 header 和页脚时,会创建这些值。
  • When you want to manage the header and footer of another page except for 1st page, please use defaultHeaderId and defaultFooterId as segmentId .当您要管理 header 和除第一页以外的其他页面的页脚时,请使用defaultHeaderIddefaultFooterId作为segmentId
  • True of "useFirstPageHeaderFooter": True is for python. "useFirstPageHeaderFooter": True True于 python。

Question 2:问题2:

Q2: How can I have page number as auto incremented on the right hand side of footer. Q2:如何在页脚右侧自动增加页码。

Answer:回答:

Unfortunately, it seems that in the current stage, this cannot be also achieved by Docs API.不幸的是,在现阶段,Docs API 似乎也无法实现这一点。

Question 3:问题 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? 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:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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