【问题标题】:How can we apply "different first page" for footers in the google docs using google docs API and python我们如何使用 google docs API 和 python 为 google docs 中的页脚应用“不同的首页”
【发布时间】:2022-01-06 05:04:38
【问题描述】:

@Tanaike 已经提供了以下 solution 以使用 google docs API 将页眉和页脚放入 google docs。

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. 上述代码在每一页上打印相同的页脚。如何在首页和其他页面上使用不同的页脚。

首页页脚:Disclaimer: This is my test footer. 其他页面页脚:@copyright reserved\nxyz.com

  1. 如何在页脚右侧自动增加页码。

  2. 当我在页眉中添加图像时,页眉大小增加了,如何使所有页面上的页眉大小保持不变?

【问题讨论】:

  • 在当前阶段,当创建新的 Google 文档并使用 Docs API 将页眉和页脚分隔为不同的首页时,很遗憾,firstPageHeaderIdfirstPageFooterId 没有创建。在这种情况下,文本和图像不能放在第一页的页眉和页脚。你想怎么做?
  • @Tanaike,你是说使用 google docs API 不可能有不同的首页?那么,添加页码呢?
  • 感谢您的回复。可以将页眉和页脚的第一页和其他页分开。但是,文本和图像不能放在第一页的页眉和页脚。因为没有创建 firstPageHeaderIdfirstPageFooterId。我认为这可能是由于DEFAULT 只有一种页眉和页脚类型。对于目前的情况,我深表歉意。
  • 是的。我认为你的理解是正确的。另一方面,除了第一页之外的页眉和页脚都可以通过Docs API进行管理。
  • 关于你的第三个问题,插入图像的宽度和高度可以改变。我认为这可能是您第三个问题的答案。

标签: python google-drive-api google-docs google-docs-api google-api-python-client


【解决方案1】:

问题一:

Q1:上面的代码在每一页上打印相同的页脚。如何在首页和其他页面上设置不同的页脚。

答案:

在当前阶段,当创建一个新的 Google 文档并使用 Docs API 将页眉和页脚分隔为不同的首页时,很遗憾,firstPageHeaderIdfirstPageFooterId 没有创建。在这种情况下,无法使用 Docs API 将文本和图像放入第一页页眉和页脚。我认为这可能是由于DEFAULT 只有一种页眉和页脚类型。另一方面,除了第一页之外的页眉和页脚都可以通过Docs API进行管理。

当要设置不同的首页页眉页脚时,可以使用如下请求体。

{
  "updateDocumentStyle": {
    "documentStyle": {
      "useFirstPageHeaderFooter": True
    },
    "fields": "useFirstPageHeaderFooter"
  }
}
  • 运行此请求正文时,页眉和页脚的第一页与其他页面分开。但是无法创建firstPageHeaderIdfirstPageFooterId。在当前阶段,似乎当您手动单击 Google 文档上的页眉和页脚时,会创建这些值。
  • 当您要管理除第一页以外的其他页面的页眉和页脚时,请使用defaultHeaderIddefaultFooterId作为segmentId
  • True"useFirstPageHeaderFooter": True 用于 python。

问题2:

Q2:如何让页码在页脚右侧自动递增。

答案:

很遗憾,目前看来,Docs API 也无法做到这一点。

问题3:

Q3:当我在页眉中添加图片时,页眉大小变大了,如何让所有页面的页眉大小保持不变?

答案:

可以更改插入图像的宽度和高度。我认为这可能是您第三个问题的答案。示例请求正文如下。

{
  "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"
      }
    }
  }
}

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多