Last active
February 28, 2022 07:52
-
-
Save anemochore/83e6acf22ed08c588440a945042bdf17 to your computer and use it in GitHub Desktop.
위키독스 md를 docx로 (위키독스에서 책을 다운로드하면 개별 md 파일에 문서 제목이 사라지므로, 임의의 마크업과 제목을 추가해서 하나로 합친다)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. rename mds to be ordered (use a tool like Advanced Renamer) | |
# 2. run this file to merge mds to one md | |
# 3. run pandoc to convert md to docx with my filter (increase_header_level.py) | |
# 4. in word, find ?#n headers and apply appropriate styles | |
# standalone preprocessor for md files from wikidocs | |
import os | |
Path = "./" | |
filelist = os.listdir(Path) | |
with open('./merged.md', 'w', encoding='utf-8') as outfile: | |
for fname in filelist: | |
if fname.endswith(".md"): | |
with open(Path + fname, 'r', encoding='utf-8') as f: | |
num = fname[0:fname.find(' ')] | |
depth = num.count('.') + 1 | |
outfile.write('\n\n?#' + str(depth) + ' ' + fname.replace('.md', '') + '\n\n') | |
for line in f: | |
outfile.write(line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use like this: | |
# pandoc merged.md --filter C:/Users/FOR/AppData/Local/Pandoc/increase_header_level.py --reference-doc=C:/Users/FOR/Dropbox/@@@@제이펍/@원고_템플릿_캐시/reference.docx -o mydoc.docx | |
import panflute as pf | |
def prepare(doc): | |
pass | |
def action(elem, doc): | |
if type(elem) == pf.Header: | |
if elem.level < 6: | |
elem.level += 1 | |
else: | |
return [] # Delete headers already in level 6 | |
def main(doc=None): | |
return pf.run_filter(action, prepare, finalize, doc) | |
def finalize(doc): | |
pass | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment