簡體   English   中英

如何在 header 中添加圖像並保持首頁 header 和頁腳與其他頁面不同,使用谷歌文檔 API25523EEEB49ABBDD7

[英]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

預期樣品 output:第 1 頁: 第 1 頁

Rest 其他頁面: 其他頁面

請注意,兩個頁面的頁腳是不同的,它們是右對齊的並且是彩色的。 它的左側還有頁碼。

我相信你的目標如下。

  • 您想為 Google 文檔創建 header 和頁腳。
    • I'll keep the header and footer in my document same for all the pages. ,您想對第一頁和其他頁面使用相同的 header 和頁腳。
  • 對於 header,您想在右側插入圖像。
  • 對於頁腳,您要在右側插入 2 個文本。
  • 您想使用 python 的 googleapis 來實現此目的。

在這種情況下,下面的修改腳本怎么樣?

修改后的腳本:

在此修改中,請修改您的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

筆記:

  • 在此示例腳本中,當 header 和頁腳已創建時, Default header already exists. 發生。 因為 header 和頁腳無法添加到具有 header 和頁腳的文檔中。 請注意這一點。

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM