python读取word


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)





免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM