code: def remove_whitespace(string, leading=False, trailing=False):
      """Remove white space from a string.
      Args:
          string(str): The string to remove white space from.
          leading(bool, optional): Remove leading new lines when True.
          trailing(bool, optional): Remove trailing new lines when False.
      Returns:
          str: The input string with new line characters removed and white space squashed.
      Examples:
          Single or multiple new line characters are replaced with space.
              >>> remove_whitespace("abc\\ndef")
              'abc def'
              >>> remove_whitespace("abc\\n\\n\\ndef")
              'abc def'
          New line characters surrounded by white space are replaced with a single space.
              >>> remove_whitespace("abc \\n \\n \\n def")
              'abc def'
              >>> remove_whitespace("abc  \\n  \\n  \\n  def")
              'abc def'
          Leading and trailing new lines are replaced with a single space.
              >>> remove_whitespace("\\nabc")
              ' abc'
              >>> remove_whitespace("  \\n  abc")
              ' abc'
              >>> remove_whitespace("abc\\n")
              'abc '
              >>> remove_whitespace("abc  \\n  ")
              'abc '
          Use ``leading=True`` to remove leading new line characters, including any surrounding
          white space:
              >>> remove_whitespace("\\nabc", leading=True)
              'abc'
              >>> remove_whitespace("  \\n  abc", leading=True)
              'abc'
          Use ``trailing=True`` to remove trailing new line characters, including any surrounding
          white space:
              >>> remove_whitespace("abc  \\n  ", trailing=True)
              'abc'
      """
      # Remove any leading new line characters along with any surrounding white space
      if leading:
          string = re.sub(r"^\s*\n+\s*", "", string)
  
      # Remove any trailing new line characters along with any surrounding white space
      if trailing:
          string = re.sub(r"\s*\n+\s*$", "", string)
  
      # Replace new line characters and absorb any surrounding space.
      string = re.sub(r"\s*\n\s*", " ", string)
      # TODO need some way to get rid of extra spaces in e.g. text <span>   </span>  text
      return re.sub(r"\s+", " ", string)
  
imports: import re
  import docx
  from docx import Document
  from bs4 import BeautifulSoup
  from html.parser import HTMLParser
label_desc: Use image node_id 6 for calling a global function with example usage: remove_whitespace(string, leading, trailing) and returns: re