Read Word Documents
A Word product report might contain written descriptions, specification tables, and product photos. When importing this information into another system, you need to preserve the rows and columns of each table and save the photos as separate files. You can read the author and title directly from the document properties.

This tutorial uses Spire.Doc for Python to read a specific paragraph or the full text of a Word document, export tables to CSV, extract embedded images, and retrieve document metadata. All five examples use the same code to load the document.

1. Install Spire.Doc and Load a Word Document

Run the following command in your terminal to install Spire.Doc for Python.

python -m pip install Spire.Doc 

Place an input.docx file containing paragraphs, a table, and an embedded image in your current working directory. Add the loading code below to the beginning of your script, followed by the examples you want to run. Call document.Close() at the end of the script after all read operations are complete.

from pathlib import Path from spire.doc import Document input_path = Path("input.docx") output_dir = Path("output") document = Document() document.LoadFromFile(str(input_path)) 

2. Read a Specific Paragraph in a Word Document

If a batch of documents uses the same template and the summary always appears in the same place, you can retrieve it by section and paragraph index. The following code reads the fourth paragraph in the first section. Indexing starts at 0, so the fourth paragraph has index 3.

section_index = 0 paragraph_index = 3 if section_index < document.Sections.Count: section = document.Sections.get_Item(section_index) if paragraph_index < section.Paragraphs.Count: paragraph = section.Paragraphs.get_Item(paragraph_index) print(paragraph.Text) else: print("The specified section has no fourth paragraph.") else: print("The specified section does not exist in the document.") 

Each Word paragraph occupies one index, even if it wraps across several lines on the page. To read paragraphs inside a table cell, use that cell’s Paragraphs collection.

3. Extract All Text from a Word Document

For full-text search, keyword counts, or plain-text archiving, call GetText() and write the result to a UTF-8 encoded TXT file.

output_dir.mkdir(parents=True, exist_ok=True) text_path = output_dir / "document.txt" text_path.write_text(document.GetText(), encoding="utf-8") 

Running the script creates output/document.txt. If you need to process table data by row and column, iterate through the Word table objects directly.

4. Export Word Tables to CSV

Use Section.Tables to access the tables in each section, then read their cells row by row. A cell can contain multiple paragraphs, so the example joins them with newline characters and stores them in a single CSV field. This version adapts the original space-delimited TXT example to use Python’s csv module, which handles fields containing commas, quotes, or line breaks.

import csv table_dir = output_dir / "tables" table_dir.mkdir(parents=True, exist_ok=True) for section_index in range(document.Sections.Count): section = document.Sections.get_Item(section_index) for table_index in range(section.Tables.Count): table = section.Tables.get_Item(table_index) csv_path = table_dir / f"table_{section_index + 1}_{table_index + 1}.csv" with csv_path.open("w", encoding="utf-8-sig", newline="") as file: writer = csv.writer(file) for row_index in range(table.Rows.Count): row = table.Rows.get_Item(row_index) values = [] for cell_index in range(row.Cells.Count): cell = row.Cells.get_Item(cell_index) paragraphs = [ cell.Paragraphs.get_Item(i).Text for i in range(cell.Paragraphs.Count) ] values.append("\n".join(paragraphs)) writer.writerow(values) 

For example, the second table in the first section is saved as output/tables/table_1_2.csv. The CSV contains the cell text in row order. If the original table has merged cells, the exported rows may contain different numbers of fields.

5. Extract Embedded Images from Word

Images can appear in regular paragraphs or inside table cells. Start at the document object and add composite objects to a queue so you can visit their children. When you encounter a DocPicture, read its ImageBytes property. The following example writes the bytes returned by the library directly to a file and chooses an extension based on common file signatures.

from collections import deque from spire.doc import DocPicture from spire.doc.common import ICompositeObject image_dir = output_dir / "images" image_dir.mkdir(parents=True, exist_ok=True) image_signatures = { b"\x89PNG\r\n\x1a\n": ".png", b"\xff\xd8\xff": ".jpg", b"GIF87a": ".gif", b"GIF89a": ".gif", b"BM": ".bmp", b"II*\x00": ".tif", b"MM\x00*": ".tif", } pending = deque([document]) image_number = 0 while pending: children = pending.popleft().ChildObjects for index in range(children.Count): child = children.get_Item(index) if isinstance(child, DocPicture): data = bytes(child.ImageBytes) if not data: continue extension = next( (ext for signature, ext in image_signatures.items() if data.startswith(signature)), ".bin", ) image_number += 1 (image_dir / f"image_{image_number:03d}{extension}").write_bytes(data) elif isinstance(child, ICompositeObject): pending.append(child) 

The extracted images go into output/images. For example, if the first image is a JPEG, it is saved as image_001.jpg. Formats that do not match any of the listed signatures are saved with the .bin extension so you can identify the format later.

6. Read Word Document Metadata

If you need to index a collection of Word files, use BuiltinDocumentProperties to retrieve properties such as the author, title, and subject. The example below prints six common fields to the terminal.

properties = document.BuiltinDocumentProperties metadata = { "Title": properties.Title, "Subject": properties.Subject, "Author": properties.Author, "Company": properties.Company, "Category": properties.Category, "Keywords": properties.Keywords, } for name, value in metadata.items(): print(f"{name}: {value or 'Not specified'}") 

The title comes from the Word document’s properties. Even if the first line of the document has a prominent heading, the example prints Not specified if the Title property is empty.

Conclusion

For documents that share a template, you can locate specific fields by section and paragraph index. For full-text search, you can retrieve the text with GetText(). When transferring content to another system, export tables to CSV, save images as separate files, and use document properties to build an index. To process a batch of Word files, reuse the same extraction logic and organize the output by source file.