我有一个问题: 如何在* .docx文件中添加标题(和页脚)? 我在我的django项目中使用库python-docx。但它真的不能添加标题。 我发现使用'win32com'的堆栈(2012年7月)解决方案,但现在它不适用于我。 帮我咨询。 谢谢。 祝您有愉快的一天。使用python在* .docx文件中添加标题
-1
A
回答
0
Courtest of edi9999 我认为你可以采取的最好的方法是看python-docx看看它是如何管理文件。 DOCX是一种压缩格式(Docx Tag Wiki):
的.docx
格式是包含以下文件夹压缩文件:
+--docProps
| + app.xml
| \ core.xml
+ res.log
+--word //this folder contains most of the files that control the content of the document
| + document.xml //Is the actual content of the document
| + endnotes.xml
| + fontTable.xml
| + footer1.xml //Containst the elements in the footer of the document
| + footnotes.xml
| +--media //This folder contains all images embedded in the word
| | \ image1.jpeg
| + settings.xml
| + styles.xml
| + stylesWithEffects.xml
| +--theme
| | \ theme1.xml
| + webSettings.xml
| \--_rels
| \ document.xml.rels //this document tells word where the images are situated
+ [Content_Types].xml
\--_rels
\ .rels
起初的docx例如DOCX,蟒蛇提取物库,因此你会发现它在蟒蛇的docx:我发现它给你:https://github.com/mikemaccana/python-docx/blob/master/docx.py#L65
def opendocx(file):
'''Open a docx file, return a document XML tree'''
mydoc = zipfile.ZipFile(file)
xmlcontent = mydoc.read('word/document.xml')
document = etree.fromstring(xmlcontent)
return document
你可以得到“字/ document.xml中”,这是DOCX的主要内容xmlcontent,使用的docx的Python改变它是(我向你推荐,docx- python似乎能够添加许多不同的元素。如果您想在文档的开头复制另一个word文档的内容,您可以尝试将document.xml的内容复制到document.xml中,但它可能会给您一些错误,特别是如果您使用图片或非文字内容。
要添加页眉或页脚,您必须创建一个文件word/header1.xml
或word/footer1.xml
您可以复制创建的文件的header1.xml内容,这应该起作用。
希望这会有帮助
相关问题
- 1. 使用python添加头到docx文件
- 2. 使用openxml将标题添加到docx
- 3. 使用novacode将标题中的图片添加到.docx DocX
- 4. 如何使用python docx在上标或下标中添加文本
- 5. 将标题放入Python - docx
- 6. 使用awk在文本文件中添加标题
- 7. 如何使用java在文本文件中添加标题行
- 8. 如何合并csv文件并使用python添加标题行?
- 9. 添加列标题使用python csv文件,读取JSON
- 10. python-docx添加水平线
- 11. 在CSV文件中添加列标题
- 12. 如何使用python-docx将行号添加到docx文档部分
- 13. 如何添加表格边框到Word文档使用Python Docx
- 14. 用python读取docx文件
- 15. 使用python读写.docx文件
- 16. 使用python保存回docx xml文件
- 17. 如何使用python创建docx文件
- 18. 使用POI将图像添加到单词.docx文档标题XWPF
- 19. 添加标题到文件
- 20. 将符号添加到.docx文件
- 21. 如何使用python docx功能在word文档中添加页眉和页脚?
- 22. 使用Python在文档(.docx)中的特定位置添加图像?
- 23. 使用While循环在Python中添加/添加文件
- 24. 如何使用docx中的python-docx标识分页符
- 25. 使用python-docx从docx文件读取coreproperties关键字
- 26. 使用python-docx更新大量文件的docx元数据
- 27. 使用python-docx在目录中搜索所有docx文件(批处理)
- 28. C#.NET DOCX添加图像到一个.docx文件
- 29. 在文本文件中添加文件名作为标题
- 30. 使用docx4j加密docx文件
试试win32com解决方案,应该很简单;并发布你的代码和什么不工作,我会看看我是否可以找到问题。 – Schollii