title: pandoc - replace heading with custom style for Word docx
body: <p>I'm using <a href="http://scorreia.com/software/panflute/" rel="nofollow noreferrer">panflute</a> to write a python filter for pandoc to convert Markdown into a Word document. Normally, pandoc converts Markdown headers into Word's built-in styles called Heading 1, Heading 2, etc. But due to the particulars of the Word template I have to use, I instead need to change all Markdown headers into corresponding custom styles in Word such that Header level 1 => Header1, level 2 => Header2, etc.</p>
  
  <p>Here's a quick sample Markdown file I made to test my filter:</p>
  
  <pre><code># Heading 1
  
  some text in a paragraph
  
  ## Heading 2
  
  a little bit more text down below
  </code></pre>
  
  <p>Essentially, I want to convert that Markdown as if I had written it like:</p>
  
  <pre><code>&lt;div custom-style="Header1"&gt;Heading 1&lt;/div&gt;
  
  some text in a paragraph
  
  &lt;div custom-style="Header2"&gt;Heading 2&lt;/div&gt;
  
  a little bit more text down below
  </code></pre>
  
  <p>So that way, when I run:</p>
  
  <pre><code>pandoc -S test_input.md -o test_output.docx --reference-docx ./custom_styles.docx --filter ./test_filter.py
  </code></pre>
  
  <p>The resulting Word docx will use the appropriate custom styles.</p>
  
  <p>Follow?</p>
  
  <p>Anyway, here's the filter I've written using panflute:</p>
  
  <pre><code>#! /usr/bin/env python
  #coding: utf-8
  
  from panflute import *
  
  def action( elem, doc ):
      if isinstance( elem, Header ):
          return Div( elem, classes=['Header{}'.format(elem.level)] )
  
  def main(doc=None):
      return run_filter( action, doc=doc )
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>Which, unfortunately, does not replace the Markdown headers with my custom divs for styling. It basically comes out the other end as if there was no filter in place at all.</p>
  
  <p>I'm not sure what I'm doing wrong here.</p>