我有一个Word (.docx)文档,它有标题/标题以及标题下面的相应表格,如下所示:
1.1.1.1 Table_Title_001
Name Gautham
College Oxford
University Cambridge
1.1.1.2 Table_Title_002
Name Krishnan
College Harvard
University Stanford有没有办法使用Python提取每个表的标题?关于这一点,win32com或python-docx的文档不是很清楚。
发布于 2016-04-11 15:38:58
我现在不在Windows上,所以我不能在本地测试它,但是Word表格有一个Title property,看起来就是你想要的。
使用win32com,它看起来像这样:
from win32com import client
word = client.Dispatch("Word.Application")
document = word.Documents.Open(path_to_docx)
titles = [table.Title for table in document.Tables]发布于 2016-04-26 20:48:20
使用python-docx,这些代码非常容易阅读。在python-docx中,这些实际上不是标题,而是段落。开始时,我建议您先看看this site,这样您就可以了解它是如何工作的。
from docx import Document
doc = Document("wordfile.docx")
for para in doc.paragraphs:
print(para.text)这将打印出当前文档中的每一段。如果你的文件中只有标题和表格,这对你来说很好。
https://stackoverflow.com/questions/36542048
复制相似问题