Search code examples
ms-wordpython-sphinxpandocrestructuredtext

How to convert a Word document into an html file through the chain pandoc -> sphinx and keeping the tabs of the document's code examples


I would like to create an HTML site from a Word document about Python programming. I use the chain word document -> [pandoc] -> rst file -> [sphinx] -> html site. Everything is working ok except the examples of python code: all the tabs are lost and all the code lines are left aligned which causes the code to be intelligible.

What can I do to solve this problem? I would prefer to act on the original word document than the intermediary restructuredtext file.

How to reproduce the problem (knowledge of word, pandoc, sphinx, restructuredtext required):

a) build a word document [example.docx] with a python program in it. Python code needs tabs. For example :

Python code example:

# ----------------------------------
def affiche(chaine):
    # affiche chaine
    print("chaine=%s" % chaine)


# ----------------------------------
def afficheType(variable):
    # affiche le type de variable
    print("type[%s]=%s" % (variable, type(variable)))


# ----------------------------------
def f1(param):
    # ajoute 10 à param
    return param + 10


# ----------------------------------
def f2():
    # rend 3 valeurs
    return "un", 0, 100


# -------------------------------- programme principal ------------------------------------
# ceci est un commentaire
# variable utilisée sans avoir été déclarée
nom = "dupont"

# un affichage écran
print("nom=%s" % nom)

# une liste avec des éléments de type différent
liste = ["un", "deux", 3, 4]

b) with a console, type the following command

pandoc -f docx -t rst -i example.docx - t example.rst

c) type the following commands:

sphinx-quickstart

make html

d) the produced html looks like that:

html produced from the word document

We can see that the lines of the [affiche] 1 function are all aligned on the left. That is incorrect for a python code;


Solution

  • For those interested, i found a solution: i gave to the code, the word style [Source Code] and now the html looks like that :

    enter image description here