Skip to content

Commit 22702bd

Browse files
committed
feat(export): enhance image embedding with dynamic sizing and advanced anchoring
1 parent f7bad58 commit 22702bd

1 file changed

Lines changed: 57 additions & 2 deletions

File tree

backend/usecase/export_data_usecase.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ def _create_dataframe(self, data: list[PyconExportData]) -> tuple[pd.DataFrame,
120120
if field.name != 'imageIdUrl'
121121
}
122122

123+
if 'sprintDay' in column_mapping:
124+
column_mapping['sprintDay'] = 'Join Sprint Day'
125+
123126
processed_records = []
124127
for item in data:
125128
record = item.dict()
@@ -291,6 +294,12 @@ async def download_with_semaphore(url):
291294
successful_downloads = 0
292295
failed_downloads = 0
293296

297+
cell_width_px = int(worksheet.column_dimensions[image_column_letter].width / self.__EXCEL_COLUMN_WIDTH_FACTOR)
298+
max_image_height_px = 200
299+
300+
successful_downloads = 0
301+
failed_downloads = 0
302+
294303
for idx, result in enumerate(results):
295304
row_idx = idx + 2
296305

@@ -306,9 +315,55 @@ async def download_with_semaphore(url):
306315
if isinstance(result, Image):
307316
try:
308317
img = result
309-
worksheet.row_dimensions[row_idx].height = img.height * self.__EXCEL_ROW_HEIGHT_FACTOR
310-
worksheet.add_image(img, f'{image_column_letter}{row_idx}')
318+
319+
if img.height > max_image_height_px:
320+
scale_factor = max_image_height_px / img.height
321+
img.height = max_image_height_px
322+
img.width = int(img.width * scale_factor)
323+
324+
if img.width > cell_width_px:
325+
scale_factor = (cell_width_px * 0.9) / img.width # 90% of cell width for padding
326+
img.width = int(cell_width_px * 0.9)
327+
img.height = int(img.height * scale_factor)
328+
329+
row_height_points = (img.height * self.__EXCEL_ROW_HEIGHT_FACTOR) + 5
330+
worksheet.row_dimensions[row_idx].height = row_height_points
331+
332+
try:
333+
from openpyxl.drawing.spreadsheet_drawing import (
334+
AnchorMarker,
335+
TwoCellAnchor,
336+
)
337+
338+
padding_x = 19050
339+
padding_y = 19050
340+
341+
start_marker = AnchorMarker(
342+
col=image_column_idx - 1, row=row_idx - 1, colOff=padding_x, rowOff=padding_y
343+
)
344+
345+
end_marker = AnchorMarker(
346+
col=image_column_idx - 1,
347+
row=row_idx - 1,
348+
colOff=int((img.width * 9525) + padding_x),
349+
rowOff=int((img.height * 9525) + padding_y),
350+
)
351+
352+
img.anchor = TwoCellAnchor(editAs='oneCell', _from=start_marker, to=end_marker)
353+
logger.debug(f'Using advanced cell anchoring for row {row_idx}')
354+
355+
except (ImportError, AttributeError, Exception) as anchor_error:
356+
logger.debug(f'Using simple anchoring for row {row_idx}: {anchor_error}')
357+
pass
358+
359+
if hasattr(img, 'anchor') and img.anchor and not isinstance(img.anchor, str):
360+
worksheet.add_image(img)
361+
else:
362+
worksheet.add_image(img, f'{image_column_letter}{row_idx}')
363+
311364
successful_downloads += 1
365+
logger.debug(f'Successfully embedded image in row {row_idx} ({img.width}x{img.height}px)')
366+
312367
except Exception as e:
313368
logger.error(f'Error embedding image in row {row_idx}: {e}')
314369
worksheet.cell(row=row_idx, column=image_column_idx, value='Error: Failed to embed')

0 commit comments

Comments
 (0)