首页 > 编程语言 > 详细

python读取word

时间:2020-02-21 20:41:06      阅读:102      评论:0      收藏:0      [点我收藏+]

from docx import Document

# 读取全文本

# document = Document(r‘C:\Users\13375\Desktop\python\长恨歌.docx‘)
# all_paragraphs = document.paragraphs
# for paragraph in all_paragraphs:
#     print(paragraph.text)
    
# #     读取表格中的文字
# document = Document(r‘C:\Users\13375\Desktop\python\长恨歌2.docx‘)
# all_tables = document.tables
# for table in all_tables:
#     for row in table.rows:
#         for cell in row.cells:
#             print(cell.text)

 

 

 

# 读取word中的表格和文字混排文档   需要zip文件类型(未能成功运行)
import zipfile

word = zipfile.ZipFile(‘C:/Users/13375/Desktop/python/长恨歌3.docx‘)
xml = word.read(‘word/document.xml‘).decode(‘utf-8‘)
print(xml)

xml_list = xml.split(‘<w:t>‘)
print(xml_list)
text_list = []

for i in xml_list:
    if i.find(‘<w:t>‘)+1:
        text_list.append(i[:i.find(‘<w:t>‘)])
    else:
        pass
print(text_list)

text = "".join(text_list)
print(text)




python读取word

原文:https://www.cnblogs.com/tomhu/p/12342930.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!