Extract tables from Microsoft Office Word documents
Extract tables from Microsoft Office Word documents
Leave feedback
This guide explains how to extract tables from Microsoft Office Word documents (.doc, .docx) using GroupDocs.Parser for Java.
How table extraction works
GroupDocs.Parser converts a document’s layout into a structured XML format. The getStructure method returns this XML as an org.w3c.dom.Document object. Tables are represented by <table> tags, rows by <tr> tags and cells by <td> tags. The extraction process involves reading this XML structure to identify and process table content. For more details, see Extract text structure.
Warning
getStructure method returns null value if text structure extraction isn’t supported for the document. For example, text structure extraction isn’t supported for TXT files. Therefore, for TXT file getStructure method returns null. If Microsoft Office Word document has no text, getStructure method returns an empty org.w3c.dom.Document object.
Step-by-step guide
1. Create a Parser instance
Begin by initializing the Parser with your document path.
try(Parserparser=newParser("YourDocument.docx")){// Extraction code goes here
}
2. Retrieve the document structure
Get the XML representation of the document’s structure.
Documentdocument=parser.getStructure();if(document==null){System.out.println("Text structure extraction isn't supported for this document.");return;}// Process the XML structure
3. Find and process tables
Locate the table elements in the XML document.
NodeListtables=document.getElementsByTagName("table");for(inti=0;i<tables.getLength();i++){System.out.println("Found a table:");processTable(tables.item(i));}
4. Table processing method
This method handles the extraction of rows and cells from each table.
privatestaticvoidprocessTable(Nodetable){introwIndex=0;NodeListrows=table.getChildNodes();for(intr=0;r<rows.getLength();r++){Noderow=rows.item(r);// Detect rows
if(!"tr".equals(row.getNodeName())){continue;}rowIndex++;System.out.println(String.format("[Row %d]",rowIndex));NodeListcells=row.getChildNodes();for(intc=0;c<cells.getLength();c++){Nodecell=cells.item(c);// Output the text of each cell
if("td".equals(cell.getNodeName())){System.out.println(" "+cell.getTextContent());}}}}
Complete example
Here’s a complete working example that extracts and displays tables from a Word document:
importcom.groupdocs.parser.Parser;importorg.w3c.dom.Document;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;publicclassWordTableExtractor{publicstaticvoidextractTables(StringfilePath){try(Parserparser=newParser(filePath)){Documentdocument=parser.getStructure();if(document==null){System.out.println("Text structure extraction isn't supported for this document.");return;}System.out.println("Analyzing: "+filePath);System.out.println("----------------------------------------");NodeListtables=document.getElementsByTagName("table");for(inti=0;i<tables.getLength();i++){System.out.println();System.out.println(">>> TABLE FOUND");processTable(tables.item(i));}}}privatestaticvoidprocessTable(Nodetable){introwIndex=0;NodeListrows=table.getChildNodes();for(intr=0;r<rows.getLength();r++){Noderow=rows.item(r);if(!"tr".equals(row.getNodeName())){continue;}rowIndex++;System.out.println(String.format("[Row %d]",rowIndex));NodeListcells=row.getChildNodes();for(intc=0;c<cells.getLength();c++){Nodecell=cells.item(c);if("td".equals(cell.getNodeName())){System.out.println(" "+cell.getTextContent());}}}}}
Extract tables with the getTables method
Word documents also support the getTables method. It reads the tables of the document directly and returns a collection of PageTableArea objects, so you can access cells by row and column index instead of walking the XML. The method returns null if table extraction isn’t supported for the document; check it with the isTables method of getFeatures. For more details, see Extract tables from document.
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleDocx)){// Check if the document supports table extraction
if(!parser.getFeatures().isTables()){System.out.println("Document doesn't support tables extraction.");return;}// Extract tables from the document
Iterable<PageTableArea>tables=parser.getTables();// Iterate over tables
for(PageTableAreat:tables){// Iterate over rows
for(introw=0;row<t.getRowCount();row++){// Iterate over columns
for(intcolumn=0;column<t.getColumnCount();column++){// Get the table cell
PageTableAreaCellcell=t.getCell(row,column);if(cell!=null){// Print the table cell text
System.out.print(cell.getText());System.out.print(" | ");}}System.out.println();}System.out.println();}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured Java library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.