Heres the code of what im trying to implement
from flask import Flask, request, jsonify
import json
import pythoncom
import win32com.client
from win32com.client import Dispatch
xl = None
xl = win32com.client.Dispatch("Excel.Application")
app = Flask(__name__)
@app.route("/check", methods=['POST'])
def check():
pythoncom.CoInitialize()
if request.method == 'POST':
data = request.get_json()
fname = data['fname']
phName = data['PH_Name']
liName = data['LI_NAME']
ppm = data['PPM']
policyTerm = data['Policy_Term']
sumAssured = data['Sum_Assured']
wb = None
if xl :
wb = xl.Workbooks.Open(fname)
inp_sheet = wb.Sheets["Inputs"]
#read data from sheet
data = inp_sheet.Range('D8').Value
wb.Close(False)
xl.Quit()
return (data)
if __name__ == '__main__':
app.run(threaded=True, port=5001, debug=True)
The issue is that win32com creates new threads whenever a flask function is used to open a workbook and thus requires to marshal the Dispatcher object onto a stream to pass it to the function. How do i do that for an api call ?
I found some suggestions using CoMarshalInterThreadInterfaceInStream and CoGetInterfaceAndReleaseStream but they fail to run when multiple parallel calls are made to the api.