如何在页眉中添加图像并使首页页眉和页脚与其他页面不同 [英] how to add image in header and keep first page header and footer different from other pages using google docs API in python

查看:56
本文介绍了如何在页眉中添加图像并使首页页眉和页脚与其他页面不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢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

预期样本输出: 第1页:

休息其他页面:

请注意,这两个页面的页脚是不同的,它们右对齐并且是彩色的。它的左侧还有页码。

推荐答案

我相信您的目标如下。

  • 您要创建Google文档的页眉和页脚。
    • I'll keep the header and footer in my document same for all the pages.开始,您希望对第一页和其他页使用相同的页眉和页脚。
  • 对于页眉,您希望在右侧插入图像。
  • 对于页脚,您希望在右侧插入2个文本。
  • 您希望使用用于Python的Googleapis来实现此目的。

在这种情况下,以下修改后的脚本如何?

修改后的脚本:

在本次修改中,请将insert_data的函数修改如下。

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
sample 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.的错误。因为页眉和页脚不能添加到有页眉和页脚的文档中。请注意此点。

引用:

这篇关于如何在页眉中添加图像并使首页页眉和页脚与其他页面不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆