A document formatting processing method and system

By using classification and replacement and real-time positioning methods, the problem of incorrect placeholder positions in existing technologies has been solved, achieving accuracy and automated generation of document formatting.

CN123157666APending Publication Date: 2026-11-24SHANXIN SOFTWARE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611341154.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-01
Publication Date
2026-11-24

AI Technical Summary

Technical Problem

In existing document formatting methods, the strategy of uniform traversal and uniform replacement leads to incorrect placeholder positions and invalid references, resulting in the replacement content being inserted in the wrong position or the replacement failing.

Method used

A categorized replacement strategy is adopted, dividing placeholders into two categories: plain text strings and structured HTML, which are processed separately. Replacement is performed through real-time positioning and element-by-element conversion to avoid interference from index changes.

Benefits of technology

It improves the reliability of multi-placeholder template replacement, enabling one-click generation from the source Word document to the target DOCX document without manual intervention, ensuring replacement accuracy and format consistency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123157666A_ABST
    Figure CN123157666A_ABST
Patent Text Reader

Abstract

This application relates to the field of computer document automation processing technology, and in particular to a document formatting processing method and system, comprising: receiving a source Word document uploaded by a user and an optional target DOCX template; converting the source Word document into an HTML intermediate format file; loading the target DOCX template to obtain preset placeholder information in the target DOCX template; classifying the preset placeholder information according to the replacement content, namely, a first type of placeholder, plain text strings, and a second type of placeholder, structured HTML; replacing the first type of placeholder in the target DOCX template with plain text characters in the HTML intermediate format file to obtain an intermediate DOCX document. By adopting an ordered replacement strategy of "prioritizing the replacement of the first type of placeholder and replacing the second type of placeholder later," the interference of element index changes caused by the replacement of structured HTML content on the positioning of the first type of placeholder is fundamentally avoided, and the reliability of multi-placeholder template replacement is significantly improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer document automation processing technology, and in particular to a document formatting processing method and system. Background Technology

[0002] In scenarios where user-uploaded Word documents are used to populate preset templates to generate the final document, a typical processing pipeline follows: "Source document → Intermediate format (such as HTML) → Template placeholder replacement". This pipeline faces the following two core technical challenges: Traditional templates contain multiple placeholders, some of which are replaced with structured HTML. The replacement process inserts multiple new elements into the document, causing the document element index sequence to change dynamically, requiring the document to be formatted. However, existing document formatting processes use a unified traversal and unified replacement strategy. Changes in position indexes will cause subsequent placeholders that have not yet been replaced to have incorrect positions, invalidate the references of the paragraphs containing the placeholders, and cause the replacement content to be inserted in the wrong position or fail to be replaced. Summary of the Invention

[0003] This application provides a document formatting processing method and system that can effectively solve the problems of existing document formatting processing using a unified traversal and unified replacement strategy, where changed position indices will cause subsequent placeholders that have not yet been replaced to have incorrect positions, invalid references to paragraphs containing placeholders, and replacement content being inserted into incorrect positions or failing to be replaced.

[0004] Firstly, this application provides a document formatting method, including: Receives source Word documents uploaded by users and optional target DOCX templates; Convert the source Word document into an intermediate HTML file; Load the target DOCX template to obtain the preset placeholder information in the target DOCX template; The preset placeholder information is categorized according to the replacement content into two types: first-class placeholders for plain text strings and second-class placeholders for structured HTML. Replace the first type of placeholder in the target DOCX template with plain text characters in the HTML intermediate format file to obtain an intermediate DOCX document; Replace the second type of placeholders in the intermediate DOCX document with the structured HTML in the intermediate HTML format file to obtain the target DOCX document; Save the target DOCX document.

[0005] Preferably, the source Word document is converted into an HTML intermediate format file using a UTF-8 decoder and a GBK decoder in an adaptive decoding manner, including: The source Word document is decoded using the UTF-8 decoder to obtain an intermediate HTML file; If the UTF-8 decoder fails to decode the source Word document, it will adaptively switch to GBK decoder to decode the source Word document to obtain an HTML intermediate format file.

[0006] Preferably, replacing the first type of placeholder in the target DOCX template with plain text characters in the HTML intermediate format file includes: Traverse all paragraphs of the target DOCX template to obtain all first-class placeholders in the target DOCX template; All first-class placeholders in the target DOCX template are matched and replaced with plain text characters at the string level to obtain an intermediate DOCX document.

[0007] Preferably, replacing the second type of placeholder in the intermediate DOCX document with structured HTML in the intermediate HTML format file to obtain the target DOCX document includes: indexing and locating the paragraph to which the second type of placeholder belongs in the target DOCX template to obtain paragraph one; Save the style attributes of paragraph one; Delete paragraph 1 from the target DOCX template to obtain the replacement position of the structured HTML in the intermediate HTML file; Based on the replacement position of the structured HTML, the structured HTML is converted into DOCX elements element by element and inserted into the replacement position of the structured HTML to obtain the target paragraph; The target paragraph is set according to the style attributes of the saved paragraph one to complete the replacement of the second type of placeholder.

[0008] Preferably, converting the structured HTML element by element into DOCX elements and inserting them into the replacement positions of the structured HTML based on the replacement positions of the structured HTML includes: The elements in the structured HTML are mapped to corresponding DOCX elements according to their HTML tag types. The DOCX elements include heading paragraphs, body paragraphs, DOCX tables, and embedded image container tags. Detect and skip consecutive empty elements in DOCX element one to obtain DOCX element two; Each of the DOCX elements is processed by a hierarchical self-recognition conversion engine to extract the text content of the DOCX element, thus obtaining the text content M. The text content M is used to determine the level to which the text content belongs using a hierarchical recognition regular expression, so as to obtain the level N; Set the font name corresponding to the level N in the level recognition regular expression to the DOCX element to complete the element-by-element conversion of structured HTML into DOCX elements.

[0009] Preferably, the method for saving the target DOCX document includes: Traverse the target DOCX document to check for any unreplaced residual placeholders; A warning is issued if any unreplaced placeholders are present. If no unreplaced residual placeholder exists, save the target DOCX document to the specified path.

[0010] Preferred options also include: Inject CSS rules into the target DOCX document and embed images and fonts in Base64 format to obtain a preview of the DOCX document; Use a document conversion engine to convert preview DOCX documents into preview HTML documents.

[0011] Preferably, after receiving the source Word document uploaded by the user and the optional target DOCX template, the method further includes: When the source Word document contains an attachment file, a fault-tolerant multi-level extraction strategy is applied to the content of the attachment file. This fault-tolerant multi-level extraction strategy includes: Extract the plain text used for title recognition from the attached file; A four-layer progressive fallback strategy is used to extract the title list from the plain text; If the title list is successfully extracted, the title list will be formatted and output as a structured attachment title list; If the title list extraction fails, the first few characters of each paragraph are extracted as candidate titles and the structured attachment title list is output in a formatted manner. If the source Word document does not contain any attachment files, directly delete the placeholders related to the attachments in the target DOCX template.

[0012] Preferably, the step of extracting the title list from the plain text using a four-layer progressive fallback strategy includes: The large language model API is called to extract the title list from the title list using JSON structure. Determine whether the title list was successfully extracted and whether the array used to hold the extraction results is not empty; If the title list is successfully extracted and the array used to carry the extraction results is not empty, then a structured attachment title list is output in a formatted manner. If the title list is not successfully extracted and the array used to carry the extraction results is not empty, then the title list is extracted by pattern matching. Determine whether the title list has been successfully extracted based on pattern matching; If the title list pattern matching extraction is successful, a structured attachment title list will be output in a formatted manner. If the title list pattern matching extraction fails, then keyword retrieval and extraction will be performed on the title list; Determine whether the keyword extraction from the title list was successful; If the keyword detection and extraction of the title list is successful, a structured attachment title list will be output in a formatted manner. If the keyword detection and extraction of the title list is unsuccessful, then paragraph structure analysis and extraction will be performed on the title list; Determine whether the paragraph structure analysis and extraction of the title list was successful; If the paragraph structure analysis of the title list is successful, a structured attachment title list will be output in a formatted manner. If the paragraph structure analysis and extraction of the title list is unsuccessful, the first few characters of each paragraph will be extracted as candidate titles and a structured attachment title list will be output in a formatted manner.

[0013] Secondly, this application provides a document formatting processing system, comprising: A document conversion module, which is used to convert the source Word document into the HTML intermediate format file; A template loading module loads the target DOCX template to obtain preset placeholder information in the target DOCX template; An ordered placeholder replacement engine categorizes the preset placeholder information according to the replacement content, namely, a first type of placeholder for plain text strings and a second type of placeholder for structured HTML. A hierarchical self-identification conversion engine replaces the first type of placeholders in the target DOCX template with plain text characters in the HTML intermediate format file to obtain an intermediate DOCX document, and replaces the second type of placeholders in the intermediate DOCX document with structured HTML in the HTML intermediate format file to obtain the target DOCX document. The fault-tolerant attachment extraction module is used to perform a four-level progressive rollback strategy on the attachment content and output a formatted list of attachment titles.

[0014] Based on the above technical solution, this application has the following beneficial effects: By receiving a source Word document uploaded by a user and an optional target DOCX template, the source Word document is converted into an intermediate HTML file. The target DOCX template is loaded to obtain preset placeholder information. The preset placeholder information is categorized according to the replacement content: first-type placeholders are plain text strings, and second-type placeholders are structured HTML. The first-type placeholders in the target DOCX template are replaced with plain text characters from the intermediate HTML file to obtain an intermediate DOCX document. The second-type placeholders in the intermediate DOCX document are replaced with structured HTML from the intermediate HTML file to obtain the target DOCX document. Compared to the existing technology that uniformly replaces the first and second-type placeholders, this method uses an ordered replacement strategy of "first-type placeholders replaced first, second-type placeholders replaced later." This fundamentally avoids the interference of element index changes caused by structured HTML content replacement on the positioning of the first-type placeholders, significantly improving the reliability of multi-placeholder template replacement. The text content is classified into layers N by using a regular expression to determine the layer level N. The corresponding font name in the regular expression for layer N is set to the DOCX element, thus completing the element-by-element conversion of structured HTML to DOCX elements. This achieves one-click generation from source Word document to target DOCX document without manual intervention. By simultaneously extracting plain text for title recognition from the attachment file, a four-layer progressive fallback strategy is used to extract the title list from the plain text. If the title list is successfully extracted, the title list is formatted and output as a structured attachment title list. The four-layer fallback extraction strategy automatically degrades the system when there is a network anomaly or the AI ​​service is unavailable, ensuring that the core business process is not interrupted. This solves the problem in the existing technology that there is no automatic recognition and automatic formatting capability for the hierarchical system, and that it completely relies on manual adjustment according to the standard. Attached Figure Description

[0015] To more clearly illustrate the technical solution of this application, the drawings used in the embodiments will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0016] Figure 1 This is a flowchart of an embodiment of a document formatting processing method and system according to this application; Figure 2 This is a system block diagram of a document formatting processing method and system embodiment of this application. Detailed Implementation

[0017] To enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of this application.

[0018] like Figures 1-2 As shown, a document formatting processing method is provided, including: Receives source Word documents uploaded by users and optional target DOCX templates; In this application, a Word document refers to a text processing document file stored in .docx format. The .docx file is essentially a ZIP compressed package conforming to the ISO / IEC 29500 standard, which contains multiple Extensible Markup Language files, namely XML, image resources, style definitions, and metadata components. The source Word document refers to the initial document file uploaded by the user that is to be processed, and its format includes, but is not limited to, .docx. A target DOCX template is a document file stored in .docx format with a predefined layout structure and format definition. It may contain formatting information such as placeholders, style definitions, headers and footers, and section breaks, which are used as a format reference or data filling carrier for generating the target document. In terms of data format, DOCX templates also conform to the ISO / IEC 29500 standard ZIP compressed package, which contains multiple XML files describing the document structure and style.

[0019] Convert the source Word document into an intermediate HTML file; In this application, the HTML intermediate format file is a document file in Hypertext Markup Language format. As an intermediate data carrier in the document processing flow, the HTML intermediate format file is used to represent the hierarchical structure, text content and format information of the original document with a standardized tag structure. Its purpose is to decouple the document import and export process, so that subsequent document parsing, content extraction or format conversion operations can be carried out based on a unified, cross-platform intermediate representation. The source Word document is uploaded to MinIO, and the OnlyOffice conversion service is called to convert it into an HTML intermediate format file to facilitate subsequent document formatting processing.

[0020] Load the target DOCX template to obtain the preset placeholder information in the target DOCX template; The methods for loading the target DOCX template include loading via HTTP / HTTPS remote URL, loading via internal file service path, loading via local file system path, and loading via Base64 encoded data, but are not limited to the above methods. Loading the target DOCX template yields the preset placeholder information in the target DOCX template, which facilitates subsequent replacement on the target DOCX template based on the placeholder information. The preset placeholder information in the target DOCX template is divided into two categories: plain text placeholders and HTML content placeholders. Specifically, the file bytes in the target DOCX template are obtained through the OSS file service path and loaded as an XWPFDocument object. The preset placeholder information in the target DOCX template includes: (First type of placeholder) (First type of placeholder) (First type of placeholder) (First type of placeholder) (Second type placeholder) (Second type of placeholder).

[0021] The preset placeholder information is categorized according to the replacement content, into two categories: the first type of placeholders for plain text strings and the second type of placeholders for structured HTML. In this application, the preset placeholder information in the target ODCX template is divided into two categories: plain text strings and structured HTML. First type of placeholder: , , , ; Second type of placeholder: , .

[0022] Replace the first type of placeholder in the target DOCX template with plain text characters from the HTML intermediate format file to obtain the intermediate DOCX document; In this application, placeholders in the first category of the target DOCX template are used, for example: , , , The placeholders are replaced with plain text characters in the HTML intermediate format file. In the process of replacing the first type of placeholders in the target DOCX template with plain text characters in the HTML intermediate format file, the number of document elements is not changed. Only the text content in the target paragraph is modified, so that the first type of placeholders in the target DOCX template are replaced. The second type of placeholders in the target DOCX template are still retained in the target DOCX template, resulting in the corresponding intermediate DOCX document. Specifically, in the case of In the paragraph, replace the placeholders with the heading text, and set the corresponding font, font size, paragraph style, etc., for example: in the paragraph containing... Within the paragraph, replace the placeholder with the title text, set it to Founder Xiaobiao Song Simplified 22pt, and center it. In the paragraph, replace the date with the corresponding date and format from the source Word document, and adjust the font, size, and paragraph style accordingly. For example: in the paragraph containing... Replace the current date in the paragraph with "May 29, 2025" in the date format. The font and paragraph style should be: SimSun 16pt, centered. In the paragraph containing the contact information, replace it with the corresponding contact information and the corresponding text style, for example: in the paragraph containing In the paragraph, replace with contact information and the corresponding text style of the contact information as follows: Contact person: Zhang San, Contact number: 0531-12345678, Contact information font: Fangsong 16pt; In the paragraph, replace it with the list of attachment titles extracted by AI. After this stage is completed, all first-type placeholders have been replaced, and the number of document elements is the same as before the replacement. Second-type placeholders and The paragraph position remains valid.

[0023] Replace the second type of placeholders in the intermediate DOCX document with structured HTML from the intermediate HTML file to obtain the target DOCX document; In this application, the second type of placeholder in the intermediate DOCX document, for example: , The placeholders are replaced with structured HTML from the intermediate HTML file, thus completing the replacement of placeholders in the entire target DOCX template. The output is the target DOCX document, in which the first and second types of placeholders in the target DOCX document are replaced. These placeholders are replaced from the source Word document into the target DOCX document with standard style attributes. Specifically, for Perform the replacement, locating the paragraph containing the second type of placeholder in the intermediate DOCX document, for example, index i. Save the style attributes of the paragraph containing the second type of placeholder in the intermediate DOCX document, such as first-line indentation of 480 twips, justification, etc. Delete the paragraph containing the second type of placeholder in the intermediate DOCX document, reducing the total number of elements in the intermediate DOCX document by 1. Parse the attached HTML, inserting each element into the paragraph at index i. The newly inserted paragraph inherits the saved styles. Continue to the next second type of placeholder. Replace it at this time. The paragraph index may be due to The replacement was offset, but because this method uses a real-time positioning strategy of "locate first and then replace" instead of a pre-stored index, the document is re-scanned to locate the target paragraph before each second type of replacement, so it is not affected by the previous second type of replacement.

[0024] Save the target DOCX document.

[0025] In this application, after checking for any residual placeholders, the target DOCX document is saved as... The format will return the download URL.

[0026] Preferably, the source Word document is converted into an HTML intermediate format file using a UTF-8 decoder and a GBK decoder in an adaptive decoding manner, including: The source Word document is decoded using a UTF-8 decoder to obtain an intermediate HTML file. In this application, the source Word document in .docx format is decompressed as a ZIP archive. From the decompressed files, the core file word / document.xml is located. The binary content of the document.xml file is decoded into a readable XML text string. The XML text obtained in the previous step is parsed to extract all content, including text, tables, and images. Based on the parsed content and predefined rules, the corresponding HTML code is generated, and the Word paragraphs in the source Word document are translated into HTML paragraphs, thus completing the conversion of the source Word document into an HTML intermediate format file.

[0027] If the UTF-8 decoder fails to decode the source Word document, it will adaptively switch to GBK decoder to decode the source Word document to obtain an intermediate HTML file. In this application, the system detects whether an invalid UTF-8 byte sequence exists in the byte sequence output by the UTF-8 decoder after decoding the source Word document. If an invalid UTF-8 byte sequence exists, decoding is deemed a failure, and a decoding failure signal is generated. Based on the decoding failure signal, the current decoding mode is switched from UTF-8 decoding mode to GBK decoding mode. The source Word document is then decoded again in GBK decoding mode. After the source Word document is decoded by the GBK decoder, the system detects whether there are any unrecognizable byte sequences in the decoding result. If unrecognizable byte sequences exist, they are replaced with preset replacement characters, and the decoding operation continues. The system extracts the plain text character content and structured HTML information from the decoded source Word document, maps the plain text character content and structured HTML information to HTML tags and attributes, and generates an intermediate HTML file based on the mapping result. Mapping the plain text character content and structured HTML information to HTML tags and attributes includes mapping paragraph marks in the source Word document to HTML tags and attributes. Tags, heading tags in the source Word document, mapped to HTML to Tags, bold, italic, and underline marks in the source Word document are mapped to HTML respectively. , , Tags, table tags in the source Word document mapped to HTML , , Tags, etc., but not limited to the tags and attributes mentioned above.

[0028] Preferably, the first type of placeholder in the target DOCX template is replaced with plain text characters from the HTML intermediate format file, including: Iterate through all paragraphs of the target DOCX template to obtain all first-class placeholders in the target DOCX template; In this application, the entire collection of paragraphs in the target DOCX template is obtained through the DOCX document's object model access interface. For each paragraph, its text content is extracted, and all placeholder tags that conform to the naming rules of the first type of placeholder are identified using regular expression matching or string search. For example, for placeholders defined using double curly braces, i.e., " "and" The system identifies strings between "" as first-class placeholders and records the paragraph position of each first-class placeholder, the starting character index and the ending character index in the paragraph, as well as the name identifier of the placeholder; Replace all first-type placeholders in the target DOCX template with plain text characters through string-level matching and replacement, so as to obtain an intermediate DOCX document; For all first-type placeholders in the target DOCX template, extract the corresponding plain text content from the HTML intermediate format file, and replace the placeholder marks in the target DOCX template with the extracted corresponding plain text content from the HTML intermediate format file through precise string-level matching. It should be emphasized that the replacement operation in this application is direct string-level replacement, that is, only the character sequence in the paragraph text is modified during the replacement process, without adding or deleting paragraphs, changing the number and order of paragraphs, or modifying document structure attributes such as the number of rows and columns of the table; For different types of first-type placeholders, different replacement strategies and format settings are adopted: In the paragraph containing placeholder, the system extracts the plain text content of the title of the source Word document from the HTML intermediate format file, and replaces the placeholder with the plain text content of the title, and applies the preset title paragraph style to this paragraph. For example, if the content of a certain paragraph in the target DOCX template is " ", the system will replace the " " in this paragraph with the title text extracted from the HTML intermediate format file "Notice on Further Strengthening Information Construction", and set the title style for this paragraph. The specific style parameters are: the font is set to FZXiaoBiaoSong-B05S, the font size is set to 16pt, and the paragraph alignment is set to center alignment; In the paragraph containing placeholder, the system extracts the date information corresponding to the source Word document from the HTML intermediate format file, or obtains the date information from the current system time, and replaces the placeholder with the formatted date string, and applies the preset date paragraph style to this paragraph. For example, if the content of a certain paragraph in the target DOCX template is " ", the system will replace the placeholder with the current date, the date format is "May 29, 2025" format, that is, "yyyy-MM-dd" format, and set the date style for this paragraph. The specific style parameters are: the font is set to FangSong, the font size is set to 16pt, and the paragraph alignment is set to center alignment. In other implementation manners of this application, the date format may also be configured as other date formats such as "2025-05-29" or "05 / 29 / 2025" according to actual needs, which is not specifically limited in this application; In the paragraph containing placeholder, the system extracts the corresponding contact information from the HTML intermediate format file, and replaces the Replace placeholders with contact information strings and apply preset contact paragraph styles to the paragraph, for example: the content of a paragraph in the target DOCX template is " The system will The placeholder is replaced with contact information, specifically: "Contact Person: Zhang San, Contact Number: 0531-12345678". A contact style is set for this paragraph, with the following parameters: font set to SimSun, font size set to 16pt (i.e., size 3 font). In other embodiments of this application, the contact information may also include more dimensions such as contact person's name, position, email address, and mailing address. Those skilled in the art can configure this according to actual document requirements. In containing Within the placeholder paragraph, the system extracts a list of attachment titles identified and extracted by an AI (artificial intelligence) model from the HTML intermediate format file. The placeholder is replaced with the attachment title list string. For example, if the HTML intermediate format file contains an attachment title list specifically named "Attachments: 1. Information Technology Construction Project Application Form; 2. Project Feasibility Study Report; 3. Summary Table of Expert Review Opinions", the system will extract this attachment title list as plain text and replace the existing text in the target DOCX template. Placeholder; All first-type placeholders in the target DOCX template have been replaced with their corresponding plain text content. The number of document elements—including the total number of paragraphs, tables, and images—remains exactly the same as before the replacement, with no increase, decrease, or change. Meanwhile, the second-type placeholders, namely… placeholders and The paragraph positions where the placeholders are located remain valid, unmodified or unremoved, preserving the possibility and location markers for further processing of these placeholders in subsequent stages.

[0029] Preferably, the second type of placeholders in the intermediate DOCX document are replaced with structured HTML from the intermediate HTML format file to obtain the target DOCX document, including: The index locates the paragraph containing the second type of placeholder in the target DOCX template to obtain paragraph one; In this application, the system traverses all paragraphs of the intermediate DOCX document, uses regular expression matching or string search to identify all tags belonging to the second type of placeholder, and records the paragraph position of each second type of placeholder. For each second type of placeholder, the paragraph it belongs to is marked as paragraph one, and the index position of the paragraph in the document is obtained. The index positioning adopts a real-time scanning strategy, that is, before each replacement of the second type of placeholder, all paragraphs of the intermediate DOCX document are re-scanned to locate the current second type of placeholder to be processed, rather than relying on pre-stored static index values. Through the real-time scanning strategy, the index offset problem caused by the change in the number of document paragraphs due to the previous replacement operation is effectively avoided. Save the style properties of paragraph one; In this application, before deleting paragraph one, the system reads and saves all style attributes of paragraph one through the DOCX document's object model interface. These style attributes include at least: paragraph alignment (e.g., left alignment, center alignment, right alignment, justification); paragraph indentation attributes (e.g., first-line indent, left indent, right indent); paragraph spacing attributes (e.g., before-paragraph spacing, after-paragraph spacing, line spacing); paragraph outline level; and the style template identifier associated with the paragraph. For example, for... The system saves the style attributes of the paragraph containing the placeholder as follows: first line indent 480 twips, justified alignment, 0 spacing before paragraph, 0 spacing after paragraph, and single line spacing. The style attributes may also include more dimensions of formatting information such as paragraph border attributes, shading attributes, and pagination attributes. This application does not make specific limitations on this. Delete paragraph 1 from the target DOCX template to obtain the replacement location for the structured HTML in the intermediate HTML file; In this application, the system deletes the located paragraph 1 from the intermediate DOCX document, and the total number of elements in the intermediate DOCX document is reduced by 1 accordingly. After deletion, the position originally occupied by paragraph 1 becomes an empty space, which is the replacement position for subsequent insertion of structured HTML content. It should be emphasized that the deletion operation only removes paragraph 1 and its contained second type of placeholder mark, and does not affect the content and format of other paragraphs in the document. At the same time, due to the adoption of a real-time positioning strategy, the subsequent paragraph index changes caused by the deletion operation will be automatically corrected in the next scan. Based on the replacement positions of the structured HTML, convert each element of the structured HTML into a DOCX element and insert it into the replacement positions of the structured HTML to obtain the target paragraph. In this application, the system extracts the structured HTML fragment corresponding to the current second type placeholder from the HTML intermediate format file, and based on the determined replacement position of the structured HTML, converts the structured HTML fragment element by element into DOCX elements and inserts them into the replacement position.

[0030] Specifically, "element-by-element conversion to DOCX elements" includes: parsing structured HTML fragments to generate a corresponding Document Object Model (DOM) tree, where each node in the DOM tree corresponds to an HTML element; traversing the DOM tree and converting each HTML element into a corresponding DOCX element according to preset HTML-DOCX element mapping rules, for example: [HTML element name in HTML]. The tag is mapped to a DOCX paragraph element, in HTML. The tag is mapped to a DOCX table element, in HTML. The tag is mapped to a DOCX table row element, in HTML. The tag maps to DOCX table cell elements, in HTML The tag is mapped to DOCX bold format in HTML. The tag is mapped to DOCX italic format in HTML. The tag is mapped to DOCX underscore format, in HTML The tag is mapped to a DOCX image element, in HTML. The tags are mapped to DOCX hyperlink elements; the converted DOCX elements are then inserted sequentially into the designated replacement positions in the structured HTML according to the DOM tree hierarchy. For example: for For placeholder replacement, the system extracts the structured HTML fragment corresponding to the attachment details from the HTML intermediate format file. This fragment may contain multiple paragraphs, tables, images, and other elements. The system converts the HTML fragment element by element into DOCX elements and then inserts them into the replacement position, achieving a precise conversion from structured HTML to DOCX format. Furthermore, if the structured HTML fragment contains image data, the element-by-element transformation also includes: extracting image data from the HTML. The image data pointed to by the `src` attribute of the `` tag is decoded into a binary image stream, a DOCX image element is created, and the binary image stream is embedded into the DOCX image element. The width and height attributes in the tag are converted to the display width and height of the DOCX image element; Furthermore, if a structured HTML fragment contains list elements, such as and Tag, element-wise transformation also includes: Convert the tags to a DOCX bulleted list. The tags are converted into a DOCX numbered list, while preserving the list's hierarchical structure and indentation. Set the style attributes of the target paragraph according to the saved paragraph one to complete the replacement of the second type of placeholder; The system applies the saved paragraph style attributes to all newly inserted DOCX elements, ensuring that the newly inserted content maintains a consistent style with the original paragraphs. For multiple newly inserted paragraphs, the system applies the saved style attributes to each new paragraph individually, ensuring that all newly inserted paragraphs have a uniform paragraph style. For example: for After the placeholder is replaced, the system will apply the saved first-line indentation of 480 twips and justified alignment style attributes to each new paragraph, so that all newly inserted paragraphs maintain the same indentation and alignment as the paragraphs before deletion.

[0031] Furthermore, if the newly inserted element contains non-paragraph elements such as tables or images, the system will apply the paragraph-related properties from the saved style properties, such as indentation and alignment, to the container paragraph containing the table or image, while keeping the style properties of the table or image itself unchanged.

[0032] In specific application scenarios, the system scans intermediate DOCX documents and locates... The paragraph containing the placeholder is recorded with its index position as 'i', and its style attributes are saved: first-line indent 480 twips, justified alignment, 0 spacing before paragraph, and 0 spacing after paragraph. Next, the system deletes... The paragraph containing the placeholder is reduced by 1 from the total number of elements in the DOCX document. Then, the system parses and... The corresponding attachment details HTML fragment is converted element-by-element into a DOCX element and inserted at index i. Finally, the system applies the saved paragraph style attributes to all newly inserted paragraphs, completing the process. Placeholder replacement, then, for Replacement is performed because of the preceding sequence. The replacement operation may have added multiple paragraphs. The paragraph index may have shifted relative to its original value before replacement. However, because this application employs a real-time positioning strategy of "locating first and then replacing," that is, rescanning all paragraphs of the document to locate the target paragraph before each second type of placeholder replacement, rather than relying on pre-stored static index values, the index shift does not affect the positioning result, and the preceding second type of replacement operation will not affect the positioning result. The replacement will not cause any interference. The system will rescan the intermediate DOCX document and locate it in real time. The placeholder is located in the current paragraph, and the same steps as described above—saving the style, deleting the paragraph, converting the insertion, and restoring the style—are performed to complete the process. Replacement of placeholders.

[0033] Preferably, the method of converting structured HTML element by element into DOCX elements and inserting them into the replacement positions of the structured HTML, based on the replacement positions of the structured HTML, includes: The elements in the structured HTML are mapped to the corresponding DOCX element type according to the HTML tag type. DOCX element type includes heading paragraph, body paragraph, DOCX table, and embedded image container tag. In this application, the mapping rules for mapping elements in structured HTML to corresponding DOCX elements include, but are not limited to, the following mapping relationships: HTML heading tags to Mapped to DOCX heading paragraph elements; Paragraph tags in HTML Mapped to DOCX text paragraph elements; Use table tags in HTML , , These are mapped to DOCX table elements, DOCX table row elements, and DOCX table cell elements, respectively. Use image tags in HTML Map to a DOCX file and embed image elements; List tags in HTML , , Mapped to DOCX list paragraph elements; Use HTML quotation tags Map to DOCX reference paragraph elements; HTML code tags , Mapped to DOCX code block elements; Detect and skip consecutive empty elements in DOCX element one to obtain DOCX element two; Specifically, DOCX elements also include container tag elements, which are used to hold content with nested structures, including but not limited to: section containers containing multiple paragraphs, table containers containing multiple cells, and unordered list containers and ordered list containers containing multiple list items. Through the above mapping operation, the system converts all elements in the structured HTML into corresponding DOCX elements, providing basic data for subsequent hierarchical recognition and style setting. The system performs empty element detection on the DOCX elements. Specifically, for each DOCX element, the system checks whether the text content of the element is empty or contains only whitespace characters, including spaces, tabs, newlines, and carriage returns. If the current element is detected as empty, it further checks whether its next adjacent element is also empty. For consecutive empty elements, the system automatically skips these empty elements, i.e., it does not consider them as empty elements. The filtered DOCX element 2 is then incorporated into the subsequent conversion process. A sliding window is used to traverse the list of DOCX element 1, retaining each non-empty element. For empty elements, they are only retained if both the preceding and following elements are non-empty. If two or more empty elements appear consecutively, only one is retained, and the rest are skipped. Through the above processing, the problem of a large number of blank paragraphs in the generated DOCX document caused by redundant blank lines and line breaks in the source HTML is effectively avoided, improving the layout cleanliness of the final target DOCX document.

[0034] Each DOCX element 2 extracts its text content using a hierarchical self-recognition conversion engine to obtain the text content M; In this application, a hierarchical self-identification conversion engine is invoked to extract the plain text content of each DOCX element. Specifically, the hierarchical self-identification conversion engine obtains the text nodes of the element through the object model interface of the DOCX element, extracts the character sequences of all text nodes in the element, and concatenates the character sequences into complete text content M. For composite DOCX elements containing child elements, the hierarchical self-identification conversion engine recursively extracts the text content of all child elements and combines them into complete text content M according to the order of the child elements.

[0035] The text content M is used to determine the level to which the text content belongs using a hierarchical recognition regular expression, so as to obtain the level N; The extracted text content M is used as input, and a pre-defined set of hierarchical recognition regular expressions is applied to determine the hierarchical level of the text content M. The hierarchical recognition regular expression set contains multiple regular expression patterns corresponding to different hierarchical headings. The system sequentially matches the text content M against each regular expression pattern, and determines the hierarchical level N to which the text content belongs based on the matching results. The hierarchical recognition regular expression set uses the matching rules shown in Table 1.

[0036] Table 1. Regular Expressions for Hierarchical Recognition Set the font name corresponding to the level N in the level recognition regular expression to the DOCX element, and complete the element-by-element conversion of structured HTML to DOCX elements. In this application, based on the determined level N, the system searches for the corresponding mapped font name from the set of level recognition regular expressions and sets the mapped font name to the current DOCX element. Simultaneously, the system also applies the corresponding paragraph formatting settings to the DOCX element based on the preset paragraph style attributes corresponding to that level.

[0037] Specifically, in a preferred embodiment of this application, the paragraph formatting settings include the following rules: For DOCX elements classified as ordinary paragraphs, the system uniformly sets the following paragraph style attributes: font to SimSun_GB2312, font size to 16pt (i.e., size 3), line spacing to a fixed value of 28 points, and paragraph alignment to justified. It should be noted that the fixed line spacing of 28 points conforms to the specifications for body text line spacing in the national standard GB / T9704-2012 "Format of Official Documents of Party and Government Organs".

[0038] For heading paragraphs classified as level 1 to level 4, the system uniformly sets the following paragraph style attributes for DOCX elements: font settings are as shown in Table 1 corresponding to the mapped font, font size is set to 16pt (i.e., size 3 font), and paragraph alignment is set to center alignment.

[0039] Preferably, the target DOCX document is saved, including: Traverse the target DOCX document to check for any unreplaced residual placeholders; A warning will be issued if any unreplaced placeholders exist; If no unreplaced residual placeholders are found, save the target DOCX document to the specified path; In this application, the entire collection of paragraphs in the target DOCX document is obtained, each paragraph is traversed, the text content of the paragraph is extracted, and regular expression matching is used to check whether the text content contains placeholders. The regular expression is as follows: , used to match double curly braces " "Begins with double curly braces" Any string ending with "" matches all tags that conform to the placeholder naming rules. In addition to checking the main text paragraphs, the system also checks other document elements in the target DOCX document that may contain text content. These document elements include, but are not limited to: text in table cells, text in headers, text in footers, text in text boxes, text in footnotes, text in endnotes, and text in comments. By traversing and scanning all the above document elements, the system ensures that no placeholder remnants are missed. For each detected placeholder tag, the system records the following information: the placeholder's name, the document element type containing the placeholder, the specific location of the placeholder, and the complete tag string of the placeholder. The system counts the total number of unreplaced residual placeholders and generates a residual placeholder list. The list must include at least the name, location, and complete tag string of each residual placeholder. A warning message is generated based on the residual placeholder list, in the format: "Warning: Detected..." There are 10 unreplaced residual placeholders. Please check the following location: [Placeholder List], where N is the total number of residual placeholders. A warning message will be displayed on the user interface as a pop-up or notification bar. At the same time, the list of residual placeholders will be displayed on the user interface, marking the specific paragraph or document element position of each residual placeholder for users to quickly locate and manually check. Check if a file with the same name exists in the target file path. If a file with the same name exists, the system will generate a file conflict prompt message and ask the user through the user interface whether to overwrite the existing file, save as a new file, or cancel the save operation. The user interface provides a dialog box with at least the following options: "Overwrite", "Save As", and "Cancel" for the user to choose the appropriate processing method.

[0040] Preferred options also include: Inject CSS rules into the target DOCX document and embed images and fonts in Base64 format to obtain a preview of the DOCX document; Use a document conversion engine to convert preview DOCX documents into preview HTML documents; In this application, the system traverses all paragraphs and text runs of the target DOCX document, extracts the style attributes of each element, and converts the style attributes into equivalent CSS rules; the mapping relationship between style attributes and CSS properties is shown in Table 2:

[0041] Table 2: Mapping Relationship between Style Properties and CSS Properties Preferably, after receiving the source Word document uploaded by the user and the optional target DOCX template, the process also includes: When the source Word document contains attachments, a fault-tolerant multi-level extraction strategy is applied to the content of the attachments. This fault-tolerant multi-level extraction strategy includes: Extract the plain text used for title recognition from the attached file; A four-level progressive fallback strategy is used to extract the title list from plain text; If the title list is successfully extracted, the title list will be formatted and output as a structured attachment title list. If the title list extraction fails, the first few characters of each paragraph are extracted as candidate titles and the structured attachment title list is output in a formatted manner. In this application, after receiving a source Word document uploaded by a user, the system parses the source Word document, detects whether it contains attachments, parses the OpenXML structure of the source Word document, and checks whether there are embedded object reference tags in the word / document.xml file. For example, the embedded object is represented in OpenXML as... Tags or In the tag The sub-tag is marked to check whether there are embedded attachment files (such as .docx, .pdf, .xlsx, etc.) in the word / embeddings directory of the source Word document. If any of the above checks is positive, it is determined that the source Word document contains attachment files; if all the above checks are negative, it is determined that the source Word document does not contain attachment files. If the attachment file is a DOCX format file, all text node content in word / document.xml is extracted by parsing its OpenXML structure. If the attachment file is a PDF format file, all text content in the PDF document is extracted by a PDF parsing library (such as PDFBox or iText). If the attachment file is in other formats (such as TXT, RTF, XLSX, etc.), the plain text content is extracted according to the corresponding format parser. The system calls the four-layer progressive fallback strategy to extract the title list from the plain text extracted in sub-step S8021. The four-level progressive rollback strategy attempts four different extraction methods sequentially, from high precision to low precision and from strong intelligence to weak intelligence. If the current extraction method fails, it automatically rolls back to the next level until extraction succeeds or all levels have been tried. It iterates through all paragraphs of the target DOCX template, locating placeholders related to attachments. These placeholders include, but are not limited to: placeholders and For placeholders located related to attachments, the entire paragraph containing them is deleted to avoid leaving invalid empty paragraphs or placeholder marks in the final generated target DOCX document. If the target DOCX template contains chapter marks used to separate attachment sections (such as the "Attachment" chapter title paragraph), the system decides whether to delete the chapter marks simultaneously based on a preset cleanup strategy. In a preferred embodiment of this application, if the paragraph containing the chapter mark only contains the word "Attachment" and does not contain any other content, it is deleted; if the paragraph containing the chapter mark also contains other non-attachment related content, it is retained.

[0042] If the source Word document does not contain attachments, simply delete the attachment-related placeholders in the target DOCX template. In this application, when the detection result indicates that the source Word document does not contain attachment files, the system directly deletes the placeholders related to attachments in the target DOCX template, traverses all paragraphs of the target DOCX template, and locates the placeholders related to attachments. Placeholders related to attachments include, but are not limited to: placeholders and For placeholders located related to attachments, the entire paragraph containing them is deleted to avoid leaving invalid empty paragraphs or placeholder marks in the final generated target DOCX document. If the target DOCX template contains chapter marks used to separate attachment sections (such as the "Attachment" chapter title paragraph), the system decides whether to delete the chapter marks simultaneously based on a preset cleanup strategy. In a preferred embodiment of this application, if the paragraph containing the chapter mark only contains the word "Attachment" and does not contain any other content, it is deleted; if the paragraph containing the chapter mark also contains other non-attachment related content, it is retained.

[0043] Preferably, a four-level progressive fallback strategy is used to extract the title list from the plain text, including: The large language model API is used to extract the title list from the JSON structure. Check if the title list was successfully extracted and if the array used to hold the extracted results is not empty; If the title list is successfully extracted and the array used to hold the extraction results is not empty, then a structured attachment title list will be output in a formatted manner. If the title list is not successfully extracted and the array used to hold the extraction results is not empty, then the title list is extracted by pattern matching. Determine if the title list successfully matches the pattern and has been extracted. If the title list pattern is successfully extracted, a structured attachment title list will be output in a formatted manner. If pattern matching extraction of the title list fails, then keyword retrieval and extraction will be performed on the title list. Determine if keyword extraction from the title list was successful; If the keyword detection and extraction of the title list is successful, a structured list of attachment titles will be output in a formatted manner. If keyword detection and extraction from the title list fails, then paragraph structure analysis and extraction will be performed on the title list. Determine whether the paragraph structure analysis and extraction of the title list was successful; If the paragraph structure analysis of the heading list is successful, a structured attachment heading list will be output in a formatted manner. If the paragraph structure analysis and extraction of the title list is unsuccessful, then the first few characters of each paragraph will be extracted as candidate titles and the structured attachment title list will be output in a formatted manner. In this application, the system calls the Large Language Model (LLM) application programming interface to extract a structured title list from plain text. Specifically, the LLM structured extraction includes: constructing extraction prompts, which contain natural language descriptions of the title list extraction task, including but not limited to: instructing the LLM to identify all title entries from the plain text, outputting the title entries in JSON array format, and annotating the hierarchical relationship of each title. For example, the prompt is: "Please extract all titles from the following document content and output them in JSON array format. Each title contains two fields: 'title' and 'level', where level is represented by numbers 1-4 to indicate the level depth: [plain text content]". The system sends a request containing the prompts to the LLM API, receives the response returned by the LLM, parses the response, and determines whether the returned JSON data is valid and contains non-empty titles. The system uses an array to determine if the title list extraction was successful and if the array containing the extracted results is not empty. If the JSON parsing is successful and the length of the parsed title array is greater than 0, the first-level extraction is considered successful; otherwise, the first-level extraction is considered unsuccessful. If the title list extraction is successful and the array containing the extracted results is not empty, a structured attachment title list is output in a formatted manner. If the title list extraction is unsuccessful or the array containing the extracted results is empty, the system reverts to the second level and performs keyword search extraction. When the first-level large language model extraction fails, the system reverts to the pattern matching extraction method, which extracts the title list from the plain text using regular expressions. Pattern matching extraction includes: matching and extracting the plain text according to a preset set of regular expression patterns for the title level, summarizing the matching results, generating a candidate title list, sorting it according to the order of appearance in the document, and determining whether the title list was successfully extracted using pattern matching. Specifically, if the number of matched title entries is greater than the preset threshold, the second-level extraction is considered successful, for example, greater than or equal to 3; otherwise, the second-level extraction is considered unsuccessful. If the title list pattern matching extraction is successful, a structured attachment title list is formatted and output. If the title list pattern matching extraction is unsuccessful, the system reverts to the third level and performs keyword retrieval extraction. When the second-level pattern matching extraction fails, the system reverts to the keyword retrieval extraction method. The system identifies the title by detecting whether the plain text contains common title guide words. The system detects whether the plain text contains preset title guide words. The preset title guide words include, but are not limited to: “I.”, “II.”, “III.”, “(I)”, “(II)”, “1.”, “2.”, “(1)”, “(2)”, “Chapter 1”, “Chapter 2”, “Attachment”, “Appendix”, “Figure”, etc. For each detected title guide word, the entire line of text in the line containing the guide word or the text after the guide word and before the next newline character is extracted as a candidate title. All the extracted candidate titles are arranged into a candidate title list according to the order in which they appear in the document. The system then determines whether the title list keyword extraction is successful.Specifically, if the number of extracted candidate titles is greater than a preset threshold, for example, greater than or equal to 2, then the third-level extraction is considered successful; otherwise, the third-level extraction is considered a failure. If the title list keyword detection extraction is successful, a structured attachment title list is output in a formatted manner. If the title list keyword detection extraction is unsuccessful, the process reverts to the fourth level and performs paragraph structure analysis extraction. When the third-level keyword retrieval extraction fails, the system reverts to the paragraph structure analysis extraction method, identifying candidate titles by analyzing the paragraph structure and length features of the plain text. Paragraph structure analysis extraction includes: dividing the plain text into multiple paragraphs according to line breaks, counting the character length of each paragraph, calculating the average character length of all paragraphs, and selecting paragraphs with a length less than a preset length threshold, for example, those less than the average length. Half of the paragraphs are marked as short paragraphs. The beginning part of each short paragraph is extracted as a candidate title, for example, the first 20 characters of the paragraph. The success of the paragraph structure analysis extraction of the title list is determined. Specifically, if the number of extracted candidate titles is greater than a preset threshold, the fourth level of extraction is considered successful, for example, greater than or equal to 1; otherwise, the fourth level of extraction is considered a failure. If the paragraph structure analysis extraction of the title list is successful, a structured attachment title list is output in a formatted manner. If the paragraph structure analysis extraction of the title list is unsuccessful, the first few characters of each paragraph are extracted and formatted as a structured attachment title list, for example, the first 20 characters, as candidate titles. Regardless of which level of extraction of the title list is successful, the system will format the extraction result into a uniform structured attachment title list. The structured attachment title list is in JSON array format, and each element contains title text and hierarchy information.

[0044] A document formatting processing system, comprising: The document conversion module is used to convert source Word documents into HTML intermediate format files. In this application, the source Word document is uploaded to MinIO, and the OnlyOffice conversion service is called to convert it into an intermediate HTML file to facilitate subsequent document formatting.

[0045] The template loading module loads the target DOCX template to obtain the preset placeholder information in the target DOCX template; In this application, the methods for loading the target DOCX template include loading via HTTP / HTTPS remote URL, loading via internal file service path, loading via local file system path, loading via Base64 encoded data, etc., but are not limited to the above methods. Loading the target DOCX template obtains the preset placeholder information in the target DOCX template, which facilitates subsequent replacement on the target DOCX template based on the placeholder information. The preset placeholder information in the target DOCX template is divided into two categories: plain text placeholders and HTML content placeholders.

[0046] The ordered placeholder replacement engine categorizes the preset placeholder information according to the replacement content, into two categories: first-class placeholders for plain text strings and second-class placeholders for structured HTML. In this application, file bytes in the target DOCX template are obtained through the OSS file service path and loaded as an XWPFDocument object. The preset placeholder information in the target DOCX template includes: (First type of placeholder) (First type of placeholder) (First type of placeholder) (First type of placeholder) (Second type placeholder) (Second type of placeholder).

[0047] The hierarchical self-identification conversion engine replaces the first type of placeholders in the target DOCX template with plain text characters in the HTML intermediate format file to obtain the intermediate DOCX document, and replaces the second type of placeholders in the intermediate DOCX document with structured HTML in the HTML intermediate format file to obtain the target DOCX document. In this application, placeholders in the first category of the target DOCX template are used, for example: , , , The placeholders are replaced with plain text characters in the HTML intermediate format file. In the process of replacing the first type of placeholders in the target DOCX template with plain text characters in the HTML intermediate format file, the number of document elements is not changed. Only the text content in the target paragraph is modified, so that the first type of placeholders in the target DOCX template are replaced. The second type of placeholders in the target DOCX template are still retained in the target DOCX template, resulting in the corresponding intermediate DOCX document.

[0048] The fault-tolerant attachment extraction module is used to perform a four-level progressive rollback strategy on the attachment content and output a structured list of attachment titles in a formatted manner.

[0049] The embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described below do not represent all embodiments consistent with this application. They are merely examples of systems and methods consistent with some aspects of this application as detailed in the claims.

Claims

1. A document formatting processing method, characterized in that, include: Receives source Word documents uploaded by users and optional target DOCX templates; Convert the source Word document into an intermediate HTML file; Load the target DOCX template to obtain the preset placeholder information in the target DOCX template; The preset placeholder information is categorized according to the replacement content into two types: first-class placeholders for plain text strings and second-class placeholders for structured HTML. Replace the first type of placeholder in the target DOCX template with plain text characters in the HTML intermediate format file to obtain an intermediate DOCX document; Replace the second type of placeholders in the intermediate DOCX document with the structured HTML in the intermediate HTML file to obtain the target DOCX document; Save the target DOCX document.

2. The document formatting method according to claim 1, characterized in that, The source Word document is converted into an HTML intermediate format file using a UTF-8 decoder and a GBK decoder in an adaptive decoding manner, including: The source Word document is decoded using the UTF-8 decoder to obtain an intermediate HTML file; If the UTF-8 decoder fails to decode the source Word document, it will adaptively switch to GBK decoder to decode the source Word document to obtain an HTML intermediate format file.

3. The document formatting method according to claim 1, characterized in that, Replace the first type of placeholder in the target DOCX template with plain text characters in the HTML intermediate format file, including: Traverse all paragraphs of the target DOCX template to obtain all first-class placeholders in the target DOCX template; All first-class placeholders in the target DOCX template are matched and replaced with plain text characters at the string level to obtain an intermediate DOCX document.

4. The document formatting method according to claim 1, characterized in that, The second type of placeholders in the intermediate DOCX document are replaced with structured HTML from the intermediate HTML file to obtain the target DOCX document, including: The index is used to locate the paragraph to which the second type of placeholder belongs in the target DOCX template, thus obtaining paragraph one; Save the style attributes of paragraph one; Delete paragraph 1 from the target DOCX template to obtain the replacement position of the structured HTML in the intermediate HTML file; Based on the replacement position of the structured HTML, the structured HTML is converted into DOCX elements element by element and inserted into the replacement position of the structured HTML to obtain the target paragraph; The target paragraph is set according to the style attributes of the saved paragraph one to complete the replacement of the second type of placeholder.

5. The document formatting method according to claim 4, characterized in that, The process of converting the structured HTML element by element into DOCX elements and inserting them into the replacement positions of the structured HTML, based on the replacement positions of the structured HTML, includes: The elements in the structured HTML are mapped to corresponding DOCX elements according to their HTML tag types. The DOCX elements include heading paragraphs, body paragraphs, DOCX tables, and embedded image container tags. Detect and skip consecutive empty elements in DOCX element one to obtain DOCX element two; Each of the DOCX elements is processed by a hierarchical self-recognition conversion engine to extract the text content of the DOCX element, thus obtaining the text content M. The text content M is used to determine the level to which the text content belongs using a hierarchical recognition regular expression, so as to obtain the level N; Set the font name corresponding to the level N in the level recognition regular expression to the DOCX element to complete the element-by-element conversion of structured HTML into DOCX elements.

6. The document formatting method according to claim 5, characterized in that, The target DOCX document to be saved includes: Traverse the target DOCX document to check for any unreplaced residual placeholders; A warning is issued if any unreplaced placeholders are present. If no unreplaced residual placeholder exists, save the target DOCX document to the specified path.

7. The document formatting method according to claim 1, characterized in that, Also includes: Inject CSS rules into the target DOCX document and embed images and fonts in Base64 format to obtain a preview of the DOCX document; Use a document conversion engine to convert preview DOCX documents into preview HTML documents.

8. The document formatting method according to claim 1, characterized in that, After receiving the source Word document uploaded by the user and the optional target DOCX template, the method further includes: When the source Word document contains an attachment file, a fault-tolerant multi-level extraction strategy is applied to the content of the attachment file. This fault-tolerant multi-level extraction strategy includes: Extract the plain text used for title recognition from the attached file; A four-layer progressive fallback strategy is used to extract the title list from the plain text; If the title list is successfully extracted, the title list will be formatted and output as a structured attachment title list; If the title list extraction fails, the first few characters of each paragraph are extracted as candidate titles and the structured attachment title list is output in a formatted manner. If the source Word document does not contain any attachment files, directly delete the placeholders related to the attachments in the target DOCX template.

9. A document formatting processing method according to claim 8, characterized in that, The step of using a four-layer progressive fallback strategy to extract the title list from the plain text includes: The large language model API is called to extract the title list from the title list using JSON structure. Determine whether the title list was successfully extracted and whether the array used to hold the extraction results is not empty; If the title list is successfully extracted and the array used to carry the extraction results is not empty, then a structured attachment title list is output in a formatted manner. If the title list is not successfully extracted and the array used to carry the extraction results is not empty, then the title list is extracted by pattern matching. Determine whether the title list has been successfully extracted based on pattern matching; If the title list pattern matching extraction is successful, a structured attachment title list will be output in a formatted manner. If the title list pattern matching extraction fails, then keyword retrieval and extraction will be performed on the title list; Determine whether the keyword extraction from the title list was successful; If the keyword detection and extraction of the title list is successful, a structured attachment title list will be output in a formatted manner. If the keyword detection and extraction of the title list is unsuccessful, then paragraph structure analysis and extraction will be performed on the title list; Determine whether the paragraph structure analysis and extraction of the title list was successful; If the paragraph structure analysis of the title list is successful, a structured attachment title list will be output in a formatted manner. If the paragraph structure analysis and extraction of the title list is unsuccessful, the first few characters of each paragraph will be extracted as candidate titles and a structured attachment title list will be output in a formatted manner.

10. A document formatting processing system, applied to the method described in any one of claims 1-9, characterized in that, include: A document conversion module, which is used to convert the source Word document into the HTML intermediate format file; A template loading module loads the target DOCX template to obtain preset placeholder information in the target DOCX template; An ordered placeholder replacement engine categorizes the preset placeholder information according to the replacement content, namely, a first type of placeholder for plain text strings and a second type of placeholder for structured HTML. A hierarchical self-identification conversion engine replaces the first type of placeholders in the target DOCX template with plain text characters in the HTML intermediate format file to obtain an intermediate DOCX document, and replaces the second type of placeholders in the intermediate DOCX document with structured HTML in the HTML intermediate format file to obtain the target DOCX document. The fault-tolerant attachment extraction module is used to perform a four-level progressive rollback strategy on the attachment content and output a formatted list of attachment titles.