有谁知道一个 python 库来读取 docx 文件?
我有一个我正在尝试从中读取数据的 word 文档。
python-docx 可以读写。
doc = docx.Document('myfile.docx')
allText = []
for docpara in doc.paragraphs:
allText.append(docpara.text)
现在所有段落都将在列表 allText 中。
感谢 Al Sweigart 的“如何使用 Python 自动化无聊的东西”的指针。
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