python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Embedding custom pptx template within executable file

Open kamranabbas20 opened this issue 3 years ago • 2 comments

I have written a code that takes several pictures from a folder and collates them into a company-specific PowerPoint template. Within the code, I am indicating the location of the template, which is in the same directory as the .py file. However, upon creating a .exe file it results in an error in the line that specifies the template of the presentation:

template = os.getcwd() + '\\template.pptx'
prs = Presentation(template) ### ERROR OCCURS HERE

Is there a way to embed my own power point template while building .exe file. I have tried putting it into .spec file, by indicating the location of the custom pptx template in datas, which I put into the site-packages as follows:

Screenshot_1

p = "C:\\Users\\<username>\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\pptx\\templates\\my_template.pptx"

block_cipher = None

a = Analysis(['Pictures_to_Slides.py'],
         pathex=[],
         binaries=[],
         datas=[ (p, '.\\pptx\\templates\\') ],
         hiddenimports=[],
         hookspath=[],
         hooksconfig={},
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)

After doing this the .exe file was created successfully but again resulted in error at the stage of declaring Presentation("my_template.pptx)

Does anybody have an experience with building custom pptx templates within .exe files?

kamranabbas20 avatar Apr 05 '22 06:04 kamranabbas20

I have the same issue but on macOS and without the custom template. As soon as I build a binary file to run this in a terminal window, I get these errors:

Traceback (most recent call last): File "PP_Report_Gen.py", line 184, in File "PP_Report_Gen.py", line 53, in create_presentation File "pptx/api.py", line 28, in Presentation File "pptx/opc/package.py", line 73, in open File "pptx/opc/package.py", line 157, in _load File "pptx/opc/package.py", line 186, in load File "pptx/opc/package.py", line 190, in _load File "pptx/util.py", line 215, in get File "pptx/opc/package.py", line 219, in _parts File "pptx/util.py", line 215, in get File "pptx/opc/package.py", line 203, in _content_types File "pptx/opc/serialized.py", line 35, in getitem File "pptx/util.py", line 215, in get File "pptx/opc/serialized.py", line 49, in _blob_reader File "pptx/opc/serialized.py", line 135, in factory pptx.exc.PackageNotFoundError: Package not found at '/var/folders/4j/3vvncgj534n0lp0g5c1pbgkw0000gn/T/_MEIorqIds/pptx/templates/default.pptx' [61955] Failed to execute script 'PP_Report_Gen' due to unhandled exception!

It looks like that the pyhton-pptx is looking for a default template. (/pptx/templates/default.pptx) If you could tell me where this file is located or how I can tell the program where to find it, that would be awesome.

aseedig avatar Jan 06 '24 18:01 aseedig

OK, I found the solution: Iin the folder where I have the python script, I created a new folder called pptxTemplate In my code I use this to locate the template:

Get the path of the template

template_path = os.path.dirname(os.path.abspath(__file__))

Get the directory of the template

source_folder = os.path.join(template_path, "pptxTemplate")

Construct the path to the image file

template_file_path = os.path.join(source_folder, "ocuair_template.pptx")

In my def, where I create the PowerPoint, I use this:

Load the template presentation

presentation = Presentation(template_file_path)

When I compile the program I use the following command:

pyinstaller PP_Report_Gen.py \
	--onefile \
	--add-data="./pptxTemplate:pptxTemplate" \
	--target-arch universal2 \
	-n ppt-generator

Now the program works on both CPUs, Intel and Apple Silicon.

I hope this helps.

aseedig avatar Jan 06 '24 20:01 aseedig