@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
First page footer: Disclaimer: This is my test footer.
Other pages footer: @copyright reserved\nxyz.com
How can I have page number as auto incremented on the right hand side of footer.
When I add an image in the header, the header size is increased, how can I make header size static on all the pages?
Q1: The above code prints the same footer on every page. How can I have different footer on first page and other pages.
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"
}
}
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.defaultHeaderId
and defaultFooterId
as segmentId
.True
of "useFirstPageHeaderFooter": True
is for python. Q2: How can I have page number as auto incremented on the right hand side of footer.
Unfortunately, it seems that in the current stage, this cannot be also achieved by Docs API.
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?
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"
}
}
}
}
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.