fix: handle None image_path safely to prevent AttributeError
Fix bug introduced in previous commit where image_path=None caused
AttributeError when calling .lower() on None value.
**Problem**:
Setting image_path to None for table placeholders caused crashes at:
- Line 415: 'table' in img.get('image_path', '').lower()
- Line 453: 'table' not in img.get('image_path', '').lower()
When key exists but value is None, .get('image_path', '') returns None
(not default value), causing .lower() to fail.
**Solution**:
Use img.get('type') == 'table' to identify table entries instead of
checking image_path string. This is:
- More explicit and reliable
- Safer (no string operations on potentially None values)
- Cleaner code
**Changes**:
- Line 415: Check img.get('type') == 'table' for table count
- Line 453: Filter using img.get('type') != 'table' and image_path is not None
- Added informative log message showing table count
**Verification**:
draw_image_region already safely handles None/empty image_path (lines 1013-1015)
by returning early if not image_path_str.
Task 2.1 now fully functional without crashes.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -412,9 +412,9 @@ class PDFGeneratorService:
|
|||||||
|
|
||||||
# Filter text regions to avoid overlap with tables/images
|
# Filter text regions to avoid overlap with tables/images
|
||||||
regions_to_avoid = images_metadata
|
regions_to_avoid = images_metadata
|
||||||
table_count = len([img for img in images_metadata if 'table' in img.get('image_path', '').lower()])
|
table_count = len([img for img in images_metadata if img.get('type') == 'table'])
|
||||||
|
|
||||||
logger.info(f"過濾文字區域: {len(regions_to_avoid)} 個區域需要避免")
|
logger.info(f"過濾文字區域: {len(regions_to_avoid)} 個區域需要避免 (含 {table_count} 個表格)")
|
||||||
|
|
||||||
filtered_text_regions = self._filter_text_in_regions(text_regions, regions_to_avoid)
|
filtered_text_regions = self._filter_text_in_regions(text_regions, regions_to_avoid)
|
||||||
|
|
||||||
@@ -450,7 +450,8 @@ class PDFGeneratorService:
|
|||||||
page_image_regions = [
|
page_image_regions = [
|
||||||
img for img in images_metadata
|
img for img in images_metadata
|
||||||
if img.get('page') == page_num - 1
|
if img.get('page') == page_num - 1
|
||||||
and 'table' not in img.get('image_path', '').lower()
|
and img.get('type') != 'table'
|
||||||
|
and img.get('image_path') is not None # Skip table placeholders
|
||||||
]
|
]
|
||||||
|
|
||||||
# Draw in layers: images → tables → text
|
# Draw in layers: images → tables → text
|
||||||
|
|||||||
Reference in New Issue
Block a user