Below is my function which works fine when executed separately
import win32com.client
def document_tohtml(inputfile):
print("Input File",inputfile)
doc = win32com.client.GetObject(inputfile)
saved_file = doc.SaveAs (FileName="C:\Extraction\TMS TO 692M15-22-F-00073 Monthly Status _ Financial Report July 2022.html", FileFormat=8)
doc.Close ()
return saved_file
if __name__ == '__main__':
document_tohtml("C:\Extraction\TMS TO 692M15-22-F-00073 Monthly Status _ Financial Report July 2022.docx")
Issue : I am trying to convert the above function into Flask API which is below
@app.route('/doctohtml', methods=['GET','POST'])
def document_tohtml():
if request.method == 'POST':
files = request.files['file']
with open(files.filename, "rb") as docx_file:
print("Files",files)
print("File name",files.filename)
# print("Type",type(new_file))
doc = win32com.client.GetObject(str(files))
with open(files.filename, "w") as html_file:
saved_file=html_file.write(doc.value)
# saved_file = doc.SaveAs (FileName="sample.html", FileFormat=8)
# doc.Close ()
return saved_file
But when I am hitting the api via postman I am getting below error
Files <FileStorage: 'TMS TO 692M15-22-F-00073 Monthly Status _ Financial Report July 2022.docx' ('application/vnd.openxmlformats-officedocument.wordprocessingml.document')>
File name TMS TO 692M15-22-F-00073 Monthly Status _ Financial Report July 2022.docx
127.0.0.1 - - [20/Sep/2022 16:11:06] "POST /doctohtml HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Extraction\Exenv\lib\site-packages\flask\app.py", line 2548, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Extraction\Exenv\lib\site-packages\flask\app.py", line 2528, in wsgi_app
response = self.handle_exception(e)
File "C:\Extraction\Exenv\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Extraction\Exenv\lib\site-packages\flask\app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "C:\Extraction\Exenv\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Extraction\Exenv\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Extraction\Exenv\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Extraction\Exenv\lib\site-packages\flask\app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
**[![File "C:\Extraction\Doc_Extract\new_approach.py", line 30, in document_tohtml
doc = win32com.client.GetObject(str(files))
File "C:\Extraction\Exenv\lib\site-packages\win32com\client\__init__.py", line 85, in GetObject
return Moniker(Pathname, clsctx)
File "C:\Extraction\Exenv\lib\site-packages\win32com\client\__init__.py", line 102, in Moniker
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
pywintypes.com_error: (-2147221020, 'Invalid syntax', None, None)][1]][1]**
P.S I have also tried other libraries like mammoth,aspose but they are not providing desired results and then tried above pywin32