table_process.py 11 KB

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