python如何处理解析word文档doc docx , python-docx,python-docx2txt,zipfile

关于python如何处理word文档doc docx,可以关注 python-docx 和 python-docx2txt 两个项目,python-docx复杂一些,适合创建文档,python-docx2txt可以方便将文档转换成txt:

https://python-docx.readthedocs.org/en/latest/

https://github.com/python-openxml/python-docx


另外doc文件本身是个压缩文件,实际文档内容是xml结构的,可使用unzip解压:

# unzip test.docx
Archive:  test.docx
  inflating: _rels/.rels             
  inflating: word/settings.xml       
  inflating: word/_rels/document.xml.rels  
  inflating: word/fontTable.xml      
  inflating: word/styles.xml         
  inflating: word/document.xml       
  inflating: docProps/app.xml        
  inflating: docProps/core.xml       
  inflating: [Content_Types].xml     
# ls
[Content_Types].xml  docProps  _rels  test.docx  word

# ls
document.xml  fontTable.xml  _rels  settings.xml  styles.xml

# cat document.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"><w:body><w:p><w:pPr><w:pStyle w:val="Heading2"/><w:spacing w:lineRule="auto" w:line="240" w:before="0" w:after="0"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Heading5"/><w:spacing w:lineRule="auto" w:line="240"/><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/><w:b w:val="false"/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/><w:b w:val="false"/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t>Summary:02</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/><w:b w:val="false"/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t>系统基本功能</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/><w:b w:val="false"/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t>-01</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/><w:b w:val="false"/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t>系统核心功能</w:t></w:r><w:r>


不使用现成库可以使用zipfile直接解压:

import zipfile

document = zipfile.ZipFile('test.docx')
xml_content = document.read('word/document.xml')
reparsed = minidom.parseString(xml_content)
print reparsed.toprettyxml(indent="   " , encoding="utf-8")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值