Hi @Sharma, Gaurav
There's something wrong with the docx package. My local setup is Python 3.11 with the same requirements.txt as you provided using the v2 programming model and the function wouldn't run locally. I received the same error message as you. When I removed the code reference to docx, the function built successfully and properly registered.
I'll preface by saying I'm not well versed in docx, but in search I came across https://python-docx.readthedocs.io/en/latest/ and checking the python package library, saw https://pypi.org/project/python-docx/#history. So I attempted to use a lower version 1.01. which did not registered and received the following error message:
ERROR: Could not find a version that satisfies the requirement docx==1.0.1 (from versions: 0.1.2, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4)
ERROR: No matching distribution found for docx==1.0.1
So, I then changed the requirements.txt to reference python-docx instead of docx and followed the docs using from docx import Document
. Doing this resulted in a successful build and proper registration of the Azure Function locally. The code I used is very basic (as you can see below) but didn't result in any errors that docx resulted in. I don't know what the differences are, if any, between both, but I would advise using python-docx and see if that works for you. I also commented out PyPDF2 as that also resulted in a build/run failure. Could be a similar issue with the package just like python-docx.
import logging
import azure.functions as func
from azure.storage.blob import BlobServiceClient,BlobClient, ContainerClient
import os
from docx import Document
# import PyPDF2
import re
app = func.FunctionApp()
@app.route(route="GetMessage", auth_level=func.AuthLevel.ANONYMOUS)
def GetMessage(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
filename = req.params.get('name')
if not filename:
return func.HttpResponse("Pass 'name' as a query string parameter.", status_code=400)
doc = Document()
doc.add_paragraph(req.get_body().decode('utf-8'))
doc.save(f"{filename}.docx")
except Exception as e:
logging.error(str(e))
return func.HttpResponse("An error occurred while processing the request.", status_code=500)
return func.HttpResponse(f"File {filename}.docx created successfully.", status_code=200)