I have a Python Azure Function App. I'm deploying the code via Github actions and have python-pptx
in my requirements.txt
, but when it's deployed, I get this error:
Error: No module named 'pptx'
import azure.functions as func
import logging
logging.basicConfig(level=logging.INFO)
app = func.FunctionApp()
# logging.info("Importing PPTManager from assets.powerpoint")
@app.route(route="http_trigger", methods=\["get", "post"\])
def http_trigger(req: func.HttpRequest) -\> func.HttpResponse:
from assets.powerpoint import PPTManager
logging.info('Python HTTP trigger function processed a request.')
return func.HttpResponse("Hello World", status_code=200)
import logging
try:
import pptx
logging.info("pptx imported successfully")
except ImportError as e:
logging.error(f"Error: {str(e)}")
print(f"Error: {str(e)}")
It is in the requirements.txt
:
# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues
azure-functions
azure-storage-blob
python-pptx
office365
sqlalchemy
pandas
numpy
yaspin
tqdm
tenacity
pymongo
pyodbc
filelock
colorama
When I check the logs in the Github actions, it tells me the package has been installed:
Collecting python-pptx (from -r requirements.txt (line 7))
Downloading python_pptx-1.0.2-py3-none-any.whl.metadata (2.5 kB)
Collecting Pillow>=3.3.2 (from python-pptx->-r requirements.txt (line 7))
Downloading pillow-11.0.0-cp311-cp311-manylinux_2_28_x86_64.whl.metadata (9.1 kB)
Collecting XlsxWriter>=0.5.7 (from python-pptx->-r requirements.txt (line 7))
Downloading XlsxWriter-3.2.0-py3-none-any.whl.metadata (2.6 kB)
Collecting lxml>=3.1.0 (from python-pptx->-r requirements.txt (line 7))
Downloading lxml-5.3.0-cp311-cp311-manylinux_2_28_x86_64.whl.metadata (3.8 kB)
Downloading python_pptx-1.0.2-py3-none-any.whl (472 kB)
Installing collected packages: pytz, PyQt5-Qt5, pyperclip, pydub, prettierfier, parsedatetime, maybe-else, gender-guesser, fuzzywuzzy, cursor, colour, azure-functions, appdirs, aenum, XlsxWriter, urllib3, tzlocal, tzdata, typing-extensions, tqdm, termcolor, tenacity, tabulate, soupsieve, six, simplejson, Send2Trash, regex, readchar, PyQt5-sip, PyPDF2, pyodbc, pyinstrument, pycparser, Pillow, olefile, oauthlib, numpy, more-itertools, lxml, isodate, infi.systray, imageio-ffmpeg, idna, greenlet, filelock, dnspython, dill, decorator, colorama, clipboard, charset-normalizer, chardet, certifi, yaspin, typeguard, sqlalchemy, requests, **python-pptx**, python-docx, python-dateutil, PyQt5, pymongo, proglog, mbstrdecoder, lxml-html-clean, imageio, cffi, case-conversion, beautifulsoup4, APScheduler, typepy, requests-oauthlib, pandas, moviepy, inflect, html-text, cryptography, bs4, azure-core, O365, msoffcrypto-tool, azure-storage-blob, pysubtypes, pathmagic, pymiscutils, pyiotools, office365
Successfully installed APScheduler-3.10.4 O365-2.0.37 Pillow-11.0.0 PyPDF2-3.0.1 PyQt5-5.15.11 PyQt5-Qt5-5.15.15 PyQt5-sip-12.15.0 Send2Trash-1.8.3 XlsxWriter-3.2.0 aenum-3.1.15 appdirs-1.4.4 azure-core-1.32.0 azure-functions-1.21.3 azure-storage-blob-12.24.0 beautifulsoup4-4.12.3 bs4-0.0.2 case-conversion-2.1.0 certifi-2024.8.30 cffi-1.17.1 chardet-5.2.0 charset-normalizer-3.4.0 clipboard-0.0.4 colorama-0.4.6 colour-0.1.5 cryptography-43.0.3 cursor-1.3.5 decorator-4.4.2 dill-0.3.9 dnspython-2.7.0 filelock-3.16.1 fuzzywuzzy-0.18.0 gender-guesser-0.4.0 greenlet-3.1.1 html-text-0.6.2 idna-3.10 imageio-2.36.0 imageio-ffmpeg-0.5.1 infi.systray-0.1.12 inflect-7.4.0 isodate-0.7.2 lxml-5.3.0 lxml-html-clean-0.4.1 maybe-else-0.2.1 mbstrdecoder-1.1.3 more-itertools-10.5.0 moviepy-1.0.3 msoffcrypto-tool-5.4.2 numpy-2.1.3 oauthlib-3.2.2 office365-0.3.15 olefile-0.47 pandas-2.2.3 parsedatetime-2.6 pathmagic-0.3.14 prettierfier-1.0.3 proglog-0.1.10 pycparser-2.22 pydub-0.25.1 pyinstrument-5.0.0 pyiotools-0.3.18 pymiscutils-0.3.14 pymongo-4.10.1 pyodbc-5.2.0 pyperclip-1.9.0 pysubtypes-0.3.18 python-dateutil-2.9.0.post0 python-docx-1.1.2 **python-pptx-1.0.2** pytz-2024.2 readchar-4.2.1 regex-2024.11.6 requests-2.32.3 requests-oauthlib-2.0.0 simplejson-3.19.3 six-1.16.0 soupsieve-2.6 sqlalchemy-2.0.36 tabulate-0.9.0 tenacity-9.0.0 termcolor-2.3.0 tqdm-4.67.0 typeguard-4.4.1 typepy-1.3.2 typing-extensions-4.12.2 tzdata-2024.2 tzlocal-5.2 urllib3-2.2.3 yaspin-3.1.0
Any assistance is welcomed.
I have been able to run it locally without error.
I have a Python Azure Function App. I'm deploying the code via Github actions and have python-pptx
in my requirements.txt
, but when it's deployed, I get this error:
Error: No module named 'pptx'
import azure.functions as func
import logging
logging.basicConfig(level=logging.INFO)
app = func.FunctionApp()
# logging.info("Importing PPTManager from assets.powerpoint")
@app.route(route="http_trigger", methods=\["get", "post"\])
def http_trigger(req: func.HttpRequest) -\> func.HttpResponse:
from assets.powerpoint import PPTManager
logging.info('Python HTTP trigger function processed a request.')
return func.HttpResponse("Hello World", status_code=200)
import logging
try:
import pptx
logging.info("pptx imported successfully")
except ImportError as e:
logging.error(f"Error: {str(e)}")
print(f"Error: {str(e)}")
It is in the requirements.txt
:
# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues
azure-functions
azure-storage-blob
python-pptx
office365
sqlalchemy
pandas
numpy
yaspin
tqdm
tenacity
pymongo
pyodbc
filelock
colorama
When I check the logs in the Github actions, it tells me the package has been installed:
Collecting python-pptx (from -r requirements.txt (line 7))
Downloading python_pptx-1.0.2-py3-none-any.whl.metadata (2.5 kB)
Collecting Pillow>=3.3.2 (from python-pptx->-r requirements.txt (line 7))
Downloading pillow-11.0.0-cp311-cp311-manylinux_2_28_x86_64.whl.metadata (9.1 kB)
Collecting XlsxWriter>=0.5.7 (from python-pptx->-r requirements.txt (line 7))
Downloading XlsxWriter-3.2.0-py3-none-any.whl.metadata (2.6 kB)
Collecting lxml>=3.1.0 (from python-pptx->-r requirements.txt (line 7))
Downloading lxml-5.3.0-cp311-cp311-manylinux_2_28_x86_64.whl.metadata (3.8 kB)
Downloading python_pptx-1.0.2-py3-none-any.whl (472 kB)
Installing collected packages: pytz, PyQt5-Qt5, pyperclip, pydub, prettierfier, parsedatetime, maybe-else, gender-guesser, fuzzywuzzy, cursor, colour, azure-functions, appdirs, aenum, XlsxWriter, urllib3, tzlocal, tzdata, typing-extensions, tqdm, termcolor, tenacity, tabulate, soupsieve, six, simplejson, Send2Trash, regex, readchar, PyQt5-sip, PyPDF2, pyodbc, pyinstrument, pycparser, Pillow, olefile, oauthlib, numpy, more-itertools, lxml, isodate, infi.systray, imageio-ffmpeg, idna, greenlet, filelock, dnspython, dill, decorator, colorama, clipboard, charset-normalizer, chardet, certifi, yaspin, typeguard, sqlalchemy, requests, **python-pptx**, python-docx, python-dateutil, PyQt5, pymongo, proglog, mbstrdecoder, lxml-html-clean, imageio, cffi, case-conversion, beautifulsoup4, APScheduler, typepy, requests-oauthlib, pandas, moviepy, inflect, html-text, cryptography, bs4, azure-core, O365, msoffcrypto-tool, azure-storage-blob, pysubtypes, pathmagic, pymiscutils, pyiotools, office365
Successfully installed APScheduler-3.10.4 O365-2.0.37 Pillow-11.0.0 PyPDF2-3.0.1 PyQt5-5.15.11 PyQt5-Qt5-5.15.15 PyQt5-sip-12.15.0 Send2Trash-1.8.3 XlsxWriter-3.2.0 aenum-3.1.15 appdirs-1.4.4 azure-core-1.32.0 azure-functions-1.21.3 azure-storage-blob-12.24.0 beautifulsoup4-4.12.3 bs4-0.0.2 case-conversion-2.1.0 certifi-2024.8.30 cffi-1.17.1 chardet-5.2.0 charset-normalizer-3.4.0 clipboard-0.0.4 colorama-0.4.6 colour-0.1.5 cryptography-43.0.3 cursor-1.3.5 decorator-4.4.2 dill-0.3.9 dnspython-2.7.0 filelock-3.16.1 fuzzywuzzy-0.18.0 gender-guesser-0.4.0 greenlet-3.1.1 html-text-0.6.2 idna-3.10 imageio-2.36.0 imageio-ffmpeg-0.5.1 infi.systray-0.1.12 inflect-7.4.0 isodate-0.7.2 lxml-5.3.0 lxml-html-clean-0.4.1 maybe-else-0.2.1 mbstrdecoder-1.1.3 more-itertools-10.5.0 moviepy-1.0.3 msoffcrypto-tool-5.4.2 numpy-2.1.3 oauthlib-3.2.2 office365-0.3.15 olefile-0.47 pandas-2.2.3 parsedatetime-2.6 pathmagic-0.3.14 prettierfier-1.0.3 proglog-0.1.10 pycparser-2.22 pydub-0.25.1 pyinstrument-5.0.0 pyiotools-0.3.18 pymiscutils-0.3.14 pymongo-4.10.1 pyodbc-5.2.0 pyperclip-1.9.0 pysubtypes-0.3.18 python-dateutil-2.9.0.post0 python-docx-1.1.2 **python-pptx-1.0.2** pytz-2024.2 readchar-4.2.1 regex-2024.11.6 requests-2.32.3 requests-oauthlib-2.0.0 simplejson-3.19.3 six-1.16.0 soupsieve-2.6 sqlalchemy-2.0.36 tabulate-0.9.0 tenacity-9.0.0 termcolor-2.3.0 tqdm-4.67.0 typeguard-4.4.1 typepy-1.3.2 typing-extensions-4.12.2 tzdata-2024.2 tzlocal-5.2 urllib3-2.2.3 yaspin-3.1.0
Any assistance is welcomed.
I have been able to run it locally without error.
Share Improve this question edited Nov 21, 2024 at 10:56 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Nov 21, 2024 at 7:52 diegodoesdodiegodoesdo 12 bronze badges 1- Check if below solution helps @diegodoesdo – Pravallika KV Commented Dec 3, 2024 at 6:43
1 Answer
Reset to default 0Use below code to import ppt module and create PowerPoint presentation.
function_app.py:
import azure.functions as func
import logging
from assets.powerpoint import PPTManager
logging.basicConfig(level=logging.INFO)
app = func.FunctionApp()
@app.route(route="http_trigger", methods=["get", "post"])
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
ppt_manager = PPTManager() # Create an instance of PPTManager
logging.info("PPTManager instance created successfully.")
ppt_filename = ppt_manager.create_presentation() # Create a PowerPoint file
logging.info(f"PowerPoint file created at: {ppt_filename}")
logging.info('Python HTTP trigger function processed a request.')
return func.HttpResponse("Presentation created successfully", status_code=200)
except ImportError as e:
logging.error(f"Error importing PPTManager: {str(e)}")
return func.HttpResponse(f"Error importing PPTManager: {str(e)}", status_code=500)
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
return func.HttpResponse(f"An error occurred: {str(e)}", status_code=500)
try:
logging.info("pptx imported successfully")
except ImportError as e:
logging.error(f"Error: {str(e)}")
print(f"Error: {str(e)}")
assets=>powerpoint.py:
import logging
from pptx import Presentation
class PPTManager:
def __init__(self):
logging.info("Initializing PPTManager")
def create_presentation(self):
prs = Presentation()
slide_layout = prs.slide_layouts[0] # Choose slide layout
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Hello, World!"
subtitle.text = "Welcome to Azure Functions with PowerPoint"
ppt_filename = "/tmp/sample_presentation.pptx"
prs.save(ppt_filename)
logging.info(f"Presentation saved to {ppt_filename}")
return ppt_filename
def process_ppt_file(self, file_path):
try:
prs = Presentation(file_path)
logging.info(f"Processing presentation from {file_path}")
return prs.slides
except Exception as e:
logging.error(f"Error processing PowerPoint file: {e}")
raise
requirements.txt:
azure-functions
azure-storage-blob
python-pptx
office365
sqlalchemy
pandas
numpy
yaspin
tqdm
tenacity
pymongo
pyodbc
filelock
colorama
Local Response:
Functions:
http_trigger: [GET,POST] http://localhost:7071/api/http_trigger
For detailed output, run func with --verbose flag.
[2024-11-21T11:45:18.403Z] Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=98543d1c-3805-4396-8ddb-7e8970d21f6f)
[2024-11-21T11:45:18.479Z] Python HTTP trigger function processed a request.
[2024-11-21T11:45:18.479Z] Initializing PPTManager
[2024-11-21T11:45:18.479Z] PPTManager instance created successfully.
[2024-11-21T11:45:18.509Z] Presentation saved to ./sample_presentation.pptx
[2024-11-21T11:45:18.511Z] Python HTTP trigger function processed a request.
[2024-11-21T11:45:18.511Z] PowerPoint file created at: ./sample_presentation.pptx
[2024-11-21T11:45:18.596Z] Executed 'Functions.http_trigger' (Succeeded, Id=98543d1c-3805-4396-8ddb-7e8970d21f6f, Duration=219ms)
Able to deploy the function to Azure function app:
Portal:
Able to run the function successffully:
sample_presentation.pptx: