Method for adding bookmarks for scanning edition PDF documents in batches according to keywords

By using OCR recognition and regular expression to detect keywords, combined with the Fitz and pypdf libraries to automatically add bookmarks, the problem of low efficiency in batch bookmarking scanned PDF documents is solved, achieving efficient and automated processing.

CN121901157APending Publication Date: 2026-04-21黄德景
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-18
Publication Date
2026-04-21

AI Technical Summary

Technical Problem

Existing PDF document editing software or open-source projects lack methods for automatically adding bookmarks in batches, requiring scanned PDF documents to be processed manually, resulting in low efficiency.

Method used

The system uses OCR to recognize the character set in scanned PDF documents, regular expressions to detect keywords, generates a page number bookmark dictionary, and automatically adds bookmarks using the Fitz and Pypdf libraries. These features are encapsulated in different libraries to improve code reusability.

Benefits of technology

It enables automated batch bookmarking of scanned PDF documents, improving processing efficiency with a recognition accuracy of 92% and a speed of approximately 3 pages per second, effectively enhancing work efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121901157A_ABST
    Figure CN121901157A_ABST
Patent Text Reader

Abstract

The invention discloses a method for adding bookmarks for scanning edition PDF documents in batches according to keywords. The method comprises the steps that the method is achieved based on a python language; the method comprises the following steps: importing a folder containing a plurality of scanned PDF documents with similar structures, sequentially reading the PDF documents in sequence, and then extracting an image according to one third of each page to generate an image page set; carrying out OCR identification on the image pages, then detecting whether each page contains keywords in the keyword list by using a regular expression, if yes, adding the keyword page number dictionary, and if not, continuing to carry out OCR identification on the next page; duplicate removal processing is carried out on the condition that multiple keywords correspond to one bookmark, and it is guaranteed that the bookmark is only added to the page where the keywords appear initially; detecting whether the necessary bookmarks are not added or not; and renaming the PDF document according to an OCR recognition result. The method particularly lays a solid foundation for adding bookmarks to a large batch of scanning edition PDF documents with similar structures. The accuracy of automatic bookmark adding of the scanned PDF document is effectively improved, the working efficiency is improved, and the method can be further developed and utilized in the field of digital copying of paper medical records.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of natural language processing, OCR recognition, and Python language, and in particular to a method for batch adding bookmarks to scanned PDF documents based on keywords. Background Technology

[0002] According to the requirements of the National Health Commission's Medical Administration Notice

[2024] No. 96, all contents of the paper medical records of deaths and discharges without medical advice need to be scanned into PDF documents, bookmarks are added as required, and then uploaded to the designated platform. Hence, this invention was created.

[0003] Existing PDF document editing software or open-source projects lack methods for automatically adding bookmarks in batches. Scanned PDFs are essentially images, and their character sets can only be extracted using OCR methods. This approach involves processing the character sets, extracting keywords using regular expressions, generating a page number bookmark dictionary, and finally automatically adding bookmarks in batches. Summary of the Invention

[0004] This invention arose from the need to improve the efficiency of adding bookmarks to scanned PDF documents, thereby automating office work, and is achieved through the following technical methods: A method for batch adding bookmarks to scanned PDF documents based on keywords includes the following steps: A. Import a folder containing multiple PDF documents with similar structures, read all PDF documents in the folder, and then process them one by one; B. Read all pages of the PDF document and generate an image page set by taking screenshots of the top third of each page. C. Perform OCR recognition on the image page collection, and then use regular expressions to detect whether each page contains specific keywords. If it does, add the keyword page number key-value pair to the specific dictionary; otherwise, continue to perform OCR recognition on the next page. D. Add bookmarks and rename the PDF document as required; E. Remove duplicate bookmarks and monitor for missing bookmarks. If any are missing, prompt the user by renaming the PDF document. F. Proceed to the next PDF document. According to claim 1, a method for batch adding bookmarks to scanned PDF documents based on keywords is characterized in that, in step B, all pages of the PDF document are first read, a cropping area is defined, the DPI is adjusted, and the document is cropped; then it is saved as a two-dimensional matrix.

[0005] According to claim 1, a method for batch adding bookmarks to scanned PDF documents based on keywords is characterized in that, in step C, a keyword list is first defined, and each keyword is monitored to see if it appears on a certain page. If it appears, "keyword: page number" is added to a specific dictionary dict1, and the keyword is deleted. Then, the next page is checked.

[0006] According to claim 1, a method for batch adding bookmarks to scanned PDF documents based on keywords is characterized in that, if specific content in the PDF document is needed when renaming in step D, then specific areas and content need to be processed in steps B and C.

[0007] According to claim 1, a method for batch adding bookmarks to scanned PDF documents based on keywords is characterized in that, in step D, a dictionary dict2 with the keyword "bookmark" is first defined, and bookmarks are added in batches by combining dict1 and dict2.

[0008] According to claim 1, a method for batch adding bookmarks to scanned PDF documents based on keywords is characterized in that, in claim 6, the same bookmarks may be added to different pages, and there may be cases where bookmarks are missed. Therefore, the added bookmarks need to be processed and detected. If there are any missed bookmarks, they can only be added manually.

[0009] Specifically, to improve code reusability, the implementations of steps B, C, D, and E are all encapsulated in different libraries and called in the main program.

[0010] Specifically, the OCR recognition described above uses the Module module of the PaddleHub library, while reading PDF documents and adding bookmarks uses the Fitz and Pypdf libraries. Attached Figure Description

[0011] 0013 Figure 1 This is a schematic diagram of the process of the present invention; Figure 2 This is a diagram illustrating the keyword list; Figure 3 This is a diagram illustrating a dictionary of keywords and bookmarks, specifically dict2 mentioned above. Figure 4 Illustration of the data source for adding bookmarks and renaming files (within the red box); Figure 5 A diagram illustrating the results of file renaming. Detailed Implementation

[0012] To make the objectives, features, and processes of this invention clearer, a more detailed description will be provided below in conjunction with the embodiments and accompanying drawings: To better and more efficiently fulfill the requirements of the National Health Commission's Medical Administration Notice

[2024] No. 96, a method for batch adding bookmarks to scanned PDF documents based on keywords has been invented. Specific steps and features include: 1. A method for batch adding bookmarks to scanned PDF documents based on keywords, characterized in that the method includes: A. Import a folder containing multiple PDF documents with similar structures, read all PDF documents in the folder, and then process them one by one; B. Read the documents one by one, and then take screenshots of the top third of each page to generate an image page set; C. Perform OCR recognition on the image page collection, and then use regular expressions to detect whether each page contains specific keywords. If it does, add the keyword page number key-value pair to the specific dictionary; otherwise, continue to perform OCR recognition on the next page. D. Add bookmarks and rename the PDF document as required; E. Remove duplicate bookmarks and monitor for missing bookmarks. If any are missing, prompt the user by renaming the PDF document. F. Proceed to the next PDF document.

[0013] 2. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1 is characterized in that the entire process uses open source projects, does not use paid projects, and has high accuracy and fast implementation speed.

[0014] 3. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, in step B, all pages of the PDF document are first read, a cropping area is defined, the DPI is adjusted and cropped; then it is saved as a two-dimensional matrix.

[0015] 4. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, in step C, a keyword list is first defined, and each keyword is checked to see if it appears on a certain page. If it appears, "keyword: page number" is added to a specific dictionary dict1, and the keyword is deleted. Then, the next page is checked.

[0016] 5. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, if specific content in the PDF document is needed when renaming in step D, then specific areas and content need to be processed in steps B and C.

[0017] 6. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, in step D, a dictionary dict2 with the keyword "bookmark" is first defined, and bookmarks are added in batches by combining dict1 and dict2.

[0018] 7. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, in claim 6, the same bookmarks may be added to different pages, and there may be cases where bookmarks are missed. Therefore, the added bookmarks need to be processed and detected. If there are any missed bookmarks, they can only be added manually.

[0019] 8. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that the present invention is applicable to scenarios where the number of PDF documents to be processed is large and the structures are similar, such as in the digitization and reproduction of paper medical records and other archival materials.

[0020] Furthermore, in step A of this embodiment, the `listdir` method of the `os` library in Python is used to read all PDF document names for use in subsequent steps.

[0021] Furthermore, in step B of this embodiment, the open method of the Fitz library in Python is used to open the target PDF document, the page_count attribute of Fitz is called to count the total number of pages in the document to control the number of loops, the get_pixmap method of the Fitz library is used to extract the top third of each page, the frombytes method of the Image module of the PIL library is used to convert it into a data stream, and finally the array method of the NumPy library is called to store the data stream in a specific list.

[0022] Furthermore, the keywords to be detected in this embodiment are as follows: Figure 2 As shown. In step C, this keyword list is first created, and then the ch_pp-ocrv3 model from PaddleHub is used to perform OCR recognition on the results of step B one by one. The findall method of the re library is called to detect keywords in the keyword list one by one. If a keyword is found, "keyword: page number" is added to the specific dictionary dict1, and the keyword is deleted. Then, the detection continues to the next page. If no keyword is detected, the process jumps directly to the next page. In this step, the OCR recognition result of the first page is returned, and the contents of this page are needed when renaming in step D.

[0023] Furthermore, in step D of this embodiment, the PdfWriter class from the pypdf library is used to call its add_outline_item method to add bookmarks. The title parameter is set to dict2[key of dict1], which is the bookmark to be added; the page_number parameter is set to dict1[key of dict1], which retrieves the page number corresponding to the bookmark; and the parent parameter is set to None. After adding the bookmarks, they are saved to the current file path.

[0024] Furthermore, according to Figure 2 and Figure 3 As we can see, multiple keywords corresponding to the same bookmark indicate that the same bookmark might be added to different pages, necessitating a bookmark deduplication process. This process uses the `get_toc` method from the Fitz library to retrieve all bookmarks in the PDF document, deduplicates them, and then uses the `set_toc` method from the Fitz library to re-add the bookmarks. The result is as follows. Figure 4 As shown. Following step B, the first page of OCR recognition results is returned, and the `findall` method from the `re` library is used to detect specified content (such as...). Figure 4 (As shown in the red box), after organizing, rename as required, the result should look like this. Figure 5 As shown.

[0025] According to the statistical results of the implementation examples, the one-time success rate reached 80%. Among the results indicating missing bookmarks, 96% were already missing from the document, with recognition errors accounting for only 4%. Overall, the OCR recognition accuracy of this invention can reach over 92%, with a speed of approximately 3 pages per second, effectively improving work efficiency.

[0026] The above embodiments are merely examples of the present invention, and the specific scope of patent protection of the present invention is described in the claims.

Claims

1. A method for batch adding bookmarks to scanned PDF documents based on keywords, characterized in that, The method includes: A. Import a folder containing multiple PDF documents with similar structures, read all PDF documents in the folder, and then process them one by one; B. Read the documents one by one, and then take screenshots of the top third of each page to generate an image page set; C. Perform OCR recognition on the image page collection, and then use regular expressions to detect whether each page contains specific keywords. If it does, add the keyword page number key-value pair to the specific dictionary; otherwise, continue to perform OCR recognition on the next page. D. Add bookmarks and rename the PDF document as required; E. Remove duplicate bookmarks and monitor for missing bookmarks. If any are missing, prompt the user by renaming the PDF document. F. Proceed to the next PDF document.

2. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, The entire process uses open-source projects, without using paid projects, and boasts high accuracy and fast implementation speed.

3. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, In step B, first read all pages of the PDF document, define the cropping area, adjust the DPI and crop; then save it as a two-dimensional matrix.

4. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, In step C, a keyword list must first be defined, and each keyword must be monitored to see if it appears on a certain page. If it appears, "keyword: page number" is added to the specific dictionary dict1, and the keyword is deleted. Then, the monitoring continues to the next page.

5. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, If renaming in step D requires specific content from the PDF document, then specific areas and content need to be processed in steps B and C.

6. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, In step D, we first need to define a dictionary dict2 with the keyword "bookmark", and then combine dict1 and dict2 to add bookmarks in batches.

7. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, In claim 6, the same bookmarks may be added to different pages, and there may be cases where bookmarks are not added. Therefore, the added bookmarks need to be processed and detected. If there are any cases where bookmarks are not added, they can only be added manually.

8. The method for batch adding bookmarks to scanned PDF documents based on keywords according to claim 1, characterized in that, This invention is applicable to scenarios where there is a large volume of PDF documents that are similar in structure, such as those used in the digitization and reproduction of paper medical records and other archival materials.