Fix PDF issue
This commit is contained in:
@ -305,6 +305,12 @@ class SumasenExcel:
|
||||
logging.error(f"Error in proceed_group: {str(e)}")
|
||||
return {"status": False, "message": f"Exception in proceed_group : Error generating report: {str(e)}"}
|
||||
|
||||
def pixels_to_EMU(pixels):
|
||||
"""
|
||||
Convert pixels to EMU (English Metric Units)
|
||||
EMU = pixels * 9525
|
||||
"""
|
||||
return int(pixels * 9525)
|
||||
|
||||
def format_cell_value(self, field_value, cell):
|
||||
"""セルの値を適切な形式に変換する
|
||||
@ -369,7 +375,8 @@ class SumasenExcel:
|
||||
|
||||
# ワークシートを取得
|
||||
worksheet = cell.parent
|
||||
|
||||
|
||||
logging.info('step-1')
|
||||
try:
|
||||
# 列の幅を取得(文字単位からピクセルに変換)
|
||||
column_letter = get_column_letter(cell.column)
|
||||
@ -378,6 +385,7 @@ class SumasenExcel:
|
||||
except Exception:
|
||||
cell_width = 100 # デフォルト値
|
||||
|
||||
logging.info('step-2')
|
||||
try:
|
||||
# 行の高さを取得(ポイント単位からピクセルに変換)
|
||||
row_height = worksheet.row_dimensions[cell.row].height
|
||||
@ -385,6 +393,7 @@ class SumasenExcel:
|
||||
except Exception:
|
||||
cell_height = 20 # デフォルト値
|
||||
|
||||
logging.info('step-3')
|
||||
# マージンの設定(ピクセル単位)
|
||||
margin_horizontal = 4 # 左右のマージン
|
||||
margin_vertical = 2 # 上下のマージン
|
||||
@ -393,6 +402,7 @@ class SumasenExcel:
|
||||
effective_cell_width = cell_width - (margin_horizontal * 2)
|
||||
effective_cell_height = cell_height - (margin_vertical * 2)
|
||||
|
||||
logging.info('step-4')
|
||||
# 最小サイズを設定(マージンを考慮)
|
||||
effective_cell_width = max(effective_cell_width, 92) # 100 - (4 * 2)
|
||||
effective_cell_height = max(effective_cell_height, 16) # 20 - (2 * 2)
|
||||
@ -403,6 +413,7 @@ class SumasenExcel:
|
||||
effective_cell_width = min(effective_cell_width, max_width)
|
||||
effective_cell_height = min(effective_cell_height, max_height)
|
||||
|
||||
logging.info('step-5')
|
||||
# アスペクト比を保持しながらリサイズ
|
||||
img_width, img_height = img.size
|
||||
img_aspect = img_width / img_height
|
||||
@ -414,7 +425,11 @@ class SumasenExcel:
|
||||
else:
|
||||
height = effective_cell_height
|
||||
width = int(height * img_aspect)
|
||||
|
||||
|
||||
# 画像処理部分の修正
|
||||
#from openpyxl.utils.units import pixels_to_EMU
|
||||
|
||||
logging.info('step-6')
|
||||
# 画像をリサイズ
|
||||
img_resized = img.resize((width, height), Image.BICUBIC)
|
||||
|
||||
@ -422,29 +437,47 @@ class SumasenExcel:
|
||||
img_byte_arr = io.BytesIO()
|
||||
img_resized.save(img_byte_arr, format='JPEG', quality=85, optimize=True)
|
||||
img_byte_arr.seek(0)
|
||||
|
||||
# OpenPyXLのImageオブジェクトを作成
|
||||
from openpyxl.drawing.image import Image as XLImage
|
||||
excel_image = XLImage(img_byte_arr)
|
||||
|
||||
# 画像のオフセット位置を設定(マージンを適用)
|
||||
from openpyxl.drawing.spreadsheet_drawing import OneCellAnchor, AnchorMarker
|
||||
from openpyxl.utils.units import pixels_to_EMU
|
||||
|
||||
# セルの左上を基準に、マージン分オフセットした位置に配置
|
||||
col_offset = pixels_to_EMU(margin_horizontal)
|
||||
row_offset = pixels_to_EMU(margin_vertical)
|
||||
|
||||
# マージンを考慮した配置
|
||||
marker = AnchorMarker(col=cell.column - 1, colOff=col_offset,
|
||||
row=cell.row - 1, rowOff=row_offset)
|
||||
anchor = OneCellAnchor(_from=marker, ext=None)
|
||||
|
||||
logging.info('step-7')
|
||||
# OpenPyXLのImageオブジェクトを作成
|
||||
excel_image = XLImage(img_byte_arr)
|
||||
|
||||
# EMUユニットでのサイズを設定
|
||||
excel_image.width = pixels_to_EMU(width)
|
||||
excel_image.height = pixels_to_EMU(height)
|
||||
|
||||
# 正しいアンカー設定
|
||||
from openpyxl.drawing.spreadsheet_drawing import OneCellAnchor, AnchorMarker
|
||||
from openpyxl.drawing.xdr import XDRPositiveSize2D
|
||||
|
||||
logging.info('step-8')
|
||||
marker = AnchorMarker(
|
||||
col=cell.column - 1,
|
||||
colOff=pixels_to_EMU(margin_horizontal),
|
||||
row=cell.row - 1,
|
||||
rowOff=pixels_to_EMU(margin_vertical)
|
||||
)
|
||||
|
||||
# XDRPositiveSize2Dを使用して画像サイズを設定
|
||||
size = XDRPositiveSize2D(
|
||||
cx=pixels_to_EMU(width),
|
||||
cy=pixels_to_EMU(height)
|
||||
)
|
||||
|
||||
anchor = OneCellAnchor(_from=marker, ext=size)
|
||||
excel_image.anchor = anchor
|
||||
|
||||
|
||||
|
||||
logging.info('step-9')
|
||||
# 画像をワークシートに追加
|
||||
worksheet.add_image(excel_image)
|
||||
#cell.parent.add_image(excel_image)
|
||||
|
||||
logging.info('step-A')
|
||||
# メモリ解放
|
||||
#img_byte_arr.close()
|
||||
|
||||
return ""
|
||||
|
||||
except Exception as e:
|
||||
@ -467,7 +500,28 @@ class SumasenExcel:
|
||||
# その他の場合は文字列に変換
|
||||
return str(field_value)
|
||||
|
||||
def verify_image_insertion(self, worksheet, cell, image):
|
||||
"""画像の挿入を検証するためのヘルパーメソッド"""
|
||||
try:
|
||||
# 画像が実際にワークシートに追加されているか確認
|
||||
images_in_sheet = worksheet._images
|
||||
if not images_in_sheet:
|
||||
logging.warning(f"No images found in worksheet at cell {cell.coordinate}")
|
||||
return False
|
||||
|
||||
# 画像のアンカー位置を確認
|
||||
last_image = images_in_sheet[-1]
|
||||
anchor = last_image.anchor
|
||||
if not anchor:
|
||||
logging.warning(f"Image anchor not set properly at cell {cell.coordinate}")
|
||||
return False
|
||||
|
||||
logging.info(f"Image successfully inserted at cell {cell.coordinate}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error verifying image insertion: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def proceed_one_record(self,table:str,where:str,group_range:str,variables: Dict[str, Any]):
|
||||
|
||||
Reference in New Issue
Block a user