Search code examples
ms-wordfont-sizepandoc

Font is not changed in Word docx generated by pandoc using --reference-doc


I have tried a number of ways to change the font in the word docx generated by pandoc. The following is the latest:

  1. Update the custom-reference.docx to use size-10 font for everything but the Title and Header fields

  2. pandoc --reference-doc=custom-reference.docx -o docs/reader_filtering.docx docs/reader_filtering.md

The output font of the output file is still Cambria with font size=12 (except for the Title and Headers). I am pretty desperate to get this fixed because the client insists on using Word.

Update Changing the font in the custom-reference.docx does not work. It is necessary to bring up the Style dialog for a given paragraph and change the font manually by hitting "Modify" and then entering the font. You have to do this one by one for every style:

enter image description here

This is tedious and nonsensical.


Solution

  • I dug a bit and found the python library python-docx. The specific Styles that pandoc creates mostly end with Tok so the following code is doing a good job:

    #!/usr/bin/env bash
    
    infile="$1"
    infile_noext=$(echo "${infile}"  | cut -f 1 -d '.')
    outfile="${infile_noext}.docx"
    # pandoc --print-default-data-file reference.docx > custom-reference.docx
    # pandoc --reference-doc=custom-reference.docx   -o ${infile_noext}.docx ${infile}
    pandoc  -o ${outfile} ${infile}
    
    # Do this once: "pip3 install -y python-docx"
    
    python <<EOF -
    import docx
    from docx.shared import Pt
    import sys
    # d = docx.Document(sys.argv[1])
    d = docx.Document('${outfile}')
    # for style in list(filter(lambda x: x.name.endswith('Tok'), d.styles)):
    for style in list(filter(lambda x: not x.name.startswith('Heading'), d.styles)):
        #['CommentTok','NormalTok','Source Code', 'AttributeTok']
        # style = document.styles['Normal']
        font = style.font
        font.size = Pt(10)
    d.save('${outfile}')
    EOF
    echo "Created ${outfile}"
    

    Then

    enter image description here