【问题标题】:how to add image in header and keep first page header and footer different from other pages using google docs API in python如何使用python中的google docs API在页眉中添加图像并使首页页眉和页脚与其他页面不同
【发布时间】: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

预期的样本输出: 第 1 页:

其他页面:

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

【问题讨论】:

  • 当我看到你的演示脚本时,似乎没有包含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 文档并且页眉和页脚作为不同的第一页分开时,很遗憾,firstPageHeaderIdfirstPageFooterId 没有创建。在这种情况下,文本和图像不能放在第一页的页眉和页脚。手动创建页眉和页脚时,会创建这些值。对此,你会怎么做?我认为造成这种情况的原因可能是标题类型只有DEFAULT的一个值。
  • @Tanaike,那么当我们将页眉/页脚类型设置为HEADER_FOOTER_TYPE_UNSPECIFIED 而不是DEFAULT 时会发生什么?这将如何解决问题?

标签: python google-drive-api google-docs google-docs-api google-api-python-client


【解决方案1】:

我相信你的目标如下。

  • 您想为 Google 文档创建页眉和页脚。
    • 来自I'll keep the header and footer in my document same for all the pages.,您希望第一页和其他页面使用相同的页眉和页脚。
  • 对于标题,您想在右侧插入图像。
  • 对于页脚,您想在右侧插入 2 个文本。
  • 您想使用 googleapis for python 来实现这一点。

这样的话,下面修改的脚本怎么样?

修改脚本:

在本次修改中,请将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\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. 的错误。因为无法将页眉和页脚添加到具有页眉和页脚的 Document 中。请注意这一点。

参考资料:

【讨论】:

  • 非常感谢@Tanaike。这按预期工作。还有一个问题,如果我想使用本地电脑中的图像,"uri" 字段的替代选项是什么?
  • @Ask_Ashu 感谢您的回复。我很高兴你的问题得到了解决。关于你的what would be the alternate for "uri" field if I want to use image from my local pc?的新问题,在这种情况下,需要将图片文件上传到可以使用直接链接的站点。因此,在这种情况下,我建议将您的新问题作为新问题发布。
  • 是的@Tanaike,我对此发布了一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 2021-08-14
  • 2015-09-02
相关资源
最近更新 更多