function test_pptx_file
Tests the ability to open and read a PowerPoint (.pptx) file using the python-pptx library, validating file existence, size, and basic slide iteration.
/tf/active/vicechatdev/docchat/test_problematic_files.py
17 - 55
simple
Purpose
This function serves as a diagnostic tool to verify that PowerPoint files can be successfully opened and processed. It checks file existence, reports file size, attempts to open the presentation with python-pptx, counts slides, and tests iteration through the first two slides. It provides detailed console output with success/failure indicators and returns a boolean indicating whether the file was successfully processed.
Source Code
def test_pptx_file(file_path):
"""Test opening a PowerPoint file"""
print(f"\n{'='*80}")
print(f"Testing PowerPoint: {Path(file_path).name}")
print(f"{'='*80}")
try:
import pptx
file_path_obj = Path(file_path)
# Check if file exists
if not file_path_obj.exists():
print(f"❌ File does not exist!")
return False
# Check file size
file_size = file_path_obj.stat().st_size
print(f"✓ File exists, size: {file_size:,} bytes ({file_size/1024/1024:.2f} MB)")
# Try to open with python-pptx
print(f"Attempting to open with python-pptx...")
prs = pptx.Presentation(str(file_path))
print(f"✓ Successfully opened with python-pptx")
print(f" - Number of slides: {len(prs.slides)}")
# Try to extract some text
slide_count = 0
for slide in prs.slides:
slide_count += 1
if slide_count > 2: # Just test first 2 slides
break
print(f"✓ Can iterate through slides")
return True
except Exception as e:
print(f"❌ Error: {type(e).__name__}: {e}")
print(f"\nFull traceback:")
traceback.print_exc()
return False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
file_path |
- | - | positional_or_keyword |
Parameter Details
file_path: String or path-like object representing the file system path to the PowerPoint (.pptx) file to be tested. Can be absolute or relative path. The file should exist and be a valid PowerPoint presentation format.
Return Value
Returns a boolean value: True if the PowerPoint file exists, can be opened successfully with python-pptx, and slides can be iterated; False if any error occurs during the process (file not found, corrupted file, or any exception during processing).
Dependencies
pptxpathlibtraceback
Required Imports
from pathlib import Path
import traceback
import pptx
Usage Example
from pathlib import Path
import traceback
import pptx
def test_pptx_file(file_path):
"""Test opening a PowerPoint file"""
print(f"\n{'='*80}")
print(f"Testing PowerPoint: {Path(file_path).name}")
print(f"{'='*80}")
try:
import pptx
file_path_obj = Path(file_path)
if not file_path_obj.exists():
print(f"❌ File does not exist!")
return False
file_size = file_path_obj.stat().st_size
print(f"✓ File exists, size: {file_size:,} bytes ({file_size/1024/1024:.2f} MB)")
print(f"Attempting to open with python-pptx...")
prs = pptx.Presentation(str(file_path))
print(f"✓ Successfully opened with python-pptx")
print(f" - Number of slides: {len(prs.slides)}")
slide_count = 0
for slide in prs.slides:
slide_count += 1
if slide_count > 2:
break
print(f"✓ Can iterate through slides")
return True
except Exception as e:
print(f"❌ Error: {type(e).__name__}: {e}")
print(f"\nFull traceback:")
traceback.print_exc()
return False
# Example usage
result = test_pptx_file('presentation.pptx')
if result:
print('PowerPoint file is valid and readable')
else:
print('Failed to process PowerPoint file')
Best Practices
- Ensure the python-pptx library is installed before calling this function
- Provide valid file paths (absolute or relative) to existing PowerPoint files
- The function only tests the first 2 slides for performance reasons; modify the limit if deeper validation is needed
- Check the return value to determine if the file was successfully processed
- Console output provides detailed diagnostic information useful for debugging
- The function handles exceptions gracefully and prints full tracebacks for debugging
- File path is converted to string when passed to pptx.Presentation() for compatibility
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function main_v63 74.1% similar
-
function test_docx_file 70.3% similar
-
class PowerPointProcessor 58.8% similar
-
function test_web_ui 48.9% similar
-
function test_pyodbc_import 48.9% similar