【发布时间】:2022-01-04 09:38:48
【问题描述】:
感谢Tanaike's solution,我能够在我的文档中添加页眉和页脚。唯一的问题是我想让第一页的页眉和页脚与其他页面不同。
我还想在标题中添加多个小图像,但在标题中使用insertInlineImage 添加图像会引发错误。
我的工作代码:
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']
requests.append(add_header_content(index, header_id))
requests.append(add_footer_content(footer_id))
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_header_content(index, headerId):
headerText = {
"insertText": {
"location": {
"segmentId": headerId,
"index": index,
},
"text": "sample text"
}
}
return headerText
def add_footer():
footer = {
"createFooter": {
"type": "DEFAULT"
}
}
return footer
def add_footer_content(footer_id):
footer_data = {
"insertText": {
"location": {
"segmentId": footer_id,
"index": 0
},
"text": "This is my footer"
}
}
return footer_data
请注意,两个页面的页脚是不同的,它们是右对齐的并且是彩色的。它的左侧还有页码。
【问题讨论】:
-
当我看到你的演示脚本时,似乎没有包含
Also I want to add multiple small images in my header but using insertInlineImage in the header to add an image throws error.的脚本。可以加吗?而且,在您的目标中,您似乎想要使用不同的页眉和页脚首页。在您当前的脚本中,文本不会放在第一页。这个结果是你想要的吗?为了正确理解您的问题,能否以图片形式提供示例输出情况? -
@Tanaike,我添加了一个示例响应。我不知道使用什么有效负载来对齐页眉和页脚中的图像和文本。
-
感谢您回复并添加更多信息。但我不得不再次为我糟糕的英语水平道歉。当我看到您更新的问题时,似乎不包括您的
Also I want to add multiple small images in my header but using insertInlineImage in the header to add an image throws error.显示脚本。可以加吗?而且,您的示例图像中的文本似乎与您的显示脚本不同。这个怎么样?我从您的示例图像和脚本中混淆了您的目标。我想正确理解你的问题。对此我深表歉意。 -
现在,我注意到当创建一个新的 Google 文档并且页眉和页脚作为不同的第一页分开时,很遗憾,
firstPageHeaderId和firstPageFooterId没有创建。在这种情况下,文本和图像不能放在第一页的页眉和页脚。手动创建页眉和页脚时,会创建这些值。对此,你会怎么做?我认为造成这种情况的原因可能是标题类型只有DEFAULT的一个值。 -
@Tanaike,那么当我们将页眉/页脚类型设置为
HEADER_FOOTER_TYPE_UNSPECIFIED而不是DEFAULT时会发生什么?这将如何解决问题?
标签: python google-drive-api google-docs google-docs-api google-api-python-client