2017-05-02 41 views
1

我使用panflute爲pandoc編寫了一個python過濾器,將Markdown轉換爲Word文檔。通常,pandoc會將Markdown標題轉換爲Word的內置樣式,稱爲標題1,標題2等。但由於必須使用Word模板的詳細信息,我需要將所有Markdown標題更改爲Word中相應的自定義樣式這個頭1級=>頭1,2 =>頭2級等pandoc - 用Word自定義樣式替換標題docx

這裏是我做了測試我的過濾器快速樣品降價文件:

# Heading 1 

some text in a paragraph 

## Heading 2 

a little bit more text down below 

從本質上講,我要轉換的是降價,就好像我寫過這樣的:

<div custom-style="Header1">Heading 1</div> 

some text in a paragraph 

<div custom-style="Header2">Heading 2</div> 

a little bit more text down below 

這樣,當我運行時:

pandoc -S test_input.md -o test_output.docx --reference-docx ./custom_styles.docx --filter ./test_filter.py 

生成的Word docx將使用適當的自定義樣式。

關注?

無論如何,這裏是我使用panflute寫的過濾器:

#! /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() 

,不幸的是,不符合我的風格定製的div取代降價頭。它基本上出現在另一端,好像根本沒有過濾器。

我不知道我在做什麼錯在這裏。

+0

我不熟悉panflute,也許在pandoc-discuss郵件列表上詢問? – mb21

回答

1

啊哈!最後我自己想清楚了。

from panflute import * 

def action(elem, doc): 
    if isinstance(elem, Header): 
     #return Div(elem, attributes={'custom-style': 'Header{}'.format(elem.level)}) 
     return Div(Para(*elem.content), attributes={'custom-style': 'Header {}'.format(elem.level)}) 

def main(doc=None): 
    return run_filter(action, doc=doc) 

if __name__ == "__main__": 
    main()