有人知道一个Python库来读取docx文件吗?
我有一个 Word 文档,我正在尝试从中读取数据。
python-docx 既可以读也可以写。
doc = docx.Document('myfile.docx')
allText = []
for docpara in doc.paragraphs:
allText.append(docpara.text)
现在所有段落都将出现在 allText 列表中。
感谢 Al Sweigart 的 Automate the Boring Stuff with Python 的指点。
查看这个允许读取 docx 文件的库 https://python-docx.readthedocs.io/en/latest/
您应该使用 PyPi 上提供的 python-docx 库。然后你就可以使用以下
doc = docx.Document('myfile.docx')
allText = []
for docpara in doc.paragraphs:
allText.append(docpara.text)
import docx
def main():
try:
doc = docx.Document('test.docx') # Creating word reader object.
data = ""
fullText = []
for para in doc.paragraphs:
fullText.append(para.text)
data = '\n'.join(fullText)
print(data)
except IOError:
print('There was an error opening the file!')
return
if __name__ == '__main__':
main()
不要忘记使用(pip install python-docx)安装python-docx