【发布时间】:2014-12-14 03:08:48
【问题描述】:
我正在尝试使用 Django 模板呈现存储在 google 驱动器中的 word 文档文件 (docx) 的内容。 word文档文件(docx)是带有django变量的模板。 将文件转换为 google docs 格式将使 docx 文件失去其字体和样式格式,因此我试图在 google 应用引擎中实现以下步骤
- 使用其 downloadUrl 从谷歌驱动器下载 docx 文件
- 将下载的文件传递到python-docx模块中以提取文本
- 将提取的文本传递给 Django 以呈现 Django 变量
- 使用 python-docx 将文本写回 docx
- 最后将 docx 文件上传到另一个 google drive 帐户。
我在尝试将下载的文件传递到 python-docx 时遇到问题here
以下是我在谷歌应用引擎中的代码
downloadUrl = searchResult.get('items')[1]['downloadUrl']
if downloadUrl:
resp, tempContent = drive_service._http.request(downloadUrl)
if resp.status == 200:
f = StringIO.StringIO(tempContent)
document = Document(f)
para = document.paragraphs()
print para
f.close()
上面的代码给出了以下错误:
para = document.paragraphs()
TypeError: 'list' object is not callable
这是我在 Django 模板中渲染提取的文本的代码
myTemplate = Template(tempContent)
c = Context({
"salutation": "William",
"inventionTitle":"Biometric KeyLock"
})
fullContent = myTemplate.render(c)
下载文件的mimetype是:
application/vnd.openxmlformatsofficedocument.wordprocessingml.document
我的问题是,我不知道如何处理下载的文件。我想在不丢失格式的情况下替换存储在谷歌驱动器中的 word docx 中的占位符/变量,然后将其上传回谷歌驱动器。
如果有更好的实现方式,请告诉我。
谢谢。
【问题讨论】:
-
错误信息告诉你所有你需要知道的:
document.paragraphs不是一个方法,不要试图调用它。但在完成这项工作之前,您还有一大堆工作需要解决:就在我的脑海中,您希望如何将数据返回到 word doc 的正确位置? -
感谢 Daniel,但根据文档 >>> def paragraphs(self): """ 与文档中的段落相对应的 |Paragraph| 实例列表,按文档顺序排列。注意段落在
<w:ins>或<w:del>等修订标记内不会出现在此列表中。""" return self._document_part.paragraphs` -
链接到 [python-docx] (python-docx.readthedocs.org/en/latest/_modules/docx/…) 的文档
标签: python django google-app-engine ms-word django-templates