Method for extracting complex table based on docx in xml format
By traversing the underlying xml information of the word document, using global variables to record the serial numbers of tables and cells, and recursively traverse each cell to extract text content, solving the problem of not being able to accurately associate component labels and text information in the existing technology, and achieving efficient and accurate data extraction.
Patent Information
- Application Number
- CN202510151796.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-12
- Publication Date
- 2025-03-14
- Estimated Expiration
- 2045-02-12
AI Technical Summary
现有技术在处理docx转化为xml后的数据提取时,尤其是在处理包含<w:sdtPr>文本组件的表格结构时,无法准确关联组件标签与文本信息,导致数据提取的准确性和完整性受到影响。
通过获取word文档的底层xml信息,遍历xml信息中的标签,利用全局变量记录表格、行和单元格的序号,并对每个单元格标签<w:tc>进行独立递归遍历,提取<w:t>标签的文本内容,拼接作为单元格的内容。
实现了在O(1)时间复杂度内提取所有<w:tag>标签及内容的方法,提高了数据提取的准确性和效率,能够处理复杂的docx文档结构。
Smart Images

Figure CN119623440A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a method for extracting complex tables based on XML format docx. Background Art
[0002] Existing technology simply relies on <w:tr>(table rows) and <w:tc>(table cell) tag to parse the table content, ignoring the various text types that may exist in the table. In fact, the table contains not only ordinary text entered directly, but also <w:sdtpr>Text component type text. This text component is similar to the normal <w:t>Label texts differ in structure, so simply relying on row and cell labels cannot accurately associate component labels with their corresponding text information.
[0003] The existing technology is difficult to process when extracting data after converting docx to xml, especially when processing data containing <w:sdtpr>When it comes to the table structure of text components, there is a problem of being unable to accurately associate component labels with text information, which limits the accuracy and completeness of data extraction and affects the subsequent further analysis and processing of document content. Summary of the invention
[0004] In order to help solve the above technical problems, this application provides a method for extracting complex tables based on XML format docx, using the following technical solutions: A method for extracting complex tables based on xml format docx, wherein the method comprises: Step S1: Get the underlying XML information of the word document, traverse the tags in the XML information, and traverse to a table tag each time <w:tbl>, then the value of the first global variable tableFlag is increased by 1, and the first global variable tableFlag is used to record the table number; Step S2: Traverse to the table tag <w:tbl>After that, continue to traverse, each time you traverse to a row label <w:tr>, then the value of the second global variable trFlag is increased by 1, and the second global variable trFlag is used to mark the row number. At the same time, the third global variable tcFlag is assigned a value of 0, indicating that a new row has not yet found a cell, and the third global variable tcFlag is used to mark the column number; Step S3: Traverse to row labels <w:tr>After that, continue to traverse, each time you traverse to a cell label <w:tc>, execute tcFlag=tcFlag+1 to mark the cell and record the row and column number of the cell. At the same time, assign the fourth global variable txt to an empty string. The fourth global variable txt is used to record the content of each cell in the table. Step S4: On the row labels that have been traversed <w:tr>, locate each cell label <w:tc>, and for each cell label <w:tc>Perform independent recursive traversal, and whenever you encounter a <w:t>When labeling, extract <w:t>The text content of the label will be <w:t>The text contents of the labels are concatenated as the content of the cell.
[0005] Preferably, the method further comprises step S5: locating the table tag <w:tbl>, for the table tag <w:tbl>Cell labels within <w:tc>and mark tags <w:tag>Traverse and record each mark label during the traversal <w:tag>and cell labels <w:tc>The parallel follow-up sequence during the traversal process, and the mark label is established according to the parallel follow-up sequence <w:tag>With cell label <w:tc>The corresponding relationship.
[0006] Preferably, the step S1 comprises: Input and decompress the Word document, read the document.xml file, and use the XML parsing library to load the XML information of the document.xml file, which includes tags.
[0007] In summary, this application plays an important role in the process of templated word writing and advance writing, solves the problem of content structuring of templated word writing using components, and has great application significance in actual production: 1. This application provides an effective method for parsing docx containing advanced components based on XML; 2. Using the XML structure of docx <w:tag>Tags and <w:t>The parallel follow-up order of tags can complete the extraction of all tags and content within the time complexity of O(1). This method provides a reliable solution for processing complex docx document structures and helps improve the accuracy and efficiency of data extraction. BRIEF DESCRIPTION OF THE DRAWINGS
[0008] Figure 1 This is a flowchart of an embodiment of a method for extracting complex tables based on XML format docx in the present application; Figure 2 It is a partial schematic diagram of a docx format word file containing advanced components to be extracted; Figure 3 for Figure 1 The embodiment shown is applied to Figure 2 Schematic diagram of part of the code. DETAILED DESCRIPTION
[0009] The present application is further described below in conjunction with the accompanying drawings, and the structure and principle of the present application are very clear to people in the field. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0010] Figure 1 This is a flowchart of an embodiment of a method for extracting complex tables based on XML format docx in the present application.
[0011] Step S1: Get the underlying XML information of the Word document, input and decompress the Word document, read the document.xml file, and use the XML parsing library to load the XML information of the document.xml file. The XML information includes tags. Traverse the tags in the XML information, and each time you traverse to a table tag <w:tbl>, then the value of the first global variable tableFlag is increased by 1. The first global variable tableFlag is used to record the table number.
[0012] Step S1 is the initial step of processing the underlying XML information of the Word document. The purpose is to obtain the XML structure of the Word document and traverse the XML structure of the Word document. <w:tbl>The label is used to mark the table number. The following is a detailed description of step S1: Step S1 is used to parse the XML content of the Word document, identify and mark the serial number of each table. This is done by traversing the XML document. <w:tbl>The tag is implemented. Every time this tag is encountered, the value of the global variable tableFlag is increased to assign a unique serial number to each table in the document. A Word document is actually a compressed package that contains multiple XML files, among which document.xml is the file containing the main content of the document. To obtain the XML content of a Word document, you first need to decompress the Word document and then read the document.xml file. The above content can be implemented programmatically. For example, in Python, you can use the zipfile library to decompress the file and use libraries such as lxml or xml.etree.ElementTree to parse the XML file. Then use the XML parsing library to load the content of the document.xml file. The parsing library will build an internal representation based on the structure and tags of XML, which is a tree structure. During the parsing process, the parsing library will identify all tags, attributes, and text content, which can be used for subsequent processing and analysis.
[0013] In addition, in step S1, the parsed XML document is traversed to find all <w:tbl>Label. <w:tbl>The tag is used to mark the beginning of a table in the XML representation of a Word document. The traversal process can be implemented recursively or iteratively. Recursive traversal is suitable for XML documents with nested structures, while iterative traversal is suitable for documents with simpler structures. Whenever a <w:tbl>When the tag is set, the value of the global variable tableFlag is increased by 1. tableFlag is used to record the serial number of the current table, so that each table is assigned a unique serial number. This serial number can be used to identify and reference different tables in subsequent processing.
[0014] Step S2: Traverse to the table tag <w:tbl>After that, continue to traverse, each time you traverse to a row label <w:tr>, the value of the second global variable trFlag is increased by 1. The second global variable trFlag is used to mark the row number. At the same time, the third global variable tcFlag is assigned a value of 0, indicating that a new row has not yet found a cell. The third global variable tcFlag is used to mark the column number.
[0015] Step S2 is a process of further processing the XML content of the Word document based on the recognition and marking of the table number in step S1. The main purpose is to mark the row number inside the table and initialize the column number mark for the new row. The following is a detailed introduction to step S2: Step S2 is used to identify the table (i.e. <w:tbl>Label) continue to traverse and find all row labels ( <w:tr>), and assign a unique row number to each row. At the same time, whenever a new row is encountered, the column number mark is initialized to prepare to record the information of the cells in the row.
[0016] In step S1, the table ( <w:tbl>Tags), continue to traverse the contents of the table. In the XML representation of a Word document, the contents of a table are usually contained in one or more <w:tr>Within the tag, each <w:tr>The tag represents a row in a table. Find all <w:tr>Tags, whenever you encounter a new <w:tr>Tag, indicating that a new row of content has begun in the table. Whenever a new <w:tr>When the tag is set, the value of the second global variable trFlag is increased by 1. trFlag is used to record the serial number of the current row, so that each row in the table will be assigned a unique row number, which can be used to identify and reference different rows in subsequent processing. <w:tr>When the third global variable tcFlag is assigned a value of 0, tcFlag is used to record the serial number of the cell in the current row, and the cell and row and column coordinates are output at this time. Initializing tcFlag to 0 is to indicate that no cell has been found in the new row, and the information of the cells in the row is about to be recorded.
[0017] By assigning a unique row number to each row in the table and a unique column number to each cell in each row (although the column number is further processed in step S3), step S2 provides a structured identification for the table content. This identification helps to quickly locate and reference a specific row or cell in the table in subsequent processing. In addition, when processing Word documents, it is often necessary to extract data from the table for analysis. Step S2 facilitates subsequent data extraction and analysis by providing a structured identification for the table content.
[0018] Step S3: Traverse to row labels <w:tr>After that, continue to traverse, each time you traverse to a cell label <w:tc>, tcFlag=tcFlag+1 is executed to mark the cell and record the row and column number of the cell. At the same time, the fourth global variable txt is assigned an empty string. The fourth global variable txt is used to record the content of each cell in the table.
[0019] Step S3 is based on step S2, and the table cells in the Word document ( <w:tc>The purpose of traversing and marking is to assign a unique column number to each cell in the table and record the content of the cell. The following is a detailed description of step S3: Step S3 is used to identify the table row ( <w:tr>Label), continue to traverse the table content and find all cell labels ( <w:tc>), and assign a unique column number to each cell. At the same time, record the content of each cell for subsequent processing.
[0020] In step S2, the table row has been identified ( <w:tr>Label) and marked the row number. In step S3, you need to traverse the content of each row and find all the cell labels ( <w:tc>), each <w:tc>The tag represents a cell in the table. Whenever a new <w:tc>When traversing cell labels ( <w:tc>), the content of each cell is recorded at the same time. The content of a cell may include text, pictures, formulas, etc. When processing text cells, its content can be extracted and saved to the corresponding data structure, such as a list, dictionary, etc. In a Word document, a table may contain merged cells. When processing merged cells, special attention should be paid to the assignment of column numbers and the extraction of content. Merged cells may span multiple rows or columns, so corresponding logic is required to handle this situation when traversing and marking.
[0021] By assigning a unique column number to each cell in the table, step S3 provides a unique identifier for the table cell, which helps to quickly locate and reference a specific cell in the table in subsequent processing. When processing table data in a Word document, it is often necessary to extract data from the table for analysis. Step S3 facilitates subsequent data extraction and analysis by providing a unique identifier for the table cell and recording its content.
[0022] Create an object to record the previous table, row number, and column number tags. Its structure is as follows: { String cellValue; int tableId; int rowNum; int colNum; int colCount; } In this embodiment, the structure may include: String cellValue: The value of the cell.
[0023] int tableId: the serial number of the table.
[0024] int rowNum: the row number.
[0025] int colNum: the column number.
[0026] int colCount: May be used to record the total number of columns or other related information.
[0027] Step S4: On the row labels that have been traversed <w:tr>, locate each cell label <w:tc>, and for each cell label <w:tc>Perform independent recursive traversal, and whenever you encounter a <w:t>When labeling, extract <w:t>The text content of the label will be <w:t>The text contents of the labels are concatenated as the content of the cell.
[0028] Step S4 is used to process the extraction of table cell contents in the Word document, especially for empty cells and complex structures (such as multiple text components) that may exist in cells. The following is a detailed explanation of step S4: In the XML representation of a Word document, table cells are represented by <w:tc>Label indicates that the text content in a cell is usually contained in <w:t>tags. However, when the cell is empty, <w:t>The label may not exist. In addition, the cell may contain multiple <w:t>Tags, indicating segmented or differently formatted text. The purpose of step S4 is to recursively traverse <w:tc>Tags, accurately extract and splice all text contents in cells.
[0029] In the identified table rows ( <w:tr>), locate each cell label ( <w:tc>). <w:tc>tags are recursively traversed independently. This means that <w:tc>All child nodes of the tag, looking for possible <w:t>During the recursive traversal, whenever a <w:t>When a tag is found, its text content is extracted. <w:t>Labels may contain segmented or differently formatted text, so there may be multiple <w:t>Tags. <w:t>The text contents of the labels are concatenated to form the final value of the cell. The order of the text contents is maintained during concatenation to ensure the accuracy of the final value. <w:t>If the cell is empty, the cell is determined to be empty. You can set the value of the empty cell to an empty string, a specific tag, or perform other processing as needed.
[0030] Step S4 ensures that all text content in the cell can be accurately extracted, including segmented and differently formatted text, through recursive traversal and text concatenation. At the same time, it explicitly handles the case of empty cells to avoid errors or omissions in data analysis and processing. Through recursive traversal and concatenation processing, the code can handle various complex cell structures, enhancing the robustness and applicability of the code.
[0031] Step S4 is mainly to solve the problem of empty cells in word documents, because empty cells will not appear <w:t>label, so it is not possible to determine whether a value has been retrieved. We can determine the cell label <w:tc>It must be <w:t>The parent of the label. Even if text components or other complex structures are used in the cell, it will not be affected. <w:tc>As <w:t>The subordinate relationship of the parent label. Therefore, when performing independent recursion, if you find <w:t>label, you can take out its value as the value of the cell. At the same time, if there are multiple <w:t>Tags, they all belong to the cell <w:tc>, so just replace each occurrence of <w:t>The label values are concatenated to get the final value of the cell.
[0032] Step S5: Locate the table tag <w:tbl>, for the table tag <w:tbl>Cell labels within <w:tc>and mark tags <w:tag>Traverse and record each mark label during the traversal <w:tag>and cell labels <w:tc>The parallel follow-up sequence during the traversal process, and the mark label is established according to the parallel follow-up sequence <w:tag>With cell label <w:tc>The corresponding relationship.
[0033] Step S5 is used to use the XML structure of docx <w:tag>Tags and <w:t>The tag parallel follow-up position order relationship is used to extract tags and content in step S5 using the XML structure of the docx document. <w:tag>Tags and <w:t>Label (or other text label, such as <w:r>Next <w:t>) in parallel and follow the order relationship, efficiently and accurately extract all <w:tag>Tags and their contents. The detailed steps are as follows: In the word / document.xml file, locate the <w:tag>A table or paragraph of tags. <w:tag>Tags are often used to add metadata or markup to document content. Traverse the XML elements in the table or paragraph you locate. During the traversal, be sure to identify <w:tag>Tags and <w:t>The juxtaposition of labels (or other text labels) follows the order of the labels. <w:tag>Tags and <w:t>Tags (or other text tags) are followed in parallel, and the tags and content can be extracted by recording their positional relationship. <w:tag>tag, check the first text tag after it (such as <w:t>), and the content of the text label is <w:tag>The tags are associated with each other. This association can be stored in a dictionary or list during the traversal process, where the key is <w:tag>The content or attribute of the tag, the value is the corresponding text content.
[0034] Since this method utilizes <w:tag>Tags and <w:t>The parallel follow-up order of the labels can complete a single <w:tag>Extraction of tags and content (O(1) here means for each encountered <w:tag>Tags, the time complexity of extracting their corresponding text content is constant level). Overall, the time complexity of traversing the entire document depends on the size and complexity of the document, but the process of extracting tags and content itself is efficient.
[0035] Step S5 can ensure that the extracted <w:tag>The tags and their contents are correct. The time complexity of the extraction process is low and it is suitable for processing large-scale documents. This method can be applied to different types of <w:tag>Labels and text tags, with some flexibility.
[0036] Figure 2 This is a partial schematic diagram of a word file in docx format containing advanced components to be extracted.
[0037] All components and their corresponding contents can be obtained by extraction, including components and contents in the table: [{"keyTag":"_SEC_e03362b794b84f94aa9e1cf0d75ab0a7","keyValue":"Securities Code: 601107 Securities Name: Sichuan Chengyu Announcement No.: 2024-032"},{"keyTag":"_GBC_cc6fdf7dc2054e4f9e082ed74b6a5425","keyValue":"601107"},{"keyTag":"_GBC_77e8fad392474aa4be479414251ffb31","keyValue ":"Sichuan Chengyu"},{"keyTag":"_GBC_fff01b59764149628ec7651b658cdfb6","keyValue":"2024-032"},{"keyTag":"_SEC_b41758ce170d4001bde4cc3e7b883f48","keyValue":"Announcement on the Suspension of Trading of Sichuan Chengyu Expressway Co., Ltd. on Planning to Issue Shares and Pay Cash to Purchase Assets and Related Transactions"},{"keyTag":"_GBC_469ed98c26544cde935109 dfa7edca74","keyValue":"Sichuan Chengyu Expressway Co., Ltd."},{"keyTag":"_SEC_36fd8164e5044892854b7c85141847a1","keyValue":"The board of directors and all directors of the company guarantee that there are no false records, misleading statements or major omissions in the content of this announcement, and assume legal responsibility for the authenticity, accuracy and completeness of its content. "},{"keyTag":"_SEC_bd115fdf57bb417bb207a7a3bcb3e7ae","keyValue ":"Securities trading suspension and resumption: Applicable"},{"keyTag":"_GBC_7bc6e15c25be4af7a604115d7be3c1e0","keyValue":"Applicable"},{"keyTag":"_SEC_b999c2774ad04c7a8dbd224d5f4ce2bf","keyValue":"Due to the planning of issuing shares and paying cash to purchase assets, the relevant securities of the company are suspended and resumed as follows: Securities Code Securities Name Suspension and Resumption Type Suspension Start Date Suspension Period Suspension End Date Resumption Date 601107 Sichuan Chengyu A shares suspended 2024 / 8 / 1 Test Test"},{"keyTag":"_GBC_61b6f7d4484843e7833d3af4a0b1815a","keyValue":"Planning to issue shares and pay cash to purchase assets"},{"keyTag":"_GBC_61b6f7d4484843e7833d3af4a0b1815a","keyValue":"Planning to issue shares and pay cash to purchase assets"},{"keyTag":"_PLD_91fd862c5ccb4038b8fab53000b60eab","keyValue ":"Securities Code"},{"keyTag":"_PLD_b497811ce5314dcb881e3522117bd243","keyValue":"Securities Abbreviation"},{"keyTag":"_PLD_d81a043638844ba484a776d722b21bf9","keyValue":"Suspension and Resumption Type"},{"keyTag":"_PLD_f4831 a4233dd436abe6b7b3339f83673","keyValue":"Suspension start date"},{"keyTag":"_PLD_b4e8cf5d3e45400ebe8f801e5d5dae30","keyValue":"Suspension period"},{"keyTag":"_PLD_1cddf0503b3448558ba96b1bcf87f03e","k eyValue":"Suspension end date"},{"keyTag":"_PLD_9bb8cdea07214f2996453d259535c186","keyValue":"Resumption date"},{"keyTag":"_TUP_c8ea1c70c9b54a6787cb064245caf3b0","keyValue":"601107 Sichuan Chengyu A shares suspended 2024 / 8 / 1 "},{"keyTag":"_GBC_7d973a87f5b5431a917e529ec3d0e616","keyValue":"601107"},{"keyTag":"_GBC_701adc90556040dca1f1a8640d69bb50","keyValue":" Sichuan Chengyu"},{"keyTag":"_GBC_217c8c8e470449ef8e5af2ebdb9d4d24","keyValue": "A shares"},{"keyTag":"_GBC_8dd9a1d861b846d7a792e9a2f570c689","keyValue":" "},{"keyTag":"_GBC_f6393728d9ce4378a646226773f36ac6","keyValue":"Suspended"},{"keyTag":"_GBC_ed78751f42a54bb98c b77b2add4fc2e7","keyValue":"2024 / 8 / 1"},{"keyTag":"_GBC_324721512e9b463cab0882af78445285","keyValue":" "},{"keyTag":"_GBC_41f8881dc1884262859a4bd77e1db0ab","keyValue":" "},{"keyTag":"_GBC_8c1300bb25dc48e18ccf94d390189524","keyValue":" "},{"keyTag":"_GBC_a0dbe34339a344a896b553a3a318a794","keyValue":"Sichuan Chengyu Expressway Co., Ltd."},{"keyTag":"_GBC_b0649edb53524c19a256bbb6e780e07f","keyValue":"July 31, 2024"}] Figure 2 It shows the component property information displayed for DOCX when using the development tool. By viewing the property information of the advanced text component with the content "Securities Code", it can be seen that the tag value of the component is: "_PLD_91fd862c5ccb4038b8fab53000b60eab". This information will be used as a unique primary key for extracting the XML corresponding to the document.
[0038] This method plays an important role in the process of templated word writing and advance preparation. It solves the problem of content structuring when using components for templated word writing and is widely used in actual production.
[0039] Figure 3 for Figure 1 The embodiment shown is applied to Figure 2 Schematic diagram of part of the code. Figure 2 and Figure 3 It can be seen that the content "Securities Code" is located in a cell of the table. According to normal XML logic, this text component should be located in <w:tc>tag, but the actual situation is that <w:sdtpr>and <w:sdtcontent>is a level relationship, and <w:tc>The label is located at <w:sdtcontent>In this way, it will not pass <w:tc>position <w:tag>Therefore, our method is to analyze the structure of the table <w:tag>After it appears, there will be a <w:t>Tags, when we traverse to <w:tag w:val="_PLD_91fd862c5ccb4038b8fab53000b60eab" / > When recording {"keyTag":"_PLD_91fd862c5ccb4038b8fab53000b60eab","keyValue":""}, continue traversing until the nearest <w:t> Securities Code< / w:t> When the previous structure is supplemented, {"keyTag":"_PLD_91fd862c5ccb4038b8fab53000b60eab","keyValue":"Securities Code"} is obtained, thus completing the extraction of high-level components in a table.
[0040] This figure shows the use of advanced component tags in the underlying XML data of DOCX to locate the cell content in the table. <w:tbl>For positioning tables, <w:tr>For positioning rows, <w:tc>Used to locate cells. From this document structure, we can see that the component information in the cell is located at <w:tc>The tags are in parallel relationship, and the content to be extracted is located in <w:tc>Within the tag range <w:t> tag.< / w:t> < / w:tc> < / w:tc> < / w:tc> < / w:tr> < / w:tbl> < / w:t> < / w:tag> < / w:tag> < / w:tc> < / w:sdtcontent> < / w:tc> < / w:sdtcontent> < / w:sdtpr> < / w:tc> < / w:tag> < / w:tag> < / w:tag> < / w:tag> < / w:t> < / w:tag> < / w:tag> < / w:tag> < / w:t> < / w:tag> < / w:t> < / w:tag> < / w:t> < / w:tag> < / w:tag> < / w:tag> < / w:tag> < / w:t> < / w:r> < / w:t> < / w:tag> < / w:t> < / w:tag> < / w:tc> < / w:tag> < / w:tc> < / w:tag> < / w:tag> < / w:tc> < / w:tbl> < / w:tbl> < / w:t> < / w:tc> < / w:t> < / w:t> < / w:t> < / w:tc> < / w:t> < / w:tc> < / w:t> < / w:t> < / w:t> < / w:t> < / w:t> < / w:t> < / w:t> < / w:tc> < / w:tc> < / w:tc> < / w:tr> < / w:tc> < / w:t> < / w:t> < / w:t> < / w:tc> < / w:t> < / w:t> < / w:t> < / w:tc> < / w:tc> < / w:tr> < / w:tc> < / w:tc> < / w:tc> < / w:tc> < / w:tr> < / w:tc> < / w:tr> < / w:tc> < / w:tc> < / w:tr> < / w:tr> < / w:tr> < / w:tr> < / w:tr> < / w:tr> < / w:tr> < / w:tbl> < / w:tr> < / w:tbl> < / w:tr> < / w:tbl> < / w:tbl> < / w:tbl> < / w:tbl> < / w:tbl> < / w:tbl> < / w:tbl> < / w:t> < / w:tag> < / w:tc> < / w:tag> < / w:tc> < / w:tag> < / w:tag> < / w:tc> < / w:tbl> < / w:tbl> < / w:t> < / w:t> < / w:t> < / w:tc> < / w:tc> < / w:tr> < / w:tc> < / w:tr> < / w:tr> < / w:tbl> < / w:tbl> < / w:sdtpr> < / w:t> < / w:sdtpr> < / w:tc> < / w:tr>
Claims
1. A method for extracting complex tables based on XML format docx, characterized in that: The method comprises: Step S1: Get the underlying XML information of the word document, traverse the tags in the XML information, and traverse to a table tag each time <w:tbl> , then the value of the first global variable tableFlag is increased by 1, and the first global variable tableFlag is used to record the table number;< / w:tbl> Step S2: Traverse to the table tag <w:tbl>After that, continue to traverse, each time you traverse to a row label <w:tr> , then the value of the second global variable trFlag is increased by 1, and the second global variable trFlag is used to mark the row number. At the same time, the third global variable tcFlag is assigned a value of 0, indicating that a new row has not yet found a cell, and the third global variable tcFlag is used to mark the column number;< / w:tr> < / w:tbl> Step S3: Traverse to row labels <w:tr>After that, continue to traverse, each time you traverse to a cell label <w:tc> , execute tcFlag=tcFlag+1 to mark the cell and record the row and column number of the cell. At the same time, assign the fourth global variable txt to an empty string. The fourth global variable txt is used to record the content of each cell in the table.< / w:tc> < / w:tr> Step S4: On the row labels that have been traversed <w:tr>, locate each cell label <w:tc>, and for each cell label <w:tc>Perform independent recursive traversal, and whenever you encounter a <w:t>When labeling, extract <w:t>The text content of the label will be <w:t> The text contents of the labels are concatenated as the content of the cell.< / w:t> < / w:t> < / w:t> < / w:tc> < / w:tc> < / w:tr> 2. The method according to claim 1, characterized in that The method further comprises step S5: locating the table tag <w:tbl>, for the table tag <w:tbl>Cell labels within <w:tc>and mark tags <w:tag>Traverse and record each mark label during the traversal <w:tag>and cell labels <w:tc>The parallel follow-up sequence during the traversal process, and the mark label is established according to the parallel follow-up sequence <w:tag>With cell label <w:tc> The corresponding relationship.< / w:tc> < / w:tag> < / w:tc> < / w:tag> < / w:tag> < / w:tc> < / w:tbl> < / w:tbl> 3. The method according to claim 1 or 2, characterized in that: The step S1 comprises: Input and decompress the Word document, read the document.xml file, and use the XML parsing library to load the XML information of the document.xml file, which includes tags.
Citation Information
Patent Citations
Method and device for converting form
CN102103587A
File display method and device
CN104156207A
Structured analysis method and system for docx format document
CN111062187A
Method and device for processing word document
CN112861489A
Method for automatically filling table based on freemarker
CN114118015A