在Python中提取Word文档内容,你可以使用以下几种方法:
1. 使用`python-docx`库:
from docx import Document打开文档doc = Document('example.docx')读取段落for para in doc.paragraphs:print(para.text)读取表格for table in doc.tables:for row in table.rows:for cell in row.cells:print(cell.text)
2. 使用`win32com`库(适用于Windows系统):
import win32com.client as wcword = wc.Dispatch('Word.Application')doc = word.Documents.Open('c:/test.docx')doc.SaveAs('c:/test.txt', 4) 使用4表示保存为文本文件doc.Close()word.Quit()
3. 使用`Spire.Doc`库:
from spire.doc import Document创建Document对象document = Document()载入Word文档document.LoadFromFile('example.docx')获取文档中的文本text = document.GetText()将文本写入文本文件with open('output.txt', 'w') as f:f.write(text)提取图片for img in document.Images:img.SaveToFile('output_images/' + img.Name)document.Close()
4. 使用`zipfile`模块提取图片:
import zipfileimport osdef extract_images_from_word(docx_path, output_folder):with zipfile.ZipFile(docx_path, 'r') as docx_zip:for filename in docx_zip.namelist():if filename.endswith('.png') or filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.gif'):with open(os.path.join(output_folder, filename), 'wb') as img_out:img_out.write(docx_zip.read(filename))
请根据你的需求选择合适的方法。

