Method and system for converting documents supporting image retention into lightweight markup language
By using the Spire component and MongoDB database to generate a unique identifier to replace the image position in the Office document, the problem of incomplete image conversion in the existing technology is solved, and efficient and accurate conversion of images in Office documents to Markdown documents is achieved.
Patent Information
- Application Number
- CN202311802017.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-26
- Publication Date
- 2025-10-03
- Estimated Expiration
- 2043-12-26
AI Technical Summary
When converting Office documents containing images into Markdown, existing technologies perform incomplete processing and have poor accuracy, resulting in loss of image information or incorrect formatting.
Use the Spire component to read Office documents, extract image information and upload it to the MongoDB database, generate a unique identifier, replace the image location with the image interface address, and ensure the integrity and accessibility of the image in the Markdown document.
It achieves efficient and accurate conversion of images in Office documents, ensures the integrity of the format, size and links of images in Markdown documents, and realizes seamless conversion between images and text.
Smart Images

Figure CN117829102B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of document information processing and conversion, and in particular relates to a method and system for converting a document that supports preserving images into a lightweight markup language. Background Art
[0002] Markdown is a lightweight markup language used to simplify text formatting and typesetting. Several technologies and tools exist for converting Office documents to Markdown, including open source tools, commercial software, and online services. These technologies and tools typically parse the structure and content of Office documents, extracting text, styles, and other metadata, and then generate corresponding Markdown documents according to Markdown syntax rules.
[0003] However, existing technologies may present challenges and limitations when converting Office documents containing images to Markdown. Image processing may require additional algorithms and processes to correctly embed images into Markdown documents and address image size and format conversion requirements. Furthermore, handling image metadata such as descriptions, annotations, and links is also a key consideration.
[0004] Through the above analysis, the problems and defects of the existing technology are: when converting Office documents containing pictures into lightweight markup language documents, the existing technology processes incomplete information and has poor accuracy in the conversion. Summary of the Invention
[0005] To overcome the problems existing in the related art, the embodiments disclosed in the present invention provide a method and system for converting a document that supports retaining images into a lightweight markup language, specifically a method for converting an office document into markdown that supports retaining images.
[0006] The technical solution is as follows: A method for converting a document that supports preserving images into a lightweight markup language includes the following steps:
[0007] S1, use Spire component to read the source Office document and extract the image information in the document;
[0008] S2 uploads the extracted image to the MongoDB database and generates a unique identifier based on the current date and time, clock sequence, and hardware MAC address. After the identifier is generated, it is recorded in the location information corresponding to the image. The document is located according to the location information, and the identifier and link information are concatenated to replace the image.
[0009] S3, write an interface to query MongoDB images, and use the interface to obtain the corresponding image content from the MongoDB database based on the image's unique identifier or attributes;
[0010] S4, traverse the image locations in the source Office document and replace the image locations with the addresses of the corresponding image interfaces;
[0011] S5 outputs the processed text containing the image link in the lightweight markup language Markdown format, generates a lightweight markup language Markdown document containing the retained image, replaces the image with a directly accessible http link, and is directly displayed by the markdown viewer.
[0012] In step S1, the Spire component reads the source Office document to parse the document content; the document content includes documents, tables and presentations; the image information extracted from the document includes: image file location, size, and name.
[0013] In step S1, image information in the document is extracted, including:
[0014] When reading, first read the document.xml file stream line by line, obtain the binary image data embedded in the document, and write the binary image data into the memory; read the image location information from relationships.xml and record it into the memory.
[0015] In step S2, generating a unique identifier includes: using a UUID or a unique identifier generation algorithm to generate a unique identifier, specifically including:
[0016] S2.1, format the timestamp into a 64-bit integer: Generate a 64-bit integer using the current date and time; the integer includes a high-order part and a low-order part;
[0017] S2.2, insert timestamp information into the high-order part: insert the 48 bits of the timestamp into the high-order part to make the generated UUID time ordered;
[0018] S2.3, inserting the clock sequence into the low-order portion: inserting 14 bits of the clock sequence into the low-order portion;
[0019] S2.4, inserting the MAC address into the low-order portion: inserting the 48 bits of the MAC address into the low-order portion;
[0020] S2.5, set UUID version and variant: set the version number and variant number in the final UUID to indicate the UUID version and variant used;
[0021] S2.6, final UUID generation: combine the high-order part and the low-order part to generate the final 128-bit UUID identifier.
[0022] In step S2, in the MongoDB database, the image file is selected to be stored as binary data, or the image is stored on the server, and the path or URL of the image is saved in the MongoDB database.
[0023] Furthermore, when file or image extraction fails or database connection fails, an exception handling mechanism is added to handle exceptions, and error log records and error message prompts are implemented.
[0024] In step S4, the address of the picture interface includes the text of the picture link.
[0025] Furthermore, the original image content is obtained and presented through the image interface address.
[0026] Furthermore, the method is applied to sharing and editing documents in blogs, forums or version control systems.
[0027] Another object of the present invention is to provide a system for converting a document into a lightweight markup language that supports image retention. The system is implemented by the method for converting a document into a lightweight markup language that supports image retention. The system includes:
[0028] The image information extraction module is used to use the Spire component to read the source Office document and extract the image information in the document;
[0029] The MongoDB database is used to upload the extracted image to the MongoDB database and generate a unique identifier based on the current date and time, clock sequence, and hardware MAC address. After the identifier is generated, it is recorded in the location information corresponding to the image. The document is located according to the location information, and the identifier and link information are concatenated to replace the image.
[0030] The interface for querying MongoDB images is used to write an interface for querying MongoDB images. Through the interface, the corresponding image content is obtained from the MongoDB database based on the image's unique identifier or attributes;
[0031] The image replacement module is used to traverse the image locations in the source Office document and replace the image locations with the addresses of the corresponding image interfaces;
[0032] The lightweight markup language Markdown document generation module is used to output the processed text containing image links in the lightweight markup language Markdown format, generate a lightweight markup language Markdown document containing retained images, replace the images with directly accessible http links, and be directly displayed by the markdown viewer.
[0033] Combining all the above technical solutions, the advantages and positive effects of the present invention are: the present invention can provide a more accurate and efficient method for processing Office documents containing images, and maintain their integrity and accuracy in the converted Markdown documents.
[0034] Compared to existing technologies, this invention effectively preserves the content and formatting of images when converting Office documents to Markdown. It can accurately embed images from Office documents into converted Markdown documents, ensuring the integrity of the images in terms of format, size, and links, and achieving seamless conversion between images and text. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure;
[0036] Figure 1 This is a flow chart of a method for converting a document into a lightweight markup language that supports preserving images, provided by an embodiment of the present invention;
[0037] Figure 2 This is a schematic diagram of a method for converting a document into a lightweight markup language that supports image retention, provided by an embodiment of the present invention;
[0038] Figure 3 Schematic diagram of a system for converting a document into a lightweight markup language that supports image retention, provided by an embodiment of the present invention;
[0039] In the figure: 1. Image information extraction module; 2. MongoDB database; 3. Interface for querying MongoDB images; 4. Image replacement module; 5. Lightweight markup language Markdown document generation module. DETAILED DESCRIPTION
[0040] To make the above-mentioned objects, features, and advantages of the present invention more readily apparent, specific embodiments of the present invention are described in detail below with reference to the accompanying drawings. The following description sets forth numerous specific details to facilitate a full understanding of the present invention. However, the present invention can be implemented in many other ways than those described herein, and those skilled in the art may make similar modifications without departing from the scope of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0041] The innovative point of the document to lightweight markup language conversion method and system provided by the embodiment of the present invention is that by converting images into hyperlinks, the problem of image loss when converting rich text Office format into plain text Markdown format is solved.
[0042] In an embodiment, the method for converting a document to a lightweight markup language (LML) with image retention provided by the present invention involves using the Spire component to read Office documents. Spire is a powerful Office file processing component that can be used to parse and manipulate various Office file formats, including documents, spreadsheets, and presentations.
[0043] like Figure 1 As shown, the method for converting a document to a lightweight markup language that supports preserving images provided by an embodiment of the present invention specifically includes:
[0044] S1, use Spire component to read the source Office document and extract the image information in the document;
[0045] Read the input stream, get all paragraphs and pictures according to the line break character, name the pictures with the obtained file names and store them on the local disk.
[0046] Among them, after reading the source Office document, the output result includes the document content;
[0047] After extracting the image information from the document, the output results include the image file location, size, and name;
[0048] It can be understood that office documents are actually an XML-based format, which uses the zip compression algorithm to combine many files (text, pictures, styles, etc.) into a container. Office documents contain multiple XML files. The document.xml file contains the main content of the document, including paragraphs, text, styles, and tags. It also contains relevant information about embedded pictures. The relationships.xml file describes the relationship between the various parts of the document, such as the association between text and pictures. When reading, first read the document.xml file stream line by line, obtain the binary picture data embedded in the document, and then write the binary picture data to the memory. Read the picture location information from relationships.xml and record it in the memory;
[0049] S2 uploads the extracted image to the MongoDB database and generates a unique identifier based on the current date and time, clock sequence, and hardware MAC address. After the identifier is generated, it is recorded in the location information corresponding to the image. The document is located according to the location information, and the identifier and link information are concatenated to replace the image.
[0050] As you can understand, MongoDB is a document-oriented NoSQL database suitable for storing and retrieving various types of data, including image files.
[0051] After uploading to the MongoDB database, the output results include a generated unique identifier for the image (generated using a UUID or unique identifier generation algorithm);
[0052] Exemplarily, using UUID to generate a unique identifier for an image specifically includes:
[0053] S2.1, format the timestamp as a 64-bit integer: Generate a 64-bit integer using the current date and time. This integer can be divided into two parts: a high-order part and a low-order part.
[0054] S2.2, insert timestamp information into the high-order portion: Insert some bits of the timestamp information into the high-order portion to ensure that the generated UUID is in chronological order. Specifically, insert 48 bits (6 bytes) of the timestamp into the corresponding position in the high-order portion.
[0055] S2.3, inserting clock sequence in low-order portion: insert 14 bits (2 bytes) of the clock sequence into corresponding positions of the low-order portion.
[0056] S2.4, inserting the MAC address into the low-order portion: insert the 48 bits (6 bytes) of the MAC address into the corresponding position of the low-order portion.
[0057] S2.5, Set UUID version and variant: Set the version number and variant number in the final UUID to indicate the UUID version and variant used.
[0058] S2.6, final UUID generation: combine the high-order part and the low-order part to generate the final 128-bit UUID identifier.
[0059] S3, write an interface to query MongoDB images, and use the interface to obtain the corresponding image content from the MongoDB database based on the image's unique identifier or attributes;
[0060] Among them, the image address is assembled through this interface, and the output result includes the image link;
[0061] S4, traverse the image locations in the source Office document and replace the image locations with the addresses of the corresponding image interfaces;
[0062] Use an object list to store the image's location in the file and replace it with the address of the corresponding image interface. This way, the image interface will be used to point to the actual image file in the lightweight markup language Markdown text. When replaced with the address of the corresponding image interface, the output result will contain text that links to the image.
[0063] S5 outputs the processed text containing the image link in the lightweight markup language Markdown format, generates a lightweight markup language Markdown document containing the retained image, replaces the image with a directly accessible http link, and is directly displayed by the markdown viewer.
[0064] For example, a method for converting documents to lightweight markup language that supports preserving images uses the Spire component to parse Office documents and extract image information. MongoDB is then used as an intermediate storage for the images, and the images are uploaded and a unique identifier is generated. By querying MongoDB's image API, the image content is retrieved and the image location in the document is replaced with the image API address.
[0065] As you can see, in this invention, uploading images to MongoDB and using the efficient query interface it provides ensures the relevance and accessibility of images and Markdown text. At the same time, using the Spire component as an Office file parsing tool can accurately read image information in documents and support various common Office file formats.
[0066] It can be seen from the above embodiments that the present invention first uses the Spire component to read the source Office document and extract the image information in the document. This method can ensure the complete extraction and accurate analysis of the image.
[0067] Secondly, by uploading the extracted images to a MongoDB database, this method can effectively store and manage a large number of image resources. MongoDB's features make storing and accessing images efficient and scalable, while also providing reliable data backup and recovery capabilities.
[0068] Next, we write an API to query MongoDB images. The image's unique identifier is used as the image API address in the Markdown text. This ensures that the image location is correctly replaced with the accessible image API address in the generated Markdown document. This preserves the image in the Markdown document and makes it easy to retrieve and present the original image content.
[0069] Ultimately, the generated Markdown document will retain images from the source Office document and be output in the standard Markdown format, ensuring good compatibility and readability. This allows users to easily convert Office documents to Markdown format and use them in various scenarios, such as sharing and editing documents in blogs, forums, or version control systems.
[0070] In summary, this method can efficiently and accurately convert Office documents containing images to Markdown format, preserving the images in the original document and providing a convenient way to access and manage these image resources. This will provide users with more flexibility and convenience, allowing them to better process and share documents containing images.
[0071] Example 2, as another embodiment of the present invention, Figure 2 As shown, the method for converting a document to a lightweight markup language that supports preserving images provided by an embodiment of the present invention includes:
[0072] Step 1: Read the Office document: Use the Spire component to read the source Office document and parse the document content.
[0073] Step 2: Extract image information from the document: Use the Spire component to extract image information from the document and obtain the location, size, and file name of each image.
[0074] Step 3: Upload the image to the MongoDB database: Upload the extracted image files to the MongoDB database, ensuring that each image has a unique identifier as its index in the database.
[0075] Step 4. Generate a unique identifier: Generate a unique identifier for each image. You can use a UUID (a 128-bit number consisting of a timestamp, a clock sequence, a network card MAC address, and a random number) or other unique identifier generation algorithm.
[0076] Exception handling: During the entire process from step 1 to step 4, you need to consider the handling of abnormal situations, such as file reading failure, image extraction failure, database connection failure, etc., and add appropriate exception handling mechanisms, such as error logging and error message prompts.
[0077] Step 5. Write a query interface: Write a query interface that passes in the image identifier as a parameter, searches for the corresponding image file in the MongoDB database, and returns it to the caller.
[0078] Step 6: Replace the image location with the image interface address: traverse the image locations in the document and replace the image locations with the address of the query interface so that the images can be correctly displayed in the lightweight markup language Markdown document.
[0079] Step 7: Output the lightweight markup language Markdown document: Output the lightweight markup language Markdown document with the image position replaced so that users can further process or share it.
[0080] In step 3 of the embodiment of the present invention, in terms of data storage details, it is necessary to consider how to store the image file in a MongoDB (a document-based database) database. You can choose to store the image file as binary data, or store the image on a server and save the image path or URL in MongoDB.
[0081] Example 3, as Figure 3 As shown, the system for converting a document to a lightweight markup language that supports preserving images provided by an embodiment of the present invention specifically includes:
[0082] The picture information extraction module 1 is used to use the Spire component to read the source Office document and extract the picture information in the document.
[0083] MongoDB database 2, used to store image information and image identifiers extracted from documents;
[0084] The interface 3 for querying MongoDB images is used to obtain the corresponding image content (including image name, binary information, image size, and format) from the MongoDB database based on the image's unique identifier or other attributes.
[0085] The image replacement module 4 is used to traverse the image locations in the source Office document and replace them with the addresses of the corresponding image interfaces. In this way, the image interfaces will be used to point to the real image files in the lightweight markup language Markdown text.
[0086] The lightweight markup language Markdown document generation module 5 outputs the processed text containing the image link in the lightweight markup language Markdown format to generate a lightweight markup language Markdown document containing the retained image.
[0087] The above description is only a preferred specific implementation method of the present invention, but the scope of protection of the present invention is not limited thereto. Any modifications, equivalent substitutions and improvements made by any technician familiar with this technical field within the technical scope disclosed by the present invention and within the spirit and principles of the present invention should be covered by the scope of protection of the present invention.
Claims
1. A method for converting a document into a lightweight markup language that supports preserving images, characterized in that: The method comprises the following steps: S1, use Spire component to read the source Office document and extract the image information in the document; S2 uploads the extracted image to the MongoDB database and generates a unique identifier based on the current date and time, clock sequence, and hardware MAC address. After the identifier is generated, it is recorded in the location information corresponding to the image. The document is located according to the location information, and the identifier and link information are concatenated to replace the image. S3, write an interface to query MongoDB images, and use the interface to obtain the corresponding image content from the MongoDB database based on the image's unique identifier or attributes; S4, traverse the image locations in the source Office document and replace the image locations with the addresses of the corresponding image interfaces; S5 outputs the processed text containing the image link in the lightweight markup language Markdown format, generates a lightweight markup language Markdown document containing the retained image, replaces the image with a directly accessible http link, and is directly displayed by the markdown viewer.
2. The method for converting a document to a lightweight markup language supporting image retention according to claim 1, wherein: In step S1, the Spire component reads the source Office document to parse the document content; the document content includes documents, tables and presentations; the image information extracted from the document includes: image file location, size, and name.
3. The method for converting a document to a lightweight markup language supporting image retention according to claim 1, wherein: In step S1, image information in the document is extracted, including: When reading, first read the document.xml file stream line by line, obtain the binary image data embedded in the document, and write the binary image data into the memory; read the image location information from relationships.xml and record it into the memory.
4. The method for converting a document to a lightweight markup language supporting image retention according to claim 1, wherein: In step S2, generating a unique identifier includes: using a UUID or a unique identifier generation algorithm to generate a unique identifier, specifically including: S2.1, format the timestamp into a 64-bit integer: Generate a 64-bit integer using the current date and time; the integer includes a high-order part and a low-order part; S2.2, insert timestamp information into the high-order part: insert the 48 bits of the timestamp into the high-order part to make the generated UUID time ordered; S2.3, inserting the clock sequence into the low-order portion: inserting 14 bits of the clock sequence into the low-order portion; S2.4, inserting the MAC address into the low-order portion: inserting the 48 bits of the MAC address into the low-order portion; S2.5, set UUID version and variant: set the version number and variant number in the final UUID to indicate the UUID version and variant used; S2.6, final UUID generation: combine the high-order part and the low-order part to generate the final 128-bit UUID identifier.
5. The method for converting a document to a lightweight markup language supporting image retention according to claim 1, wherein: In step S2, in the MongoDB database, the image file is selected to be stored as binary data, or the image is stored on the server, and the path or URL of the image is saved in the MongoDB database.
6. The method for converting a document to a lightweight markup language supporting image retention according to claim 5, wherein: When file or image extraction fails or database connection fails, an exception handling mechanism is added to handle exceptions, implement error logging and error message prompts.
7. The method for converting a document to a lightweight markup language supporting image retention according to claim 1, wherein: In step S4, the address of the picture interface includes the text of the picture link.
8. The method for converting a document to a lightweight markup language supporting image retention according to claim 7, wherein: Get and present the original image content through the image interface address.
9. The method for converting a document to a lightweight markup language supporting image retention according to claim 1, wherein: This method is applied to sharing and editing documents in blogs, forums or version control systems.
10. A system for converting documents to lightweight markup language that supports preserving images, characterized in that: The system is implemented by the method for converting a document supporting image retention into a lightweight markup language according to any one of claims 1 to 9, and the system includes: Image information extraction module (1), used to use Spire component to read the source Office document and extract the image information in the document; MongoDB database (2) is used to upload the extracted image to the MongoDB database, calculate based on the current date and time, clock sequence and hardware MAC address, and generate a unique identifier; after the identifier is generated, the identifier is recorded in the location information corresponding to the image, and the document is located according to the location information, and the identifier and link information are spliced to replace the image; An interface for querying MongoDB images (3) is used to write an interface for querying MongoDB images, and obtain corresponding image content from the MongoDB database based on the image's unique identifier or attribute through the interface; The picture replacement module (4) is used to traverse the picture locations in the source Office document and replace the picture locations with the addresses of the corresponding picture interfaces; The lightweight markup language Markdown document generation module (5) is used to output the processed text containing image links in the lightweight markup language Markdown format, generate a lightweight markup language Markdown document containing retained images, replace the images with directly accessible http links, and be directly displayed by the markdown viewer.
Citation Information
Patent Citations
General method for extracting document structural information
CN103399857A
Makdown document conversion method and device, equipment and storage medium
CN113935286A
Cited By
Method and device for automatically converting cross-format document into Markdown document and form enhancement method and system
CN119598980A
Methods, devices, and form enhancement methods and systems for automatically converting cross-format documents to Markdown documents
CN119598980B