Method and device for converting rich text table into plain text table and electronic equipment
By using full-width tabs and algorithms to calculate width and height when converting rich text tables to plain text, the problem of table information is solved, and the integrity of table information is preserved in plain text and paper savings during printing is achieved.
Patent Information
- Application Number
- CN202510202970.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-24
- Publication Date
- 2025-06-03
AI Technical Summary
When converting a rich text table to plain text, the table information is completely lost, resulting in the table information being unable to correctly express the table information in the plain text form.
Through an algorithm, full-width tabs are used to correctly express table information in plain text, including calculating the number of characters of the table, determining the width and height of cells, and using table lines and cell content when generating a plain text table.
It realizes the complete preservation of table information in plain text, so that the table data can still be read and utilized later, while saving paper during printing.
Smart Images

Figure CN120087332A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software technology, and particularly to a method, device, and electronic device for converting a rich text table into a plain text table. Background Art
[0002] Common rich texts include word documents and hypertext web page formats. Rich texts can use table tags to construct common two-dimensional tables. However, when converting (saving as) plain text, the table information is completely lost, resulting in the complete loss of the original table information in plain text form, leaving only the information of each cell, which cannot be read and utilized later. Summary of the Invention
[0003] The present invention provides an algorithm for converting a rich text table into a plain text table, aiming to correctly express the table information in plain text form using full-width tab characters. This algorithm can be embedded in the algorithm for converting rich text into plain text to completely retain the table information for later reading and utilization.
[0004] To achieve the above object, an embodiment of the present invention provides a method for converting a rich text table into a plain text table, including the following steps:
[0005] S1: Obtain the character width of the entire table based on the width information of the rich text table and the width information of a single character in the rich text table; if the rich text table lacks width information, use a preset character width value as the character width of the entire table;
[0006] S2: Take the number of characters in the longest line in the content of each cell in the rich text table as the width of the corresponding cell; take the width of the cell with the largest width in each column of cells as the width weight of the column, and then obtain the width of each column according to the character width of the entire table, and round up the width of each column to an integer;
[0007] S3: Divide the number of characters in the content of each cell in the rich text table by the width of the column where the corresponding cell is located, and round up to obtain the height of the corresponding cell;
[0008] When each cell content contains a line break and the position of the line break is inconsistent with the soft line break position, the height of the corresponding cell is incremented by one;
[0009] Take the height of the cell with the highest height in each row of cells as the height of the row;
[0010] S5: Generate a plain text table using the width of each column and the height of each row of the table, and use table lines and cell content, where the table lines are plain text characters;
[0011] During the process of generating a plain text table, each line of characters is aligned by filling with full-width or half-width spaces.
[0012] Preferably, the preset numerical value of the character width is 62.
[0013] Preferably, it further includes the steps:
[0014] S4: Add up the heights of each row of the table to obtain the total height of the table;
[0015] Count the number of soft line breaks in each column; for the column with the most soft line breaks, increase the width by one, and for the column with the fewest soft line breaks, decrease the width by one;
[0016] Execute step S3, and add up the heights of each row of the table again to obtain the total height of the table;
[0017] If the total height of the table changes, repeat this step until the total height of the table no longer changes.
[0018] The present invention also provides a device for converting a rich text table into a plain text table, including the following modules:
[0019] Total width module: used to obtain the character width of the entire table according to the width information of the rich text table and the width information of a single character in the rich text table; if the rich text table lacks width information, use the preset numerical value of the character width as the character width of the entire table;
[0020] Initial column width acquisition module: used to take the number of characters in the longest line in the content of each cell in the rich text table as the width of the corresponding cell; take the width of the cell with the largest width among the cells in each column as the width weight of that column, and then obtain the width of each column according to the character width of the entire table, and round the width of each column to an integer;
[0021] Row height acquisition module: used to divide the number of characters in the content of each cell in the rich text table by the width of the column where the corresponding cell is located, and round up to obtain the height of the corresponding cell;
[0022] When each line break is included in the content of the cell and the position of the line break is inconsistent with the soft line break position, the height of the corresponding cell is increased by one;
[0023] Take the height of the cell with the highest height among the cells in each row as the height of that row;
[0024] Generation module: used to generate a plain text table according to the width of each column and the height of each row of the table, using table lines and the content of the cells, and the table lines are plain text characters;
[0025] During the process of generating a plain text table, each line of characters is aligned by filling full-width or half-width spaces.
[0026] Preferably, the preset numerical value of the character width is 62.
[0027] Preferably, it further includes modules:
[0028] Optimization module: used to add up the heights of each row of the table to obtain the total height of the table;
[0029] Count the number of soft line breaks in each column; for the column with the most soft line breaks, increase the width by one, and for the column with the fewest soft line breaks, decrease the width by one;
[0030] Execute the row height acquisition module and re-add up the heights of each row of the table to obtain the total height of the table;
[0031] If the total height of the table changes, repeat the execution of this module until the total height of the table no longer changes.
[0032] The present invention also provides an electronic device,
[0033] including:
[0034] A memory for storing a computer program;
[0035] A processor, when executing the program stored in the memory, implements the method steps described in any one of claims 1-3.
[0036] Beneficial effects:
[0037] 1. When the present invention converts a rich text table into a plain text table, it can completely retain the information of the table for later reading and utilization.
[0038] 2. In a further embodiment, by optimizing and adjusting the table, the table can be made more compact, saving paper during the printing process. Description of the Drawings
[0039] Figure 1 It is the effect diagram of the notepad program displaying a plain text table in the embodiment of the present invention; Detailed Embodiments
[0040] To make the objectives, features, and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the embodiments described below are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0041] The following explains some terms in the embodiments of the present invention to facilitate understanding by those skilled in the art.
[0042] (1) Character count width: Refers to the width of a table and its cells, measured by the number of full-width characters.
[0043] (2) Soft line break: A line break caused by the width limitation of a cell.
[0044] A method for converting a rich text table into a plain text table includes the following steps:
[0045] S1: Based on the width information of the rich text table and the width information of a single character in the rich text table, obtain the character count width of the entire table; if the rich text table lacks width information, use the preset character count width value as the character count width of the entire table; in this embodiment, the preset character count width value is 62.
[0046] The form of a rich text table is generally in the word format or the hypertext web page format. Among them, the rich text table in the word format contains the width information of the entire rich text table and the width information of a single character; the rich text table in the hypertext web page format generally does not contain width information. Therefore, use the preset character count width value as the character count width of the entire table. In this embodiment, the preset character count width value is 62 because 62 full-width characters are exactly the width of an A4 paper. Selecting this value can make full use of the paper and reduce waste when printing the table. The available character count width of all cells = the character count width of the entire table - the number of columns - 1.
[0047] S2: Take the number of characters in the longest line in the content of each cell in the rich text table as the width of the corresponding cell; take the width of the cell with the largest width in each column of cells as the width weight of that column, and then obtain the width of each column based on the character count width of the entire table, and round up the width of each column to an integer.
[0048] In a rich text table, due to the influence of line break characters in the content of a cell, there may be multiple lines of content. In this step, take the number of characters in the longest line as the width of the corresponding cell; take the width of the cell with the largest width in each column of cells as the width weight of that column. Given that the character count width of the entire table has been determined, further obtain the width of each column, and round up the width of each column to an integer.
[0049] S3: Divide the number of characters in the content of each cell in the rich text table by the width of the column where the corresponding cell is located, and round up to obtain the height of the corresponding cell.
[0050] When the content of a cell contains a line break and the position of the line break does not match the soft line break position, the height of the corresponding cell is incremented by one;
[0051] Take the height of the cell with the highest height among the cells in each row as the height of that row;
[0052] S4: Add up the heights of each row of the table to obtain the total height of the table;
[0053] Count the number of soft line breaks in each column; for the column with the most soft line breaks, the width is incremented by one, and for the column with the fewest soft line breaks, the width is decremented by one;
[0054] Execute step S3 and re-add up the heights of each row of the table to obtain the total height of the table;
[0055] If the total height of the table changes, repeat this step until the total height of the table no longer changes;
[0056] Through the adjustment of this step, the following effects can be achieved, so that when the table is printed on paper, it is as compact as possible to reduce paper waste:
[0057] First, the cell content is as full as possible, and there is as little content as possible in a very large cell; second, the height of each row is as small as possible, and the height of the whole row is not increased due to the height of a certain cell; third, the overall height (number of rows) of the table is as small as possible, so that the table appears compact and neat.
[0058] S5: Generate a plain text table using the width of each column and the height of each row of the table, with the table lines being plain text characters;
[0059] During the process of generating the plain text table, each row of characters is aligned by filling with full-width or half-width spaces.
[0060] In this embodiment, the plain text character table lines include: ┌, ┐, └, ┘, ├, ┤, ┬, ┴, ┼, ─, │. For example, constructing a plain text table example is as follows:
[0061] ┌────┬────┐
[0062] │Cell 1│Cell 2│
[0063] ├────┼────┤
[0064] │Cell 3│Cell 4│
[0065] └────┴────┘
[0066] The effect in the Notepad program is as Figure 1 shown.
[0067] The above are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present invention, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present invention.
Claims
1. A method for converting a rich text table into a plain text table, characterized in that: The following steps are involved: S1: according to the width information of the rich text table and the width information of a single character in the rich text table, the character width of the entire table is obtained; if the rich text table lacks width information, a preset character width value is used as the character width of the entire table; S2: The number of characters in the longest row of the content of each cell in the rich text table is taken as the width of the corresponding cell; the width of the cell with the largest width in each column is taken as the width weight of the column, and then the width of each column is obtained according to the number of characters in the entire table. The width of each column is an integer; S3: Divide the number of characters in the content of each cell in the rich text table by the width of the column where the corresponding cell is located, and round up to the integer value as the height of the corresponding cell; When the cell content contains a line break character and the position of the line break character is inconsistent with the soft line break position, the height of the corresponding cell is increased by one; Take the height of the highest cell in each row as the height of the row; S5: Generate a plain text table using table lines and cell contents according to the width of each column and the height of each row of the table, where the table lines are plain text characters; When generating a plain text table, each row of characters is aligned by filling it with full-width or half-width spaces.
2. The method for converting a rich text table into a plain text table according to claim 1, characterized in that: The default character width value is 62.
3. The method for converting a rich text table into a plain text table according to claim 1, characterized in that: Also includes the steps: S4: Add the height of each row of the table to get the total height of the table; Count the number of soft line breaks in each column; The column with the largest number of soft line breaks has its width increased by one, and the column with the smallest number of soft line breaks has its width reduced by one; Execute step S3, and re-add the height of each row of the table to obtain the total height of the table; If the total height of the table changes, repeat this step until the total height of the table no longer changes.
4. A device for converting a rich text table into a plain text table, characterized in that: Includes the following modules: Total width module: used to obtain the character width of the entire table based on the width information of the rich text table and the width information of a single character in the rich text table; if the rich text table lacks width information, the preset character width value is used as the character width of the entire table; Initial column width acquisition module: used to take the number of characters in the longest row of the content of each cell in the rich text table as the width of the corresponding cell; take the width of the cell with the largest width in each column as the width weight of the column, and then get the width of each column according to the number of characters in the entire table. The width of each column is an integer; Row height acquisition module: used to divide the number of characters in the content of each cell in the rich text table by the width of the column where the corresponding cell is located, and round up to the integer value as the height of the corresponding cell; When the cell content contains a line break character and the position of the line break character is inconsistent with the soft line break position, the height of the corresponding cell is increased by one; Take the height of the highest cell in each row as the height of the row; Generation module: used to generate a plain text table using table lines and cell contents according to the width of each column and the height of each row of the table, where the table lines are plain text characters; When generating a plain text table, each row of characters is aligned by filling it with full-width or half-width spaces.
5. The device for converting a rich text table into a plain text table according to claim 4, characterized in that: The default character width value is 62.
6. The device for converting a rich text table into a plain text table according to claim 4, characterized in that: Also includes modules: Optimization module: used to add the height of each row of the table to get the total height of the table; Count the number of soft line breaks in each column; The column with the largest number of soft wraps has its width increased by one, and the column with the smallest number of soft wraps has its width reduced by one; Execute the row height acquisition module and re-add the height of each row of the table to get the total height of the table; If the total height of the table changes, repeat this module until the total height of the table no longer changes.
7. An electronic device, characterized in that: include: Memory, used to store computer programs; A processor, for implementing the method steps described in any one of claims 1 to 3 when executing a program stored in a memory.