Sankar guru
Sankar guru

Reputation: 1015

How to convert dotx file to html file in python?

This is my code which failing at the moment

import os
import pypandoc
source_dir = 'source'
result_dir = 'result'

for file in os.listdir(source_dir):

    output_files1 = []
    source_file = source_dir + '/'+file
    output_file = result_dir + '/'+file.replace('.dotx','.html').replace('.ott','.html')
    output = pypandoc.convert_file(source_file, 'html', outputfile=output_file)

I am trying to covert dotx file to html file, but I am getting the following error:

RuntimeError: Invalid input format! Got "dotx" but expected one of
these: commonmark, creole, docbook, docx, epub, fb2, gfm, haddock, 
html, jats, json, latex, markdown, markdown_github, markdown_mmd,
markdown_phpextra, markdown_strict, mediawiki, muse, native, odt, opml,
org, rst, t2t, textile, tikiwiki, twiki, vimwiki

Upvotes: 1

Views: 600

Answers (1)

catleeball
catleeball

Reputation: 871

While Pandoc supports .docx, unfortunately it doesn't look like Pandoc currently supports .dotx files in their list of supported formats

Fortunately, since .docx and .dotx are nearly identical, you can simply change the file extension to .docx and Pandoc will be able to support it. See this question for more context: https://superuser.com/questions/1285415/difference-between-documents-with-docx-and-dotx-filename-extensions

Here's a bit of logic added into your existing loop to help rename any .dotx to .docx files:

import os
import pypandoc
source_dir = 'source'
result_dir = 'result'

for file in os.listdir(source_dir):
    if file.endswith('.dotx'):
        filename = os.path.splitext(file)[0]
        os.rename(file, filename + '.docx')
        file = filename + '.dotx'
    output_files1 = []
    source_file = source_dir + '/'+file
    output_file = result_dir + '/'+file.replace('.dotx','.html').replace('.ott','.html')
    output = pypandoc.convert_file(source_file, 'html', outputfile=output_file)

Hope this helps! Please let me know if you have any questions.

Upvotes: 1

Related Questions