PDF document parsing method and device, electronic equipment and storage medium
By determining the coordinates of line and character elements in a PDF document, establishing sets of horizontal and vertical coordinates, identifying table areas, and drawing spreadsheets, this solves the problem that existing tools cannot handle tables without drawn lines, achieving efficient table parsing.
Patent Information
- Application Number
- CN202111082611.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-15
- Publication Date
- 2026-02-24
- Estimated Expiration
- 2041-09-15
AI Technical Summary
Existing PDF document parsing tools cannot accurately handle cases where there are undrawn table lines in tables, resulting in low parsing efficiency.
By obtaining the page object and determining the endpoint coordinates of the line elements, a set of horizontal and vertical coordinates is established to identify the table area; the string is determined based on the coordinates of the character elements, and the column and row identifiers are determined by combining the horizontal and vertical coordinates to draw the spreadsheet.
It improves the accuracy and efficiency of PDF document table parsing, enabling quick and accurate extraction of table content.
Smart Images

Figure CN113850265B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to data processing technology, especially artificial intelligence parsing technology for PDF documents, and particularly to a method, apparatus, electronic device and storage medium for parsing PDF documents. Background Technology
[0002] Portable Document Format (PDF) is a file format developed by Adobe Systems for exchanging files in a way that is independent of applications, operating systems, and hardware. At its core, PDF contains a stream of instructions describing how to draw on a page. The text data is not stored as paragraphs or words, but rather as characters that record information about specific locations on the page.
[0003] In practical use, it is necessary to convert image instructions in PDFs into spreadsheets and other electronic documents. Currently, open-source PDF parsing tools based on PDF (such as pdfplumber, py2pdf, or pdfminer) are used to parse PDF documents. However, these parsing tools can only extract fully formatted tables from PDFs. If the tables contain missing table lines, they cannot accurately parse the PDF document. Summary of the Invention
[0004] This invention provides a method, apparatus, electronic device, and storage medium for parsing PDF documents, in order to improve the parsing efficiency of PDF documents.
[0005] In a first aspect, embodiments of the present invention provide a method for parsing PDF documents, including:
[0006] Retrieve page objects from PDF documents;
[0007] The x-coordinate set and y-coordinate set are determined based on the endpoint coordinates of the line elements in the page object;
[0008] The string is determined based on the coordinates of the character elements in the page object;
[0009] The column identifier of a string is determined based on the set of its coordinates and x-coordinates.
[0010] The row identifier of the string is determined based on the set of coordinates and ordinates of the string;
[0011] Create a spreadsheet based on row and column labels.
[0012] Secondly, embodiments of the present invention also provide a PDF document parsing apparatus, comprising:
[0013] The page object retrieval module is used to retrieve page objects from PDF documents;
[0014] The coordinate set determination module is used to determine the x-coordinate set and y-coordinate set based on the endpoint coordinates of the line elements in the page object;
[0015] The string determination module is used to determine a string based on the coordinates of character elements in a page object;
[0016] The column identifier determination module is used to determine the column identifier of a string based on the set of the string's coordinates and x-coordinates;
[0017] The row identifier determination module is used to determine the row identifier of a string based on the set of coordinates and ordinates of the string.
[0018] The drawing module is used to draw spreadsheets based on row and column identifiers.
[0019] Thirdly, embodiments of the present invention also provide a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that the processor executes the program to implement the PDF document parsing method shown in the embodiments of this application.
[0020] Fourthly, embodiments of the present invention also provide a storage medium containing computer-executable instructions, which, when executed by a computer processor, are used to perform a PDF document parsing method as shown in the embodiments of this application.
[0021] The PDF document parsing method provided in this application embodiment obtains a page object from the PDF document; determines the x-coordinate set and y-coordinate set based on the endpoint coordinates of the line elements in the page object; determines the string based on the coordinates of the character elements in the page object; determines the column identifier of the string based on the string's coordinates and x-coordinate set; determines the row identifier of the string based on the string's coordinates and y-coordinate set; and draws a spreadsheet based on the row identifier and column identifier. Compared to the current problem of low PDF document parsing efficiency, the PDF document parsing method provided in this embodiment can process the line elements and character elements separately after parsing the PDF document, thereby quickly and accurately extracting the content from the PDF. Determining the x-coordinate set and y-coordinate set based on the endpoint coordinates of the line elements in the page object can determine the table area. Vertical lines of the table are drawn based on the x-coordinates in the x-coordinate set, and horizontal lines are drawn based on the y-coordinates in the y-coordinate set, thus determining the table lines to be drawn in the spreadsheet. The string located in the same row can be determined based on the coordinates of the character elements, and the column identifier of the string can be determined based on the string's coordinates and x-coordinate set. Combining the string's y-coordinate, the row identifier of the string in the spreadsheet can be accurately determined. Accurately extract strings from tables in PDFs into spreadsheets based on row and column identifiers, improving the parsing efficiency of tables in PDFs. Attached Figure Description
[0022] Figure 1 This is a flowchart of the PDF document parsing method in Embodiment 1 of the present invention;
[0023] Figure 2 This is a flowchart of the PDF document parsing method in Embodiment 2 of the present invention;
[0024] Figure 3 This is a schematic diagram of the PDF document parsing device in Embodiment 3 of the present invention;
[0025] Figure 4 This is a schematic diagram of the structure of the computer device in Embodiment 4 of the present invention. Detailed Implementation
[0026] The present invention will now be described in further detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and not intended to limit it. Furthermore, it should be noted that, for ease of description, the accompanying drawings show only the parts relevant to the present invention, and not all of the structures.
[0027] Example 1
[0028] Figure 1This is a flowchart of a PDF document parsing method provided in Embodiment 1 of the present invention. This embodiment is applicable to the parsing of PDF documents. The method can be executed by a computer device that performs PDF document parsing. The computer device can be a personal computer or a laptop computer, or it can be a terminal, including smartphones, tablets, etc. The method specifically includes the following steps:
[0029] Step 110: Obtain the page object from the PDF document.
[0030] Optionally, the PDF document can be identified using the pdfplunmer tool to obtain a page array. The page array consists of at least one page object, with each page object identifying a page in the PDF document. Each PDF object contains various elements, including line elements and character elements.
[0031] Line elements include the coordinates of their endpoints, which are the coordinates of the two endpoints of the line. Character elements include coordinates representing the coordinate range occupied by the character, and the character content. The coordinates representing the coordinate range occupied by the character can be the top-left (LU) coordinate and the bottom-right (RD) coordinate of the character. A character can be a single character. The top-left (LU) coordinate or the bottom-right (RD) coordinate can be used to represent the character's position.
[0032] Step 120: Determine the x-coordinate set and y-coordinate set based on the endpoint coordinates of the line elements in the page object.
[0033] The x-coordinate set includes the endpoint coordinates of all line elements in the page object. Endpoint coordinates can be represented by two-dimensional coordinates (x, y). X represents the x-coordinate of the endpoint, and y represents the y-coordinate of the endpoint. When the content of the corresponding PDF document is a table, after the lines constituting the table are recognized, the endpoint coordinates of the line elements are obtained, and these endpoint coordinates are split into x-coordinates and y-coordinates.
[0034] In a table document, lines include horizontal and vertical lines. Horizontal lines have the same y-coordinate at both endpoints, and vertical lines have the same x-coordinate at both endpoints. Therefore, to represent a horizontal line more concisely, the common y-coordinate of its endpoints can be used. Similarly, to represent a vertical line more concisely, the common x-coordinate of its endpoints can be used. Multiple x-coordinates can be obtained from the endpoint coordinates of line elements to represent multiple vertical lines, and multiple y-coordinates can be obtained to represent multiple horizontal lines.
[0035] Multiple x-coordinates form a x-coordinate set, which includes the x-coordinates of the endpoints of all line elements in the page object. This x-coordinate set is used to represent the x-coordinates of the vertical lines (i.e., the vertical lines) of the spreadsheet. If there are duplicate x-coordinates, they are removed.
[0036] Multiple y-coordinates form a y-coordinate set, which includes the y-coordinates of the endpoints of all straight line elements in the page object. This set is used to represent the y-coordinates of the horizontal table lines (i.e., screen breakage insurance) in the spreadsheet. If there are duplicate y-coordinates, they are removed.
[0037] After obtaining the x-coordinate set and the y-coordinate set, since tables sometimes contain shaded lines or, due to scanning or other reasons, two adjacent table borders appear—these are blurry lines inherent in PDF documents. To more accurately identify table lines and avoid misidentifying unnecessary lines, further, after determining the x-coordinate set and y-coordinate set based on the endpoint coordinates of the line elements in the page object in step 120, the following steps are also included:
[0038] If the difference between the first and second x-coordinates in the x-coordinate set is less than a preset distance threshold, then delete the first x-coordinate or delete the second x-coordinate; if the difference between the first and second y-coordinates in the y-coordinate set is less than a preset distance threshold, then delete the first y-coordinate or delete the second y-coordinate.
[0039] The preset distance threshold can be 0.5 pixels. In both the x-coordinate and y-coordinate sets, the difference between two adjacent coordinates (e.g., the first x-coordinate and the second x-coordinate, or the first y-coordinate and the second y-coordinate) is compared to see if it is less than the preset distance threshold. If it is less than the preset distance threshold, it means that the two adjacent coordinates are too close, resulting in a blurred line, and one of the coordinates is deleted.
[0040] In the above embodiments, by setting a distance threshold, blurry lines can be accurately identified and deleted, thereby improving the accuracy of PDF document recognition.
[0041] Step 130: Determine the string based on the coordinates of the character elements in the page object.
[0042] This function retrieves all character elements contained in a page object. Each character element has a top-left coordinate and a bottom-right coordinate. Since a string consists of multiple characters horizontally distributed on the same line, you can first divide the page into multiple lines based on all character elements. Within each line, the string is defined based on the spacing between character elements. You can use the top-left coordinate of the character elements to divide the lines uniformly, or you can use the bottom-right coordinate of the character elements to divide the lines uniformly.
[0043] In one implementation, step 130, determining the string based on the coordinates of the character elements in the page object, can be implemented as follows:
[0044] Step 131: Divide the character elements in the target page object according to the vertical axis to obtain multiple row groups.
[0045] The target page object is any page object in the PDF document. All character elements in the target page object are grouped according to the y-coordinate of the top-left coordinate of the character elements, with the character elements in each group having the same y-coordinate.
[0046] Step 132: For each row group, determine at least one string based on the spacing between adjacent characters.
[0047] Within each row group, the character order and spacing between characters are determined based on the x-coordinate values of the characters, thus defining the string.
[0048] In the above implementation, determining at least one string based on the spacing between adjacent characters can be implemented as follows:
[0049] Sort multiple character elements in a row group based on their top-left x-coordinate; in the sorted results, obtain the spacing between two adjacent character elements. If the spacing is less than a preset spacing threshold, then the two adjacent character elements are determined to belong to the same string.
[0050] Within a row group, multiple character elements are sorted according to their top-left x-coordinate, resulting in a sorted list. The sorting can be done in ascending order based on the x-coordinate values. In the sorted list, the distance between two adjacent character elements is calculated. If this distance is less than a preset distance threshold, the two adjacent character elements are considered to belong to the same string. This preset distance threshold can be 1 pixel. The process continues by comparing the next group of adjacent character elements; the character element with the larger x-coordinate in the previous group is then considered the character element with the smaller x-coordinate in the next group.
[0051] For example, two adjacent character elements are the first character element and the second character element. The distance between the first and second character elements is calculated. If this distance is less than a preset distance threshold, then the first and second character elements are determined to belong to the same string. The distance between the second and third character elements is then compared to see if it is less than the preset distance threshold. If the distance is less than the preset distance threshold, then the second and third character elements are determined to belong to the same string. If the distance is greater than the preset distance threshold, then the second and third character elements are determined to belong to different strings.
[0052] Similarly, it is determined whether the distance between the Nth character element and the (N+1)th character element is less than a preset distance threshold. If it is less, the Nth character element and the (N+1)th character element are considered to belong to the same string. Otherwise, if the distance is greater than the preset distance threshold, the Nth character element and the (N+1)th character element are considered not to belong to the same string, and the identified string is stored.
[0053] The above implementation method can quickly and accurately determine the string in row grouping based on the coordinates of the character elements, thereby improving the speed and accuracy of string determination.
[0054] Furthermore, the distance between two adjacent character elements can be calculated as follows. Assume the first and second character elements are adjacent, and the x-coordinate of the bottom right of the first character element is less than the x-coordinate of the top left of the second character element. Accordingly, obtaining the distance between two adjacent character elements includes:
[0055] Calculate the difference between the x-coordinate of the top left corner of the second character element and the x-coordinate of the bottom right corner of the first character element, and use this difference as the spacing between the first and second character elements; where the first and second character elements are two adjacent character elements, and the x-coordinate of the bottom right corner of the first character element is less than the x-coordinate of the top left corner of the second character element.
[0056] The above method can more accurately calculate the spacing between two adjacent characters, thereby improving the accuracy of string recognition.
[0057] Step 140: Determine the column identifier of the string based on the set of the string's coordinates and x-coordinates.
[0058] The string determined in step 130 can also be represented using top-left and bottom-right coordinates. The top-left coordinate of the string is the top-left coordinate of the first character element of the string, and the bottom-right coordinate of the string is the bottom-right coordinate of the last character element of the string.
[0059] The x-coordinate set is used to record the x-coordinates of the vertical table lines. The x-coordinates in the x-coordinate set can be sorted to obtain multiple columns in the table. Each column corresponds to two adjacent x-coordinates. Based on the x-coordinates of the string's top-left and bottom-right coordinates, along with the two x-coordinates representing the column, the column containing the string can be determined, i.e., the column identifier.
[0060] In one implementation, step 140, determining the column identifier of the string based on the set of the string's coordinates and x-coordinates, can be implemented in the following way:
[0061] Based on the first and last characters of the string, obtain the first horizontal coordinate interval occupied by the string; determine the second horizontal coordinate interval between adjacent vertical table lines based on the set of horizontal coordinates; if the first horizontal coordinate interval is within the second horizontal coordinate interval, use the column identifier corresponding to the second horizontal coordinate interval as the column identifier of the string represented by the first horizontal coordinate interval; if the first coordinate interval exceeds one second coordinate interval but does not exceed the range of multiple consecutive coordinate intervals, determine the column identifier of the merged cell based on the multiple column identifiers corresponding to the multiple consecutive second coordinate intervals that contain the first coordinate interval at the smallest; use the column identifier of the merged cell as the column identifier of the string.
[0062] After obtaining the first coordinate interval and multiple second coordinate intervals, the starting coordinates of the first coordinate interval are compared with the multiple second coordinate intervals respectively. The second coordinate interval containing the starting coordinates of the first coordinate interval is searched. If a certain second coordinate interval contains both the starting and ending coordinates of the first coordinate interval, the column identifier of the second coordinate interval is used as the column identifier of the string represented by the first coordinate interval.
[0063] The table can include individual cells or merged cells. If a string is located in a merged cell, it will be outside the second coordinate interval. In this case, we can obtain another second coordinate interval adjacent to the first one and determine whether the string is located within the horizontal coordinate interval formed by the two second coordinate intervals. If it is, the column identifier of the merged cell formed by the two second coordinate intervals is used as the column identifier of the string.
[0064] The above implementation method can accurately determine the first horizontal coordinate interval based on the horizontal coordinate of the character elements in the string, determine multiple second horizontal coordinate regions based on the horizontal coordinates, and quickly and accurately determine the first coordinate interval where the string is located based on the inclusion relationship between the second horizontal coordinate regions and the first horizontal coordinate regions, thereby determining the column identifier where the string is located and improving the accuracy of string positioning.
[0065] Step 150: Determine the row identifier of the string based on the set of coordinates and ordinates of the string.
[0066] The y-axis set contains the y-coordinates of the horizontal table lines in the PDF document. The strings obtained by grouping each row can be used as the strings in the same row. Furthermore, a relationship can be established between the strings and the y-coordinates, allowing the determination of the strings in the same row associated with that y-coordinate.
[0067] Furthermore, before step 150, determining the row identifier of the string based on the set of the string's coordinates and ordinates, the following steps are also included:
[0068] Identify distracting strings outside the table range based on the set of vertical coordinates; delete the distracting strings.
[0069] If the x-coordinate of the string is less than the minimum y-coordinate in the set of y-coordinates, then the string is determined to be outside the table range. Alternatively, if the x-coordinate of the string is greater than the maximum y-coordinate in the set of y-coordinates, then the string is determined to be outside the table range.
[0070] The above implementation method can filter out content outside the table, further improving the accuracy of table recognition.
[0071] Step 160: Draw a spreadsheet based on row and column labels.
[0072] After obtaining the row and column identifiers of the strings, determine the cell in the spreadsheet based on the row and column identifiers, and write the strings into that cell. If the same cell contains multiple strings, divide the multiple strings into rows, and write them sequentially according to the horizontal coordinate values in each group.
[0073] The PDF document parsing method provided in this application embodiment obtains a page object from the PDF document; determines the x-coordinate set and y-coordinate set based on the endpoint coordinates of the line elements in the page object; determines the string based on the coordinates of the character elements in the page object; determines the column identifier of the string based on the string's coordinates and x-coordinate set; determines the row identifier of the string based on the string's coordinates and y-coordinate set; and draws a spreadsheet based on the row identifier and column identifier. Compared to the current problem of low PDF document parsing efficiency, the PDF document parsing method provided in this embodiment can process the line elements and character elements separately after parsing the PDF document, thereby quickly and accurately extracting the content from the PDF. Determining the x-coordinate set and y-coordinate set based on the endpoint coordinates of the line elements in the page object can determine the table area. Vertical lines of the table are drawn based on the x-coordinates in the x-coordinate set, and horizontal lines are drawn based on the y-coordinates in the y-coordinate set, thus determining the table lines to be drawn in the spreadsheet. The string located in the same row can be determined based on the coordinates of the character elements, and the column identifier of the string can be determined based on the string's coordinates and x-coordinate set. Combining the string's y-coordinate, the row identifier of the string in the spreadsheet can be accurately determined. Accurately extract strings from tables in PDFs into spreadsheets based on row and column identifiers, improving the parsing efficiency of tables in PDFs.
[0074] Example 2
[0075] Figure 2 This is a flowchart illustrating a PDF document parsing method according to Embodiment 2 of the present invention. As a further explanation of the above embodiments, the method includes:
[0076] Step 201: Obtain the page object from the PDF document.
[0077] Step 202: Determine the x-coordinate set and y-coordinate set based on the endpoint coordinates of the line elements in the page object.
[0078] Furthermore, if the difference between the first and second x-coordinates in the x-coordinate set is less than a preset distance threshold, then the first x-coordinate or the second x-coordinate is deleted; if the difference between the first and second y-coordinates in the y-coordinate set is less than a preset distance threshold, then the first y-coordinate or the second y-coordinate is deleted.
[0079] Step 203: Divide the character elements in the target page object according to the vertical axis to obtain multiple row groups.
[0080] Step 204: For each row group, sort the multiple character elements in the row group according to the size of the horizontal coordinate in the upper left corner of the character element.
[0081] Step 205: In the sorting results, obtain the spacing between two adjacent character elements in turn.
[0082] Specifically, the difference between the x-coordinate of the top-left corner of the second character element and the x-coordinate of the bottom-right corner of the first character element is calculated, and this difference is used as the spacing between the first and second character elements. The first and second character elements are two adjacent character elements, and the x-coordinate of the bottom-right corner of the first character element is less than the x-coordinate of the top-left corner of the second character element.
[0083] Step 206: If the spacing is less than the preset spacing threshold, then the two adjacent character elements are determined to belong to the same string. If the spacing is greater than the preset spacing threshold, then the two adjacent character elements are determined to belong to different strings.
[0084] Step 207: Based on the first and last characters of the string, obtain the first horizontal coordinate interval occupied by the string.
[0085] Step 208: Determine the second horizontal coordinate interval between adjacent vertical table lines based on the horizontal coordinate set.
[0086] Step 209: If the first horizontal coordinate interval is located within the second horizontal coordinate interval, then the column identifier corresponding to the second horizontal coordinate interval shall be used as the column identifier of the string represented by the first horizontal coordinate interval.
[0087] Step 210: If the first coordinate interval exceeds a second coordinate interval but does not exceed the range of multiple consecutive coordinate intervals, then determine the column identifier of the merged cell based on the column identifiers corresponding to the multiple consecutive second coordinate intervals that contain the first coordinate interval at the smallest; use the column identifier of the merged cell as the column identifier of the string.
[0088] Furthermore, based on the set of vertical coordinates, identify the interfering strings outside the table range; delete the interfering strings.
[0089] Step 211: Determine the row identifier of the string based on the set of coordinates and ordinates of the string.
[0090] Step 212: Draw a spreadsheet based on row and column labels.
[0091] The PDF document parsing method provided in this application can process line elements and character elements separately after parsing the PDF document, thereby quickly and accurately extracting the content from the PDF. By determining the set of horizontal and vertical coordinates based on the endpoint coordinates of the line elements in the page object, the table area can be identified. Vertical lines of the table are drawn based on the horizontal coordinates in the horizontal coordinate set, and horizontal lines are drawn based on the vertical coordinates in the vertical coordinate set, thus determining the table lines to be drawn in the spreadsheet. The strings located in the same row can be identified based on the coordinates of the character elements, and the column identifier of the string can be determined based on the string's coordinates and the horizontal coordinate set. Combining the string's vertical coordinate, the row identifier of the string in the spreadsheet can be accurately determined. By accurately extracting the strings from the tables in the PDF into the spreadsheet based on the row and column identifiers, the parsing efficiency of tables in PDFs is improved.
[0092] Example 3
[0093] Figure 3 This is a schematic diagram of the PDF document parsing device provided in Embodiment 3 of the present invention. This embodiment is applicable to the parsing of PDF documents. The device can be implemented by a computer device that performs PDF document parsing. The computer device can be a personal computer or a laptop computer, or it can be a terminal, including smartphones, tablets, etc. The device specifically includes a page object acquisition module 310, a coordinate set determination module 320, a string determination module 330, a column identifier determination module 340, a row identifier determination module 350, and a drawing module 360.
[0094] Page object acquisition module 310 is used to acquire page objects from PDF documents;
[0095] The coordinate set determination module 320 is used to determine the horizontal coordinate set and the vertical coordinate set based on the endpoint coordinates of the line elements in the page object;
[0096] The string determination module 330 is used to determine the string based on the coordinates of the character elements in the page object;
[0097] The column identifier determination module 340 is used to determine the column identifier of a string based on the set of the string's coordinates and x-coordinates;
[0098] The row identifier determination module 350 is used to determine the row identifier of a string based on the set of coordinates and ordinates of the string.
[0099] The drawing module 360 is used to draw spreadsheets based on row and column identifiers.
[0100] Based on the above embodiments, the string determination module 330 is used for:
[0101] The character elements in the target page object are divided according to the vertical axis, resulting in multiple row groups;
[0102] For each row group, determine at least one string based on the spacing between adjacent characters.
[0103] Based on the above embodiments, the string determination module 330 is used for:
[0104] Sort multiple character elements in a row group according to the size of the x-coordinate in the top left corner of the character element;
[0105] In the sorting results, the spacing between two adjacent character elements is obtained sequentially;
[0106] If the spacing is less than the preset spacing threshold, then the two adjacent character elements are determined to belong to the same string.
[0107] Based on the above embodiments, the string determination module 330 is used for:
[0108] Calculate the difference between the x-coordinate of the top left corner of the second character element and the x-coordinate of the bottom right corner of the first character element, and use this difference as the spacing between the first and second character elements; where the first and second character elements are two adjacent character elements, and the x-coordinate of the bottom right corner of the first character element is less than the x-coordinate of the top left corner of the second character element.
[0109] Based on the above embodiments, the column identifier determination module 340 is used for:
[0110] Based on the first and last characters of the string, obtain the first horizontal coordinate interval occupied by the string;
[0111] Determine the second horizontal coordinate interval between adjacent vertical table lines based on the set of horizontal coordinates;
[0112] If the first horizontal coordinate interval is within the second horizontal coordinate interval, then the column identifier corresponding to the second horizontal coordinate interval is used as the column identifier of the string represented by the first horizontal coordinate interval.
[0113] If the first coordinate interval exceeds one second coordinate interval but does not exceed the range of multiple consecutive coordinate intervals, then the column identifier of the merged cell is determined according to the multiple column identifiers corresponding to the multiple consecutive second coordinate intervals that contain the first coordinate interval; the column identifier of the merged cell is used as the column identifier of the string.
[0114] Based on the above embodiments, an interference string processing module is also included, which is used for:
[0115] Determine the distracting strings outside the table range based on the set of vertical coordinates;
[0116] Remove distracting strings.
[0117] Based on the above embodiments, a coordinate set de-noising module is also included, which is used for:
[0118] After determining the x-coordinate and y-coordinate sets based on the endpoint coordinates of the line elements in the page object, the process also includes:
[0119] If the difference between the first and second horizontal coordinates in the set of horizontal coordinates is less than a preset distance threshold, then delete the first horizontal coordinate or delete the second horizontal coordinate.
[0120] If the difference between the first and second ordinates in the ordinate set is less than a preset distance threshold, then delete the first ordinate or delete the second ordinate.
[0121] The PDF document parsing apparatus provided in this application includes a page object acquisition module 310 for acquiring page objects from the PDF document; a coordinate set determination module 320 for determining a horizontal coordinate set and a vertical coordinate set based on the endpoint coordinates of line elements in the page object; a string determination module 330 for determining strings based on the coordinates of character elements in the page object; a column identifier determination module 340 for determining the column identifier of the string based on the coordinates and horizontal coordinate set of the string; a row identifier determination module 350 for determining the row identifier of the string based on the coordinates and vertical coordinate set of the string; and a drawing module 360 for drawing a spreadsheet based on the row identifier and column identifier. Compared to the current problem of low PDF document parsing efficiency, the PDF document parsing apparatus provided in this invention can process line elements and character elements separately after parsing the PDF document, thereby quickly and accurately extracting the content from the PDF. Determining the horizontal and vertical coordinate sets based on the endpoint coordinates of line elements in the page object allows for the determination of table areas. Vertical lines of the table are drawn based on the horizontal coordinates in the horizontal coordinate set, and horizontal lines are drawn based on the vertical coordinates in the vertical coordinate set, thereby determining the table lines to be drawn in the spreadsheet. By using the coordinates of a character element, we can determine the string located in the same row. Using the string's coordinates and x-coordinate, we can determine its column identifier. Combining this with the string's y-coordinate, we can accurately determine the string's row identifier in the spreadsheet. By accurately extracting strings from tables in PDFs into the spreadsheet using both row and column identifiers, we can improve the parsing efficiency of tables in PDFs.
[0122] The PDF document parsing device provided in this embodiment of the invention can execute the PDF document parsing method provided in any embodiment of the invention, and has the corresponding functional modules and beneficial effects of the method execution.
[0123] Example 4
[0124] Figure 4 This is a schematic diagram of the structure of a computer device provided in Embodiment 4 of the present invention, as shown below. Figure 4 As shown, the computer device includes a processor 40, a memory 41, an input device 42, and an output device 43; the number of processors 40 in the computer device can be one or more. Figure 4 Taking a processor 40 as an example; the processor 40, memory 41, input device 42, and output device 43 in a computer device can be connected via a bus or other means. Figure 4 Taking the example of a connection between China and Israel via a bus.
[0125] The memory 41, as a computer-readable storage medium, can be used to store software programs, computer-executable programs, and modules, such as the program instructions / modules corresponding to the PDF document parsing method in this embodiment of the invention (e.g., the page object acquisition module 310, coordinate set determination module 320, string determination module 330, column identifier determination module 340, row identifier determination module 350, and drawing module 360 in the PDF document parsing device). The processor 40 executes various functional applications and data processing of the computer device by running the software programs, instructions, and modules stored in the memory 41, thereby implementing the aforementioned PDF document parsing method.
[0126] The memory 41 may primarily include a program storage area and a data storage area. The program storage area may store the operating system and at least one application program required for a given function; the data storage area may store data created based on terminal usage. Furthermore, the memory 41 may include high-speed random access memory and non-volatile memory, such as at least one disk storage device, flash memory, or other non-volatile solid-state storage device. In some instances, the memory 41 may further include memory remotely located relative to the processor 40, which can be connected to the computer device via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
[0127] Input device 42 can be used to receive input digital or character information, and to generate key signal inputs related to user settings and function control of the computer device. Output device 43 may include display devices such as a display screen.
[0128] Example 5
[0129] Embodiment 5 of the present invention also provides a storage medium containing computer-executable instructions, which, when executed by a computer processor, are used to perform a PDF document parsing method, the method comprising:
[0130] Retrieve page objects from PDF documents;
[0131] The x-coordinate set and y-coordinate set are determined based on the endpoint coordinates of the line elements in the page object;
[0132] The string is determined based on the coordinates of the character elements in the page object;
[0133] The column identifier of a string is determined based on the set of its coordinates and x-coordinates.
[0134] The row identifier of the string is determined based on the set of coordinates and ordinates of the string;
[0135] Create a spreadsheet based on row and column labels.
[0136] Based on the above implementation method, determining the string according to the coordinates of character elements in the page object includes:
[0137] The character elements in the target page object are divided according to the vertical axis, resulting in multiple row groups;
[0138] For each row group, determine at least one string based on the spacing between adjacent characters.
[0139] Based on the above implementation method, at least one string is determined according to the spacing between adjacent characters, including:
[0140] Sort multiple character elements in a row group according to the size of the x-coordinate in the top left corner of the character element;
[0141] In the sorting results, the spacing between two adjacent character elements is obtained sequentially;
[0142] If the spacing is less than the preset spacing threshold, then the two adjacent character elements are determined to belong to the same string.
[0143] Based on the above implementation method, obtaining the spacing between two adjacent character elements includes:
[0144] Calculate the difference between the x-coordinate of the top left corner of the second character element and the x-coordinate of the bottom right corner of the first character element, and use this difference as the spacing between the first and second character elements; where the first and second character elements are two adjacent character elements, and the x-coordinate of the bottom right corner of the first character element is less than the x-coordinate of the top left corner of the second character element.
[0145] Based on the above implementation method, the column identifier of the string is determined according to the set of the string's coordinates and x-coordinates, including:
[0146] Based on the first and last characters of the string, obtain the first horizontal coordinate interval occupied by the string;
[0147] Determine the second horizontal coordinate interval between adjacent vertical table lines based on the set of horizontal coordinates;
[0148] If the first horizontal coordinate interval is within the second horizontal coordinate interval, then the column identifier corresponding to the second horizontal coordinate interval is used as the column identifier of the string represented by the first horizontal coordinate interval.
[0149] If the first coordinate interval exceeds one second coordinate interval but does not exceed the range of multiple consecutive coordinate intervals, then the column identifier of the merged cell is determined according to the multiple column identifiers corresponding to the multiple consecutive second coordinate intervals that contain the first coordinate interval; the column identifier of the merged cell is used as the column identifier of the string.
[0150] Based on the above implementation method, before determining the row identifier of the string according to the set of coordinates and ordinates of the string, the method further includes:
[0151] Determine the distracting strings outside the table range based on the set of vertical coordinates;
[0152] Remove distracting strings.
[0153] Based on the above implementation method, after determining the set of horizontal and vertical coordinates according to the endpoint coordinates of the line elements in the page object, the method further includes:
[0154] If the difference between the first and second horizontal coordinates in the set of horizontal coordinates is less than a preset distance threshold, then delete the first horizontal coordinate or delete the second horizontal coordinate.
[0155] If the difference between the first and second ordinates in the ordinate set is less than a preset distance threshold, then delete the first ordinate or delete the second ordinate.
[0156] Of course, the computer-executable instructions provided in the embodiments of the present invention are not limited to the above-described method operations, but can also perform related operations in the PDF document parsing method provided in any embodiment of the present invention.
[0157] Based on the above description of the implementation methods, those skilled in the art can clearly understand that the present invention can be implemented using software and necessary general-purpose hardware, and of course, it can also be implemented using hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as a computer floppy disk, read-only memory (ROM), random access memory (RAM), flash memory, hard disk, or optical disk, etc., including several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods of the various embodiments of the present invention.
[0158] It is worth noting that in the embodiments of the PDF document parsing device described above, the various units and modules included are only divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be achieved; in addition, the specific names of each functional unit are only for easy differentiation and are not used to limit the scope of protection of the present invention.
[0159] Note that the above description is merely a preferred embodiment of the present invention and the technical principles employed. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and various obvious changes, readjustments, and substitutions can be made without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in detail through the above embodiments, the present invention is not limited to the above embodiments, and may include many other equivalent embodiments without departing from the concept of the present invention, the scope of which is determined by the scope of the appended claims.
Claims
1. A method of parsing a PDF document, characterized by, The method comprises the following steps: acquiring a page object according to a PDF document; determining a horizontal coordinate set and a vertical coordinate set according to end point coordinates of a straight line element in the page object; wherein the horizontal coordinate set represents horizontal coordinates of vertical table lines, and if there are same horizontal coordinates in the horizontal coordinate set, the same horizontal coordinates are removed; the vertical coordinates of the vertical table lines, and if there are same vertical coordinates in the vertical coordinate set, the same vertical coordinates are removed; determining a character string according to coordinates of a character element in the page object; the step of determining the character string according to the coordinates of the character element in the page object comprises the following steps: dividing the character element in a target page object according to vertical coordinates to obtain a plurality of row groups; for each row group, sorting a plurality of character elements in the row group according to the size of horizontal coordinates in the upper left coordinates of the character elements; in the sorting result, the interval between two adjacent character elements is obtained in sequence; if the interval is less than a preset interval threshold, it is determined that the two adjacent character elements belong to the same character string; determining a column identifier of the character string according to the coordinates of the character string and the horizontal coordinate set; the step of determining the column identifier of the character string according to the coordinates of the character string and the horizontal coordinate set comprises the following step: determining the column in which the character string is located according to the horizontal coordinates in the upper left coordinates of the character string and the horizontal coordinates in the lower right coordinates of the character string and two horizontal coordinates representing columns; determining a row identifier of the character string according to the coordinates of the character string and the vertical coordinate set; drawing an electronic table according to the row identifier and the column identifier; the step of drawing the electronic table according to the row identifier and the column identifier comprises the following steps: determining a cell according to the row identifier and the column identifier, and writing a character string in the cell; wherein if the cell contains a plurality of character strings, the plurality of character strings are divided into groups according to rows respectively, so that in each group, the character strings are written in sequence according to the horizontal coordinate values.
2. The method of claim 1, wherein, the step of obtaining the interval between two adjacent character elements comprises the following steps: calculating the difference between the horizontal coordinate value of the upper left coordinates of a second character element and the horizontal coordinate value of the lower right coordinates of a first character element, and taking the difference as the interval between the first character element and the second character element; wherein the first character element and the second character element are two adjacent character elements, and the horizontal coordinate value of the lower right coordinates of the first character element is less than the horizontal coordinate value of the upper left coordinates of the second character element.
3. The method of claim 1, wherein, the step of determining the column identifier of the character string according to the coordinates of the character string and the horizontal coordinate set comprises the following steps: obtaining a first horizontal coordinate interval occupied by a character string according to the first character and the last character contained in the character string; obtaining a second horizontal coordinate interval between adjacent vertical table lines according to the horizontal coordinate set; if the first horizontal coordinate interval is located in the second horizontal coordinate interval, taking the column identifier corresponding to the second horizontal coordinate interval as the column identifier of the character string represented by the first horizontal coordinate interval. If the first coordinate interval exceeds one second coordinate interval and does not exceed a plurality of continuous coordinate intervals, a column identifier of a merged cell is determined according to a plurality of column identifiers corresponding to a plurality of continuous second coordinate intervals containing the first coordinate interval; and the column identifier of the merged cell is taken as a column identifier of the string.
4. The method of claim 1, wherein, Before determining the row identifier of the string according to the coordinates of the string and the set of vertical coordinates, the method further comprises: determining an interference string outside the table range according to the set of vertical coordinates; deleting the interference string.
5. The method of claim 1, wherein, After determining the set of horizontal coordinates and the set of vertical coordinates according to the endpoint coordinates of the straight line element in the page object, the method further comprises: if the difference between a first horizontal coordinate and a second horizontal coordinate in the set of horizontal coordinates is less than a preset distance threshold, deleting the first horizontal coordinate or deleting the second horizontal coordinate; if the difference between a first vertical coordinate and a second vertical coordinate in the set of vertical coordinates is less than a preset distance threshold, deleting the first vertical coordinate or deleting the second vertical coordinate.
6. An apparatus for parsing a PDF document, characterized by: The method comprises: a page object acquisition module, configured to acquire a page object according to a PDF document; a coordinate set determination module, configured to determine a set of horizontal coordinates and a set of vertical coordinates according to endpoint coordinates of a straight line element in the page object; wherein the set of horizontal coordinates represents horizontal coordinates of vertical table lines, and if there are identical horizontal coordinates in the set of horizontal coordinates, the identical horizontal coordinates are removed; the set of vertical coordinates represents vertical coordinates of horizontal table lines, and if there are identical vertical coordinates in the set of vertical coordinates, the identical vertical coordinates are removed; a string determination module, configured to determine a string according to coordinates of a character element in the page object; the string determination module is specifically configured to divide the character element in the target page object according to the set of vertical coordinates to obtain a plurality of row groups; for each row group, sort a plurality of character elements in the row group according to the size of the horizontal coordinate in the upper left coordinate of the character element; in the sorting result, sequentially obtain the interval between two adjacent character elements; if the interval is less than a preset interval threshold, determine that the two adjacent character elements belong to the same string; a column identifier determination module, configured to determine a column identifier of the string according to the coordinates of the string and the set of horizontal coordinates; the column identifier determination module is specifically configured to determine the column in which the string is located according to the horizontal coordinate in the upper left coordinate of the string and the horizontal coordinate in the lower right coordinate of the string, and two horizontal coordinates representing columns; a row identifier determination module, configured to determine a row identifier of the string according to the coordinates of the string and the set of vertical coordinates; a drawing module, configured to draw an electronic table according to the row identifier and the column identifier; the drawing module is specifically configured to determine a cell according to the row identifier and the column identifier, and write a string in the cell; wherein if the cell contains a plurality of strings, the plurality of strings are divided into row groups respectively, so that in each group, the strings are written in order according to the horizontal coordinate value.
7. A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor implements the method for analyzing the PDF document according to any one of claims 1-5 when executing the program.
8. A storage medium containing computer-executable instructions for performing a method of parsing a PDF document as recited in any of claims 1-5 when executed by a computer processor.
Citation Information
Patent Citations
Hidden table extraction method and device
CN107622041A
Method for parsing PDF table data and storage medium
CN108132916A