title: How can I convert a Markdown string to a DocX in Python?
body: <p>I am getting markdown text from my API like this:</p>
  <pre class="lang-js prettyprint-override"><code>{
      name:'Onur',
      surname:'Gule',
      biography:'## Computers
      I like **computers** so much.
      I wanna *be* a computer.',
      membership:1
  }
  </code></pre>
  <p>biography column includes markdown string like above.</p>
  <pre><code>## Computers
  I like **computers** so much.
  I wanna *be* a computer.
  </code></pre>
  <p>I want to take this markdown text and convert to docx string for my reports.</p>
  <p>In my docx template:</p>
  <pre><code>{{markdownText|mark2html}}
  
  {{simpleText}}
  </code></pre>
  <p>I am using python3 <strong>docxtpl</strong> package for creating docx and it's working for simple texts.</p>
  <ul>
  <li>I tried BeautifulSoup for convert markdown to docx text but it doesn't work for styles(bold, italic etc.).</li>
  <li>I tried pandoc and it worked but it just create a docx file, I want to add rendered markdown text to existing docx(while creating).</li>
  </ul>
  <p>My current code:</p>
  <pre class="lang-py prettyprint-override"><code>import docx
  from docxtpl import DocxTemplate, RichText
  import markdown
  import jinja2
  import markupsafe
  from bs4 import BeautifulSoup
  import pypandoc
  
  def safe_markdown(text):
      return markupsafe.Markup(markdown.markdown(text))
  
  def mark2html(value):
      html = markdown.markdown(value)
      soup = BeautifulSoup(html, features='html.parser')
      output = pypandoc.convert_text(value,'rtf',format='md')
      return RichText(value) #tried soup and pandoc..
  
  def from_template(template):
      template = DocxTemplate(template)
      context = {
          'simpleText':'Simple text test.',
          'markdownText':'Markdown **text** test.'
      } 
      jenv = jinja2.Environment()
      jenv.filters['markdown'] = safe_markdown
      jenv.filters[&quot;mark2html&quot;] = mark2html
      template.render(context,jenv)
      template.save('new_report.docx')
  </code></pre>
  <p>So, how can I add rendered markdown to existed docx or while creating, maybe with a jinja2 filter?</p>