A method, device and equipment for parsing PDF table documents based on location
By building a table model and parsing string objects in PDF table documents based on location information, the problem of the inability of existing technology to parse PDF table documents is solved, and effective processing and analysis of structured data is achieved.
Patent Information
- Application Number
- CN202010826082.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-08-17
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2040-08-17
AI Technical Summary
Existing PDF document parsing technology cannot effectively parse structured data in PDF table documents, making data analysis and processing difficult.
By building a table model, extracting and arranging string objects in PDF table documents, determining the correspondence between strings and cells based on position information, and filling the strings into the table model, complete parsing of PDF table documents is achieved.
Parsing PDF table documents into tabular forms that are easy to analyze solves the problem that existing technology cannot completely parse table row and column data, and achieves effective processing of structured data.
Smart Images

Figure CN114077830B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device, equipment and readable storage medium for parsing a location-based PDF table document. Background Art
[0002] The official PDF specification does not support tables. In a large number of industry PDF documents, data is provided in the form of tables in PDF documents, such as tax declaration documents in the financial and tax fields. PDF table documents are not convenient for data analysis and processing, and technical solutions such as PDFBox, Tabula, and Itext are all based on text data reading, and do not support table data very well, and cannot fully parse table row and column data.
[0003] It can be seen that how to parse PDF table documents is a technical problem that needs to be solved urgently by technical personnel in this field. Summary of the invention
[0004] The purpose of this application is to provide a location-based PDF table document parsing method, device, equipment and readable storage medium to solve the problem that there is currently a lack of PDF table document parsing solutions, which makes it difficult to analyze and process structured data in PDF documents. The specific solution is as follows:
[0005] In a first aspect, the present application provides a location-based PDF table document parsing method, comprising:
[0006] Build a table model based on the attributes of structured data in the PDF table document;
[0007] Performing text extraction on the PDF table document to obtain a set of string objects, wherein the string object includes a string and position information of the string in the PDF table document;
[0008] Arranging the string objects according to the position information;
[0009] Processing the arranged string objects row by row to determine the corresponding relationship between the strings and the cells in the table model;
[0010] According to the corresponding relationship, the character string is filled into the corresponding cell in the table model.
[0011] Preferably, the step of processing the arranged string objects row by row to determine the correspondence between the strings and the cells in the table model includes:
[0012] The arranged string objects are processed row by row, and the target attribute corresponding to the string is determined by performing text recognition on the string; the cell used to record the data under the target attribute in the current row of the table model is determined to obtain the corresponding relationship between the string and the cell.
[0013] Preferably, the step of processing the arranged string objects row by row to determine the correspondence between the strings and the cells in the table model includes:
[0014] The arranged string objects are processed row by row, and the corresponding relationship between the string and the cell in the table model is determined according to the position information and the arrangement order of each attribute in the table model.
[0015] Preferably, after processing the arranged string objects row by row and determining the corresponding relationship between the strings and the cells in the table model, the method further includes:
[0016] If two or more character strings correspond to the same cell of the table model, the two or more character strings are merged.
[0017] Preferably, the step of extracting text from the PDF table document to obtain a set of string objects includes:
[0018] Through the EventListener mechanism provided by itext, text is extracted from the PDF table document to obtain a collection of string objects.
[0019] Preferably, after filling the character string into the corresponding cell in the table model according to the corresponding relationship, the method further includes:
[0020] Traverse the populated table model to get valid data.
[0021] In a second aspect, the present application provides a location-based PDF table document parsing device, comprising:
[0022] Table model building module: used to build a table model based on the properties of structured data in a PDF table document;
[0023] A text extraction module: used for extracting text from the PDF table document to obtain a set of string objects, wherein the string object includes a string and position information of the string in the PDF table document;
[0024] Arrangement module: used for arranging the string objects according to the position information;
[0025] A corresponding relationship determination module: used to process the arranged string objects row by row, and determine the corresponding relationship between the string and the cell in the table model;
[0026] Filling module: used to fill the character string into the corresponding cell in the table model according to the corresponding relationship.
[0027] Preferably, it also includes:
[0028] Merge module: used for merging two or more character strings if they correspond to the same cell of the table model.
[0029] In a third aspect, the present application provides a location-based PDF table document parsing device, comprising:
[0030] Memory: used to store computer programs;
[0031] Processor: used to execute the computer program to implement the steps of the location-based PDF form document parsing method as described above.
[0032] In a fourth aspect, the present application provides a readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the computer program is used to implement the steps of the location-based PDF form document parsing method as described above.
[0033] The present application provides a method for parsing a PDF table document based on a location, comprising: constructing a table model according to the attributes of structured data in the PDF table document; performing text extraction on the PDF table document to obtain a set of string objects, wherein the string objects include strings and location information of the strings in the PDF table document; arranging the string objects according to the location information; processing the arranged string objects row by row to determine the correspondence between the strings and cells in the table model; and filling the strings into corresponding cells in the table model according to the correspondence.
[0034] It can be seen that since the PDF official specification supports telling the location of specific strings in a table document, this method first determines the location information of each string in the PDF table document through text extraction, and then determines the correspondence between the string and the cell in the table based on the location information, and finally fills in the string, thereby realizing the parsing of the PDF table document into a table format that is convenient for data analysis and processing.
[0035] In addition, the present application also provides a location-based PDF table document parsing device, equipment and readable storage medium, the technical effects of which correspond to the above method and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the embodiments of the present application or the technical solutions of the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0037] Figure 1 A flowchart of the implementation of the first embodiment of the method for parsing a PDF table document based on location provided by the present application;
[0038] Figure 2 A flowchart of the implementation of the second embodiment of the method for parsing a PDF table document based on location provided by the present application;
[0039] Figure 3 A functional block diagram of an embodiment of a location-based PDF table document parsing device provided by the present application;
[0040] Figure 4 A schematic diagram of the structure of an embodiment of a location-based PDF table document parsing device provided in the present application. DETAILED DESCRIPTION
[0041] In order to enable those skilled in the art to better understand the present application, the present application is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application.
[0042] The official PDF specification does not support tables, but supports telling the location of specific strings in a table document. Current PDF document parsing solutions, such as PDFBox, Tabula, Itext, etc., can only parse text data, and cannot fully parse the structured data in the PDF document.
[0043] To address this problem, the present application provides a location-based PDF table document parsing method, apparatus, device and readable storage medium, which can extract formatted data in a PDF table document based on a pre-built table model and PDF table document, to facilitate subsequent data analysis and processing.
[0044] The following is an introduction to a method for parsing a PDF table document based on location provided by the present application. Figure 1 , Embodiment 1 includes:
[0045] S101, constructing a table model according to the attributes of structured data in the PDF table document;
[0046] S102, extracting text from the PDF table document to obtain a set of string objects, wherein the string objects include a string and position information of the string in the PDF table document;
[0047] S103, arranging the string objects according to the position information;
[0048] S104, processing the arranged string objects row by row, and determining the corresponding relationship between the string and the cell in the table model;
[0049] S105. Fill the character string into the corresponding cell in the table model according to the corresponding relationship.
[0050] In order to parse the structured data in the PDF table document, this embodiment provides a location-based PDF table document parsing method, which can extract strings and the location information of the strings in the PDF table document from the PDF table document, determine whether the string belongs to the same row or column as other strings through the location information, parse the actual position of the string in the table model, and determine the belonging of the string in the business data in turn.
[0051] It can be understood that the position information of the above string in the PDF table document is actually the coordinate position, for example, it can be the coordinate on the X-axis and the Y-axis. In this case, when the string objects are arranged according to the position information, it can be understood that the string objects are laid out on the XY-axis plane. The position of the above string in the table model is actually the row number and column number.
[0052] Specifically, based on the preset table model and PDF table document, the formatted data in the PDF table document is extracted. The input of the whole solution is the table model and the PDF table document, and the output is the formatted data.
[0053] Among them, the table model allows customization, which can be an easy-to-understand table described by rows, columns and related attributes. In practical applications, the table format is often formulated by relevant government departments or industry associations. Specifically, the corresponding table model can be constructed according to the attribute distribution of the PDF table document to be parsed.
[0054] Since there is no concept of table in PDF, the location information of the string in the PDF table document can be used to determine the attribution problem, that is, to determine which cell the string belongs to; in the process of row-by-row processing, since the attributes of the data in the same row are often different, the text recognition can also be used to determine which cell the string belongs to. In practical applications, any one or even both of the above two methods can be used.
[0055] The present embodiment provides a method for parsing a PDF table document based on location. First, the location information of each string in the PDF table document is determined by text extraction. Then, the correspondence between the string and the cell in the table is determined based on the location information. Finally, the string is filled in, thereby parsing the PDF table document into a table format that is convenient for data analysis and processing.
[0056] A second embodiment of a location-based PDF table document parsing method provided by the present application is described in detail below. The second embodiment is implemented based on the aforementioned first embodiment and is expanded to a certain extent on the basis of the first embodiment.
[0057] See also Figure 2 , Embodiment 2 specifically includes:
[0058] S201, constructing a table model according to the attributes of structured data in the PDF table document;
[0059] Define the table model according to the actual situation of the table, including Table, tr, td, among which tr / td supports nesting.
[0060] S202, extracting text from the PDF table document through the EventListener mechanism provided by itext to obtain a set of string objects;
[0061] The string object may specifically be TextTrunk(text, Rectangle), where text represents a string, and Rectangle represents the coordinate position of the string in the PDF table document.
[0062] S203, arranging the string objects according to the position information;
[0063] S204, processing the arranged string objects row by row, and determining the corresponding relationship between the string and the cell in the table model;
[0064] S205: if two or more character strings correspond to the same cell of the table model, merge the two or more character strings;
[0065] S206, filling the character string into the corresponding cell in the table model according to the corresponding relationship;
[0066] S207, traverse the filled table model to obtain valid data.
[0067] That is to say, after obtaining the table structure containing the data, you can traverse it row by row according to the conventional processing logic to obtain valid data.
[0068] Specifically, the above S204 can be implemented through text recognition. At this time, the process is: process the arranged string objects row by row, determine the attribute corresponding to the string by performing text recognition on the string; determine the cell in the current row of the table model for recording the data under the attribute, and obtain the correspondence between the string and the cell.
[0069] The above S204 can also be implemented based on the position information. In this case, the process is: process the arranged string objects row by row, and determine the correspondence between the strings and the cells in the table model based on the position information and the arrangement order of each attribute in the table model.
[0070] In addition, the above S204 can also be implemented by text recognition and position determination according to different attributes. Assume that the target attribute is specified in advance, the character strings of the target attribute are all implemented by text recognition, and the character strings of non-target attributes are all implemented by position determination. The specific process is as follows:
[0071] Specify the target attribute in advance. When filling the table model, first determine the data under the target attribute through text recognition; then determine the median of each row based on the data under the target attribute; then calculate the distance between adjacent medians to obtain the line spacing of each row; finally, determine all the data of the row based on the median and line spacing of each row to facilitate row-by-row processing.
[0072] In the process of row-by-row processing, the text recognition string can be used to determine the correspondence between each data in a row of data and the attribute, thereby obtaining the correspondence between the string and the cell. The correspondence between each data in a row of data and the attribute can also be determined based on the position information, thereby obtaining the correspondence between the string and the cell. Specifically, based on the position information, if there is no space between adjacent data, it is considered that the adjacent data belongs to the same attribute; if there is a space between adjacent data, it is considered that the adjacent data belongs to different attributes. In addition, the arrangement order of each attribute can be known according to the table model, so the correspondence between each data in a row of data and the attribute can be obtained based on the position information.
[0073] The above process from S203 to S206 may be specifically as follows: sort the TextTrunk by vertical coordinates, and the lower left corner of the page may be used as the coordinate starting point. Process tr line by line, determine the ownership of TextTrunk by text or position, and calculate whether TextTrunk is merged by position. Fill in data, assemble the text data of the cell, and fill it into tr and td.
[0074] A location-based PDF table document parsing device provided in an embodiment of the present application is introduced below. The location-based PDF table document parsing device described below and the location-based PDF table document parsing method described above can refer to each other.
[0075] like Figure 3 As shown, the location-based PDF table document parsing device of this embodiment includes:
[0076] Table model building module 301: used to build a table model according to the attributes of structured data in the PDF table document;
[0077] The text extraction module 302 is used to extract text from the PDF table document to obtain a set of string objects, wherein the string object includes a string and position information of the string in the PDF table document;
[0078] Arrangement module 303: used to arrange the string objects according to the position information;
[0079] The corresponding relationship determination module 304 is used to process the arranged string objects row by row to determine the corresponding relationship between the string and the cell in the table model;
[0080] Filling module 305: used for filling character strings into corresponding cells in the table model according to the corresponding relationship.
[0081] In some specific embodiments, it also includes:
[0082] Merge module: used for merging two or more character strings if they correspond to the same cell of the table model.
[0083] The device for parsing a PDF table document based on location in this embodiment is used to implement the aforementioned method for parsing a PDF table document based on location. Therefore, the specific implementation of the device can be seen in the embodiment of the method for parsing a PDF table document based on location in the above text. For example, the table model building module 301, the text extraction module 302, the arrangement module 303, the correspondence determination module 304, and the filling module 305 are respectively used to implement steps S101, S102, S103, S104, and S105 in the aforementioned method for parsing a PDF table document based on location. Therefore, its specific implementation can refer to the description of the corresponding embodiments of each part, and will not be introduced here.
[0084] In addition, since the location-based PDF form document parsing device of this embodiment is used to implement the aforementioned location-based PDF form document parsing method, its function corresponds to that of the aforementioned method, which will not be described in detail here.
[0085] In addition, the present application also provides a location-based PDF table document parsing device, such as Figure 4 As shown, including:
[0086] Memory 100: used for storing computer programs;
[0087] Processor 200: configured to execute the computer program to implement the steps of the location-based PDF form document parsing method as described above.
[0088] Finally, the present application provides a readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, it is used to implement the steps of the location-based PDF form document parsing method as described above.
[0089] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0090] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0091] The above is a detailed introduction to the scheme provided by the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for general technical personnel in this field, according to the idea of the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A method for parsing a PDF table document based on location, characterized in that: include: Build a table model based on the attributes of structured data in the PDF table document; Performing text extraction on the PDF table document to obtain a set of string objects, wherein the string object includes a string and position information of the string in the PDF table document; Arranging the string objects according to the position information; Processing the arranged string objects row by row to determine the corresponding relationship between the strings and the cells in the table model; According to the corresponding relationship, fill the character string into the corresponding cell in the table model; The step of processing the arranged string objects row by row to determine the corresponding relationship between the strings and the cells in the table model includes: A target attribute is specified in advance. When filling the table model, the data under the target attribute is determined by text recognition. Then, the median of each row is determined according to the data under the target attribute, and the distance between adjacent medians is calculated to obtain the line spacing of each row. According to the median line and the line spacing of each row, all data of the row are determined to facilitate row-by-row processing; In the process of row-by-row processing, the character string is identified by text to determine the corresponding relationship between each data and attribute in a row of data, and the corresponding relationship between the character string and the cell is obtained; The corresponding relationship between each data in a row of data and the attribute is determined according to the position information of the character string of the non-target attribute, and the corresponding relationship between the character string and the cell is obtained.
2. The method according to claim 1, characterized in that The step of processing the arranged string objects row by row to determine the corresponding relationship between the strings and the cells in the table model includes: The arranged string objects are processed row by row, and the target attribute corresponding to the string is determined by performing text recognition on the string; the cell used to record the data under the target attribute in the current row of the table model is determined to obtain the corresponding relationship between the string and the cell.
3. The method according to claim 1, characterized in that The step of processing the arranged string objects row by row to determine the corresponding relationship between the strings and the cells in the table model includes: The arranged string objects are processed row by row, and the corresponding relationship between the string and the cell in the table model is determined according to the position information and the arrangement order of each attribute in the table model.
4. The method according to claim 2 or 3, characterized in that After processing the arranged string objects row by row and determining the corresponding relationship between the strings and the cells in the table model, the method further includes: If two or more character strings correspond to the same cell of the table model, the two or more character strings are merged.
5. The method according to claim 1, characterized in that The text extraction is performed on the PDF table document to obtain a set of string objects, including: Through the EventListener mechanism provided by itext, text is extracted from the PDF table document to obtain a collection of string objects.
6. The method according to claim 1, characterized in that After filling the character string into the corresponding cell in the table model according to the corresponding relationship, the method further includes: Traverse the populated table model to obtain valid data.
7. A location-based PDF table document parsing device, characterized in that: include: Table model building module: used to build a table model based on the properties of structured data in a PDF table document; A text extraction module: used for extracting text from the PDF table document to obtain a set of string objects, wherein the string object includes a string and position information of the string in the PDF table document; Arrangement module: used for arranging the string objects according to the position information; A corresponding relationship determination module: used to process the arranged string objects row by row, and determine the corresponding relationship between the string and the cell in the table model; Filling module: used for filling the character string into the corresponding cell in the table model according to the corresponding relationship; The correspondence determination module is used to pre-specify the target attribute. When filling the table model, the data under the target attribute is determined through text recognition, and then the median of each row is determined based on the data under the target attribute, and the distance between adjacent medians is calculated to obtain the line spacing of each row; based on the median and the line spacing of each row, all the data in the row is determined to facilitate line-by-line processing; in the process of line-by-line processing, the character string is identified through text to determine the correspondence between each data in a line of data and the attribute, and the correspondence between the character string and the cell is obtained; the correspondence between each data in a line of data and the attribute is determined based on the position information of the character string of the non-target attribute, and the correspondence between the character string and the cell is obtained.
8. The device according to claim 7, characterized in that Also includes: Merge module: used for merging two or more character strings if they correspond to the same cell of the table model.
9. A device for parsing a PDF table document based on location, characterized in that: include: Memory: used to store computer programs; Processor: used to execute the computer program to implement the steps of the location-based PDF form document parsing method according to any one of claims 1 to 6.
10. A readable storage medium, characterized in that: The readable storage medium stores a computer program, and when the computer program is executed by a processor, it is used to implement the steps of the location-based PDF form document parsing method according to any one of claims 1 to 6.
