![](/img/trans.png)
[英]How can we apply "different first page" for footers in the google docs using google docs API and python
[英]how to add image in header and keep first page header and footer different from other pages using google docs API in python
感謝Tanaike 的解決方案,我能夠在我的文檔中添加 header 和頁腳。 唯一的問題是我想保持第一頁的 header 和頁腳與頁面的 rest 不同。
此外,我想在 header 中添加多個小圖像,但在 header 中使用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
請注意,兩個頁面的頁腳是不同的,它們是右對齊的並且是彩色的。 它的左側還有頁碼。
我相信你的目標如下。
I'll keep the header and footer in my document same for all the pages.
,您想對第一頁和其他頁面使用相同的 header 和頁腳。在這種情況下,下面的修改腳本怎么樣?
在此修改中,請修改您的insert_data
的 function 如下。
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']
# Add header content
requests += [
{
"insertInlineImage": {
"location": {
"segmentId": header_id,
"index": 0
},
"uri": "https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png", # 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\nsample text"
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()
END
修改為START
。Default header already exists.
發生。 因為 header 和頁腳無法添加到具有 header 和頁腳的文檔中。 請注意這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.