table_process.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. This code is refer from: https://github.com/weizwx/html2docx/blob/master/htmldocx/h2d.py
  16. """
  17. import re
  18. import docx
  19. from docx import Document
  20. from bs4 import BeautifulSoup
  21. from html.parser import HTMLParser
  22. def get_table_rows(table_soup):
  23. table_row_selectors = [
  24. "table > tr",
  25. "table > thead > tr",
  26. "table > tbody > tr",
  27. "table > tfoot > tr",
  28. ]
  29. # If there's a header, body, footer or direct child tr tags, add row dimensions from there
  30. return table_soup.select(", ".join(table_row_selectors), recursive=False)
  31. def get_table_columns(row):
  32. # Get all columns for the specified row tag.
  33. return row.find_all(["th", "td"], recursive=False) if row else []
  34. def get_table_dimensions(table_soup):
  35. # Get rows for the table
  36. rows = get_table_rows(table_soup)
  37. # Table is either empty or has non-direct children between table and tr tags
  38. # Thus the row dimensions and column dimensions are assumed to be 0
  39. cols = get_table_columns(rows[0]) if rows else []
  40. # Add colspan calculation column number
  41. col_count = 0
  42. for col in cols:
  43. colspan = col.attrs.get("colspan", 1)
  44. col_count += int(colspan)
  45. return rows, col_count
  46. def get_cell_html(soup):
  47. # Returns string of td element with opening and closing <td> tags removed
  48. # Cannot use find_all as it only finds element tags and does not find text which
  49. # is not inside an element
  50. return " ".join([str(i) for i in soup.contents])
  51. def delete_paragraph(paragraph):
  52. # https://github.com/python-openxml/python-docx/issues/33#issuecomment-77661907
  53. p = paragraph._element
  54. p.getparent().remove(p)
  55. p._p = p._element = None
  56. def remove_whitespace(string, leading=False, trailing=False):
  57. """Remove white space from a string.
  58. Args:
  59. string(str): The string to remove white space from.
  60. leading(bool, optional): Remove leading new lines when True.
  61. trailing(bool, optional): Remove trailing new lines when False.
  62. Returns:
  63. str: The input string with new line characters removed and white space squashed.
  64. Examples:
  65. Single or multiple new line characters are replaced with space.
  66. >>> remove_whitespace("abc\\ndef")
  67. 'abc def'
  68. >>> remove_whitespace("abc\\n\\n\\ndef")
  69. 'abc def'
  70. New line characters surrounded by white space are replaced with a single space.
  71. >>> remove_whitespace("abc \\n \\n \\n def")
  72. 'abc def'
  73. >>> remove_whitespace("abc \\n \\n \\n def")
  74. 'abc def'
  75. Leading and trailing new lines are replaced with a single space.
  76. >>> remove_whitespace("\\nabc")
  77. ' abc'
  78. >>> remove_whitespace(" \\n abc")
  79. ' abc'
  80. >>> remove_whitespace("abc\\n")
  81. 'abc '
  82. >>> remove_whitespace("abc \\n ")
  83. 'abc '
  84. Use ``leading=True`` to remove leading new line characters, including any surrounding
  85. white space:
  86. >>> remove_whitespace("\\nabc", leading=True)
  87. 'abc'
  88. >>> remove_whitespace(" \\n abc", leading=True)
  89. 'abc'
  90. Use ``trailing=True`` to remove trailing new line characters, including any surrounding
  91. white space:
  92. >>> remove_whitespace("abc \\n ", trailing=True)
  93. 'abc'
  94. """
  95. # Remove any leading new line characters along with any surrounding white space
  96. if leading:
  97. string = re.sub(r"^\s*\n+\s*", "", string)
  98. # Remove any trailing new line characters along with any surrounding white space
  99. if trailing:
  100. string = re.sub(r"\s*\n+\s*$", "", string)
  101. # Replace new line characters and absorb any surrounding space.
  102. string = re.sub(r"\s*\n\s*", " ", string)
  103. # TODO need some way to get rid of extra spaces in e.g. text <span> </span> text
  104. return re.sub(r"\s+", " ", string)
  105. font_styles = {
  106. "b": "bold",
  107. "strong": "bold",
  108. "em": "italic",
  109. "i": "italic",
  110. "u": "underline",
  111. "s": "strike",
  112. "sup": "superscript",
  113. "sub": "subscript",
  114. "th": "bold",
  115. }
  116. font_names = {
  117. "code": "Courier",
  118. "pre": "Courier",
  119. }
  120. class HtmlToDocx(HTMLParser):
  121. def __init__(self):
  122. super().__init__()
  123. self.options = {
  124. "fix-html": True,
  125. "images": True,
  126. "tables": True,
  127. "styles": True,
  128. }
  129. self.table_row_selectors = [
  130. "table > tr",
  131. "table > thead > tr",
  132. "table > tbody > tr",
  133. "table > tfoot > tr",
  134. ]
  135. self.table_style = None
  136. self.paragraph_style = None
  137. def set_initial_attrs(self, document=None):
  138. self.tags = {
  139. "span": [],
  140. "list": [],
  141. }
  142. if document:
  143. self.doc = document
  144. else:
  145. self.doc = Document()
  146. self.bs = self.options["fix-html"] # whether or not to clean with BeautifulSoup
  147. self.document = self.doc
  148. self.include_tables = True # TODO add this option back in?
  149. self.include_images = self.options["images"]
  150. self.include_styles = self.options["styles"]
  151. self.paragraph = None
  152. self.skip = False
  153. self.skip_tag = None
  154. self.instances_to_skip = 0
  155. def copy_settings_from(self, other):
  156. """Copy settings from another instance of HtmlToDocx"""
  157. self.table_style = other.table_style
  158. self.paragraph_style = other.paragraph_style
  159. def ignore_nested_tables(self, tables_soup):
  160. """
  161. Returns array containing only the highest level tables
  162. Operates on the assumption that bs4 returns child elements immediately after
  163. the parent element in `find_all`. If this changes in the future, this method will need to be updated
  164. :return:
  165. """
  166. new_tables = []
  167. nest = 0
  168. for table in tables_soup:
  169. if nest:
  170. nest -= 1
  171. continue
  172. new_tables.append(table)
  173. nest = len(table.find_all("table"))
  174. return new_tables
  175. def get_tables(self):
  176. if not hasattr(self, "soup"):
  177. self.include_tables = False
  178. return
  179. # find other way to do it, or require this dependency?
  180. self.tables = self.ignore_nested_tables(self.soup.find_all("table"))
  181. self.table_no = 0
  182. def run_process(self, html):
  183. if self.bs and BeautifulSoup:
  184. self.soup = BeautifulSoup(html, "html.parser")
  185. html = str(self.soup)
  186. if self.include_tables:
  187. self.get_tables()
  188. self.feed(html)
  189. def add_html_to_cell(self, html, cell):
  190. if not isinstance(cell, docx.table._Cell):
  191. raise ValueError("Second argument needs to be a %s" % docx.table._Cell)
  192. unwanted_paragraph = cell.paragraphs[0]
  193. if unwanted_paragraph.text == "":
  194. delete_paragraph(unwanted_paragraph)
  195. self.set_initial_attrs(cell)
  196. self.run_process(html)
  197. # cells must end with a paragraph or will get message about corrupt file
  198. # https://stackoverflow.com/a/29287121
  199. if not self.doc.paragraphs:
  200. self.doc.add_paragraph("")
  201. def apply_paragraph_style(self, style=None):
  202. try:
  203. if style:
  204. self.paragraph.style = style
  205. elif self.paragraph_style:
  206. self.paragraph.style = self.paragraph_style
  207. except KeyError as e:
  208. raise ValueError(f"Unable to apply style {self.paragraph_style}.") from e
  209. def handle_table(self, html, doc):
  210. """
  211. To handle nested tables, we will parse tables manually as follows:
  212. Get table soup
  213. Create docx table
  214. Iterate over soup and fill docx table with new instances of this parser
  215. Tell HTMLParser to ignore any tags until the corresponding closing table tag
  216. """
  217. table_soup = BeautifulSoup(html, "html.parser")
  218. rows, cols_len = get_table_dimensions(table_soup)
  219. table = doc.add_table(len(rows), cols_len)
  220. table.style = doc.styles["Table Grid"]
  221. num_rows = len(table.rows)
  222. num_cols = len(table.columns)
  223. cell_row = 0
  224. for index, row in enumerate(rows):
  225. cols = get_table_columns(row)
  226. cell_col = 0
  227. for col in cols:
  228. colspan = int(col.attrs.get("colspan", 1))
  229. rowspan = int(col.attrs.get("rowspan", 1))
  230. cell_html = get_cell_html(col)
  231. if col.name == "th":
  232. cell_html = "<b>%s</b>" % cell_html
  233. if cell_row >= num_rows or cell_col >= num_cols:
  234. continue
  235. docx_cell = table.cell(cell_row, cell_col)
  236. while docx_cell.text != "": # Skip the merged cell
  237. cell_col += 1
  238. docx_cell = table.cell(cell_row, cell_col)
  239. cell_to_merge = table.cell(
  240. cell_row + rowspan - 1, cell_col + colspan - 1
  241. )
  242. if docx_cell != cell_to_merge:
  243. docx_cell.merge(cell_to_merge)
  244. child_parser = HtmlToDocx()
  245. child_parser.copy_settings_from(self)
  246. child_parser.add_html_to_cell(cell_html or " ", docx_cell)
  247. cell_col += colspan
  248. cell_row += 1
  249. def handle_data(self, data):
  250. if self.skip:
  251. return
  252. # Only remove white space if we're not in a pre block.
  253. if "pre" not in self.tags:
  254. # remove leading and trailing whitespace in all instances
  255. data = remove_whitespace(data, True, True)
  256. if not self.paragraph:
  257. self.paragraph = self.doc.add_paragraph()
  258. self.apply_paragraph_style()
  259. # There can only be one nested link in a valid html document
  260. # You cannot have interactive content in an A tag, this includes links
  261. # https://html.spec.whatwg.org/#interactive-content
  262. link = self.tags.get("a")
  263. if link:
  264. self.handle_link(link["href"], data)
  265. else:
  266. # If there's a link, dont put the data directly in the run
  267. self.run = self.paragraph.add_run(data)
  268. spans = self.tags["span"]
  269. for span in spans:
  270. if "style" in span:
  271. style = self.parse_dict_string(span["style"])
  272. self.add_styles_to_run(style)
  273. # add font style and name
  274. for tag in self.tags:
  275. if tag in font_styles:
  276. font_style = font_styles[tag]
  277. setattr(self.run.font, font_style, True)
  278. if tag in font_names:
  279. font_name = font_names[tag]
  280. self.run.font.name = font_name