Method for extracting table at specified position in word document
By identifying document titles and extracting tabular data under specific titles using breadth-first search algorithms, the problem of inflexible extraction in the prior art is solved, and efficient and accurate information extraction and data analysis support is achieved.
Patent Information
- Application Number
- CN202510640373.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-19
- Publication Date
- 2025-08-12
AI Technical Summary
The prior art cannot accurately locate and extract all tables under a specific title based on the structure and context of the document, resulting in cumbersome and inflexible information extraction.
By identifying titles in the document, traverse the document structure using the breadth-first search algorithm, extract all tabular data under a specific title, and add an error handling mechanism during the extraction process to ensure stability, the result output is in an easy-to-process format.
It improves the accuracy and work efficiency of information extraction, enhances the context relevance of data, is suitable for a variety of document formats, and supports data analysis and model training.
Smart Images

Figure CN120471031A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, in particular to natural language processing and document processing technology, and in particular to a method for extracting a table at a specified position in a Word document. Background Art
[0002] In modern document processing, text editing software such as Microsoft Word is widely used for information recording and proposal writing. These documents often contain multiple tables used to present data and analysis results. Traditional document table extraction methods typically default to processing by table index, meaning that users can only extract data by specifying the table's position in the document (e.g., the number of the table). However, such methods have significant limitations, primarily in that they cannot meet user needs for specific contextual information, such as directly extracting all tables under a certain heading.
[0003] In many practical applications, users want to accurately locate and extract all related tables within a document (such as a section title) based on its structure, facilitating information aggregation and analysis. However, existing technologies often ignore the logical relationships within the document, making the extraction process cumbersome and inflexible. Therefore, developing a method that can accurately extract all tables under a given title has important theoretical and practical significance. Summary of the Invention
[0004] To address the above technical issues, the present invention provides a method for extracting tables from specified locations in Word documents. By identifying document titles, this method tightly integrates table extraction with the document structure, achieving efficient and accurate information extraction. This method not only improves document processing efficiency but also provides a reliable data foundation for data analysis and decision support.
[0005] The technical solution of the present invention is:
[0006] A method for extracting a table at a specified location in a Word document, comprising the following steps:
[0007] a. Load the target .docx document;
[0008] b. Traverse each paragraph in the document to identify specific title text;
[0009] c. When the specific title is found, mark the start of the extraction phase;
[0010] d. Extract all table data under the specific heading until the next heading of the same level is encountered or the document ends.
[0011] Further,
[0012] Users can customize the target title and its corresponding table extraction area as needed.
[0013] Going further,
[0014] It also includes error handling mechanisms to ensure stability in various document structure situations.
[0015] Further,
[0016] Document loading: Load documents from local files or remote servers, treat XML files as tree structures, and use a streaming XML parser to parse the document.xml file. The streaming parser parses each element one by one.
[0017] Paragraph traversal and title recognition use XML nodes in the document to determine whether the current document is a paragraph or a table. If it is a paragraph, the paragraph style is checked to determine whether it is the target title specified by the user. If the target title specified by the user is not matched, the search continues backwards and this step is repeated. If a matching title is found, the mark starts the extraction phase.
[0018] Use the breadth-first search algorithm, the specific operations are:
[0019] a) Use a queue to store nodes to be visited;
[0020] b) Start from the first child node of the title node and add it to the queue;
[0021] c) Loop out nodes from the queue and determine whether they are table elements;
[0022] d) If it is a table element, add it to the result list;
[0023] e) Add all child nodes of the current node to the queue;
[0024] f) Repeat the above steps until the queue is empty.
[0025] Table extraction: After confirming the title, continue to traverse the tables in the document. The specific operation is to find the subordinate nodes of the node where the current title is located, and extract all the table data under the title. During the extraction process, the table content is organized into lists or dictionaries by rows and columns in a structured manner to facilitate subsequent data analysis, display, and application. During the extraction process, useless or overlong cells are removed through rule judgment. The extraction process will continue until the next title of the same level or the end of the document is encountered. During the extraction process, the directory is excluded according to the rules.
[0026] Result output: The extracted table data is saved in a user-specified format for subsequent use; at the same time, data visualization or further analysis functions are provided as needed to enhance the readability and practicality of the data.
[0027] The beneficial effects of the present invention are
[0028] Improved accuracy of information extraction: This method significantly enhances the contextual relevance of data extraction by locating and extracting relevant tables based on specific headings. This feature ensures that users obtain data directly related to their needs, avoiding omissions or errors that can occur with traditional methods due to inaccurate table indexing.
[0029] Optimize Work Efficiency: This automated extraction process significantly reduces the time users would otherwise spend manually searching and extracting tables. By streamlining the process, users can complete data collection in a shorter timeframe, providing timely support for subsequent analysis and decision-making, thereby improving overall work efficiency.
[0030] Enhanced Flexibility and Adaptability: This system is not only applicable to various documents containing tables (such as scientific papers, business reports, and legal documents), but can also handle complex documents with different formats and structures, making it suitable for a wide range of scenarios. This flexibility enables the system to meet the needs of different users in different application scenarios.
[0031] Support data analysis and decision-making, support model training: By extracting the required data quickly and accurately, the present invention provides a reliable foundation for subsequent data analysis and decision-making. Users can organize, visualize and conduct in-depth analysis of data more efficiently, thereby making more scientific and reasonable business decisions. In addition, the data extracted by the present invention can be used as training samples for large models (such as review scenarios), especially when fine-tuning is required for specific fields or scenarios, to provide high-quality labeled data. This function not only provides rich contextual information for model training, but also enhances the performance of the model in specific applications, enabling it to better understand and process text data in professional fields. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 It is a schematic diagram of the workflow of the present invention. DETAILED DESCRIPTION
[0033] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0034] This invention aims to provide an efficient and flexible method for extracting tables from specific locations in .docx documents. Specifically, it can accurately locate and extract all tables under a specific heading based on the document structure and headings. By overcoming the limitations of traditional methods that rely solely on table index extraction, this invention aims to achieve a more intelligent information extraction process, enabling users to quickly obtain the required data, thereby improving the efficiency and accuracy of document processing.
[0035] Specifically, the objects of the present invention include:
[0036] Enhance the accuracy of information extraction: Allow users to directly extract relevant tables based on the title or paragraph structure in the document, ensuring the contextual relevance of the extracted content.
[0037] Improve data processing efficiency: Reduce the time users spend manually searching and extracting tables, and speed up data collection and analysis through automated processes.
[0038] Support for multiple application scenarios: This makes the method suitable for various scenarios such as scientific research reports, business documents, legal documents, etc., to meet the needs of different users.
[0039] By achieving the above objectives, the present invention will make significant contributions to improving the efficiency of document management, data analysis, and large-scale model-specific scenario training, and provide users with a more convenient and flexible working method.
[0040] The present invention provides a method for extracting a table at a specified location from a .docx document. The basic process includes the following key steps:
[0041] Document loading: Load documents from local files or remote servers, treating XML files as tree structures and using a streaming XML parser to parse the document.xml file. The streaming parser does not load the entire document into memory, but parses it element by element, thus reducing memory usage.
[0042] Paragraph traversal and title identification: Traverse all paragraphs in the document and identify specific title text. This step uses XML nodes in the document to determine whether the current document is a paragraph or a table. If it is a paragraph, the paragraph style (such as "Heading 1," "Heading 2," etc.) is checked to determine whether it is the user-specified target title. If the user-specified target title is not matched, the search continues backward and this step is repeated. If a matching title is found, the extraction phase is marked as started. The search principle is as follows: Although traditional table acquisition methods can only obtain a list of tables in the entire document, when traversing paragraphs, the tag attribute of the XML node can be used to determine whether it is a paragraph or a table (such as w:p, w:tbl, w:t). If it is a paragraph, the w:pStyle attribute in the XML node can be used to determine the title level (for example, the attribute value is "Heading1" or "Title"); if it is a table, the entire table information can be obtained. Search scope limitation: After finding the target title, the search scope is limited to the subtree corresponding to the title. This avoids searching the entire document and improves efficiency.
[0043] Tables in most documents are relatively close to titles, so consider using a breadth-first search algorithm. The specific operations are:
[0044] ●Use a queue to store nodes to be visited.
[0045] ●Start with the first child node of the title node and add it to the queue.
[0046] ● Loop through the nodes from the queue and determine whether they are table elements (w:tbl).
[0047] ●If it is a table element, add it to the result list.
[0048] ●Add all child nodes of the current node to the queue.
[0049] ●Repeat the above steps until the queue is empty.
[0050] The specific algorithm is:
[0051] G=(V,E): represents a graph, where V is a set of vertices and E is a set of edges.
[0052] s∈V: represents the starting vertex, which is the starting point of the search.
[0053] adj(v): represents the set of adjacent vertices of vertex v. adj(v)
[0054] ={u∈V|(v,u)∈E}If it is an undirected graph, (v,u)∈E or (u,v)∈E both satisfy.
[0055] d(v): represents the distance from the starting vertex s to the vertex v, that is, the length of the shortest path (in an unweighted graph). In BFS, the value of d(v) is calculated by layer.
[0056] Q: represents a queue, which is used to store vertices to be visited.
[0057] visited(v): Boolean function indicating whether the vertex v has been visited.
[0058] Table extraction: After confirming the title, continue to traverse the tables in the document. The specific operation is to find the subordinate nodes of the node where the current title is located, and extract all the table data under the title. During the extraction process, the table content is organized into lists or dictionaries by rows and columns in a structured manner to facilitate subsequent data analysis, display, and application. During the extraction process, useless or overly long cells can be removed through rule judgment. In scenarios such as model training and intelligent review, removing low-value and overly long cell content can effectively avoid wasting tokens. The extraction process will continue until the next title of the same level or the end of the document is encountered, thereby ensuring that a complete set of information is obtained. In addition, the directory should be excluded according to the rules during the extraction process.
[0059] Result Output: The extracted tabular data is saved in a user-specified format (e.g., CSV, JSON, MD, etc.) for easy subsequent use. Data visualization or further analysis can also be provided as needed to enhance the readability and practicality of the data.
[0060] Exception handling: To improve the robustness of the system, an error handling mechanism will be added to the extraction process, which can effectively deal with situations such as irregular document structure, missing headers or empty table content, ensuring the stability of the entire extraction process.
[0061] The above description is only a preferred embodiment of the present invention and is only used to illustrate the technical solution of the present invention, and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention are included in the scope of protection of the present invention.
Claims
1. A method for extracting a table at a specified location in a word document, characterized in that: The following steps are involved: a. Load the target .docx document; b. Traverse each paragraph in the document to identify specific title text; c. When the specific title is found, mark the start of the extraction phase; d. Extract all table data under the specific heading until the next heading of the same level is encountered or the document ends.
2. The method according to claim 1, characterized in that Users can customize the target title and its corresponding table extraction area as needed.
3. The method according to claim 1 or 2, characterized in that It also includes error handling mechanisms to ensure stability in various document structure situations.
4. The method according to claim 1, wherein Document loading: Load documents from local files or remote servers, treat XML files as tree structures, and use a streaming XML parser to parse the document.xml file. The streaming parser parses each element one by one.
5. The method according to claim 4, characterized in that Paragraph traversal and title recognition use XML nodes in the document to determine whether the current document is a paragraph or a table. If it is a paragraph, the paragraph style is checked to determine whether it is the target title specified by the user. If the target title specified by the user is not matched, the search continues backwards and this step is repeated. If a matching title is found, the mark starts the extraction phase.
6. The method according to claim 5, characterized in that Use the breadth-first search algorithm, the specific operations are: a) Use a queue to store nodes to be visited; b) Start from the first child node of the title node and add it to the queue; c) Loop out nodes from the queue and determine whether they are table elements; d) If it is a table element, add it to the result list; e) Add all child nodes of the current node to the queue; f) Repeat the above steps until the queue is empty.
7. The method according to claim 5 or 6, characterized in that Table extraction: After confirming the title, continue to traverse the tables in the document. The specific operation is to find the subordinate nodes of the node where the current title is located, and extract all the table data under the title. During the extraction process, the table content is organized into lists or dictionaries by rows and columns in a structured manner to facilitate subsequent data analysis, display, and application. During the extraction process, useless or overlong cells are removed through rule judgment. The extraction process will continue until the next title of the same level is encountered or the end of the document.
8. The method according to claim 7, characterized in that During extraction, exclude directories according to the rules.
9. The method according to claim 7 or 8, characterized in that Result output: The extracted table data is saved in a user-specified format for subsequent use; at the same time, data visualization or further analysis functions are provided as needed to enhance the readability and practicality of the data.