python如何處理解析word文檔doc docx , python-docx,python-docx2txt,zipfile

來源:互聯網
上載者:User

關於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")


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.