I had a similar issue like above so I thought I would post an alternative way to solve the problem:
You can use the following to save files like .rtf (or other) to any format you are able to save/open within the Word application (such as '.docx', '.rtf', '.doc', '.vcf', '.txt', '.odt' etc. )
For that just change the ending from '.doc' in new_file_abs to '.docx' or other!
You need to have Word installed.
(Optionally I attached how to convert to .pdf)
You need to pip install:
pip install pywin32
pip install docx2pdf (optional if you need to have pdf)
Convert .rtf -> .doc / or any other Word-Format.
from glob import glob
import re
import os
import win32com.client as win32
from win32com.client import constants
def change_word_format(file_path):
word = win32.gencache.EnsureDispatch('Word.Application')
doc = word.Documents.Open(file_path)
doc.Activate()
# Rename path with .doc
new_file_abs = os.path.abspath(file_path)
new_file_abs = re.sub(r'\.\w+$', '.doc', new_file_abs)
# Save and Close
word.ActiveDocument.SaveAs(
new_file_abs, FileFormat=constants.wdFormatDocument
)
doc.Close(False)
Optional if PDF is needed .docx -> .pdf
from docx2pdf import convert
def docx_to_pdf(ori_file, new_file):
convert(ori_file, new_file)