将文档文本从Google Docs Python API请求保存到.txt中

问题描述 投票:0回答:1

[尝试使用Google Docs Python API,特别是官方示例Extract text from a document一切运行正常,最后我将提取的文本打印到终端,这是最后一部分代码的结果,主要功能

def main():
    """Uses the Docs API to print out the text of a document."""
    credentials = get_credentials()
    http = credentials.authorize(Http())
    docs_service = discovery.build(
        'docs', 'v1', http=http, discoveryServiceUrl=DISCOVERY_DOC)
    doc = docs_service.documents().get(documentId=DOCUMENT_ID).execute()
    doc_content = doc.get('body').get('content')
print(read_strucutural_elements(doc_content))

[问题是我在尝试将该文本保存到.txt文件时遇到问题,得到此错误消息

f.write(read_strucutural_elements(doc_content))UnicodeEncodeError:'ascii'编解码器无法在位置64编码字符u'\ xfa':序数不在范围内(128)

read_structural_elements调用的返回类型是,>

print(type(read_strucutural_elements(doc_content)))

<type 'unicode'>

有任何建议吗?

干杯!

[尝试使用Google Docs Python API,特别是官方示例,从文档中提取文本一切运行良好,最后我将提取的文本打印到终端,这是最后一个结果...

python google-docs-api
1个回答
0
投票
如果从终端运行脚本,您所要做的就是指示它使用>运算符将输出写入文件。像这样的东西:
© www.soinside.com 2019 - 2024. All rights reserved.