Fix printing area and options step4

This commit is contained in:
2024-11-11 08:53:23 +09:00
parent fd973575be
commit a3f602b360

View File

@ -891,29 +891,59 @@ class SumasenExcel:
target_sheet: コピー先のワークシート target_sheet: コピー先のワークシート
""" """
try: try:
if not hasattr(source_sheet, 'page_setup') or not hasattr(target_sheet, 'page_setup'):
logging.error("Sheet does not have page_setup attribute")
return
source_setup = source_sheet.page_setup source_setup = source_sheet.page_setup
target_setup = target_sheet.page_setup target_setup = target_sheet.page_setup
# シートプロパティの初期化確認
if not hasattr(target_sheet, 'sheet_properties'):
target_sheet.sheet_properties = openpyxl.worksheet.properties.WorksheetProperties()
# 基本的な印刷設定の初期化
target_setup.paperSize = source_setup.paperSize
target_setup.orientation = source_setup.orientation
# 印刷の拡大縮小設定
target_sheet.sheet_properties.pageSetUpPr = openpyxl.worksheet.properties.PageSetupProperties(
fitToPage=True
)
# まず現在の設定をクリア # まず現在の設定をクリア
target_setup.scale = None target_setup.scale = None
target_setup.fitToWidth = None target_setup.fitToWidth = None
target_setup.fitToHeight = None target_setup.fitToHeight = None
# fitToPageの設定を最初に行う # fitToPage の設定
target_setup.fitToPage = source_setup.fitToPage if hasattr(source_setup, 'fitToPage'):
fitToPage = source_setup.fitToPage
else:
# テンプレートに設定がない場合のデフォルト
fitToPage = True
if source_setup.fitToPage: if fitToPage:
# 「次のページに合わせる」が選択されている場合 # 「次のページに合わせる」設定
target_setup.fitToPage = True
if hasattr(source_setup, 'fitToWidth'): if hasattr(source_setup, 'fitToWidth'):
target_setup.fitToWidth = source_setup.fitToWidth target_setup.fitToWidth = source_setup.fitToWidth or 1
else:
target_setup.fitToWidth = 1
if hasattr(source_setup, 'fitToHeight'): if hasattr(source_setup, 'fitToHeight'):
target_setup.fitToHeight = source_setup.fitToHeight target_setup.fitToHeight = source_setup.fitToHeight
else:
target_setup.fitToHeight = None
else: else:
# 拡大縮小率指定されている場合 # 拡大縮小率指定
target_setup.fitToPage = False
if hasattr(source_setup, 'scale'): if hasattr(source_setup, 'scale'):
target_setup.scale = source_setup.scale target_setup.scale = source_setup.scale or 100
else:
target_setup.scale = 100
logging.info(f"Print scaling settings - " logging.info(f"Print scaling settings copied - "
f"fitToPage: {target_setup.fitToPage}, " f"fitToPage: {target_setup.fitToPage}, "
f"fitToWidth: {target_setup.fitToWidth}, " f"fitToWidth: {target_setup.fitToWidth}, "
f"fitToHeight: {target_setup.fitToHeight}, " f"fitToHeight: {target_setup.fitToHeight}, "
@ -921,7 +951,8 @@ class SumasenExcel:
except Exception as e: except Exception as e:
logging.error(f"Error copying print scaling settings: {str(e)}") logging.error(f"Error copying print scaling settings: {str(e)}")
raise import traceback
logging.error(traceback.format_exc())
def copy_cell_formatting(self, source_cell, target_cell): def copy_cell_formatting(self, source_cell, target_cell):
""" """
@ -1130,43 +1161,61 @@ class SumasenExcel:
# 拡大縮小印刷の設定を正確にコピー # 拡大縮小印刷の設定を正確にコピー
self.copy_print_scaling_settings(self.template_sheet, self.current_sheet) self.copy_print_scaling_settings(self.template_sheet, self.current_sheet)
# その他ページ設定のコピー # シートプロパティの確認と初期化
target_page_setup = self.current_sheet.page_setup if not hasattr(self.current_sheet, 'sheet_properties'):
source_page_setup = self.template_sheet.page_setup self.current_sheet.sheet_properties = openpyxl.worksheet.properties.WorksheetProperties()
# ページ設定属性のコピー # 印刷範囲の設定をコピーして調整
page_setup_attrs = [ if hasattr(self.template_sheet, 'print_area') and self.template_sheet.print_area:
'orientation', # 印刷の向き last_data_row = self.get_last_data_row()
'paperSize', # 用紙サイズ adjusted_print_area = self.adjust_print_area(
'firstPageNumber', # 開始ページ番号 self.template_sheet.print_area,
'useFirstPageNumber', # 開始ページ番号の使用 last_data_row
'blackAndWhite', # モノクロ印刷 )
'draft', # 下書きモード if adjusted_print_area:
'cellComments', # コメントの印刷方法 self.current_sheet.print_area = adjusted_print_area
'errors', # エラーの表示方法 logging.info(f"Print area set to: {adjusted_print_area}")
'horizontalDpi', # 水平DPI
'verticalDpi' # 垂直DPI
]
for attr in page_setup_attrs: # ページ設定のコピー
try: if hasattr(self.template_sheet, 'page_setup') and hasattr(self.current_sheet, 'page_setup'):
if hasattr(source_page_setup, attr):
setattr(target_page_setup, attr, getattr(source_page_setup, attr))
except Exception as e:
logging.warning(f"Could not copy page setup attribute {attr}: {str(e)}")
# その他ページ設定のコピー
target_page_setup = self.current_sheet.page_setup
source_page_setup = self.template_sheet.page_setup
# Copy margins # ページ設定属性のコピー
target_margins = self.current_sheet.page_margins page_setup_attrs = [
source_margins = self.template_sheet.page_margins 'orientation', # 印刷の向き
'paperSize', # 用紙サイズ
'firstPageNumber', # 開始ページ番号
'useFirstPageNumber', # 開始ページ番号の使用
'blackAndWhite', # モノクロ印刷
'draft', # 下書きモード
'cellComments', # コメントの印刷方法
'errors', # エラーの表示方法
'horizontalDpi', # 水平DPI
'verticalDpi' # 垂直DPI
]
margin_attrs = ['left', 'right', 'top', 'bottom', 'header', 'footer'] for attr in page_setup_attrs:
for attr in margin_attrs: try:
try: if hasattr(source_page_setup, attr):
if hasattr(source_margins, attr): setattr(target_page_setup, attr, getattr(source_page_setup, attr))
setattr(target_margins, attr, getattr(source_margins, attr)) except Exception as e:
except Exception as e: logging.warning(f"Could not copy page setup attribute {attr}: {str(e)}")
logging.warning(f"Could not copy margin attribute {attr}: {str(e)}")
# 余白設定のコピー
if hasattr(self.template_sheet, 'page_margins') and hasattr(self.current_sheet, 'page_margins'):
target_margins = self.current_sheet.page_margins
source_margins = self.template_sheet.page_margins
margin_attrs = ['left', 'right', 'top', 'bottom', 'header', 'footer']
for attr in margin_attrs:
try:
if hasattr(source_margins, attr):
setattr(target_margins, attr, getattr(source_margins, attr))
except Exception as e:
logging.warning(f"Could not copy margin attribute {attr}: {str(e)}")
# Copy print options # Copy print options
target_print = self.current_sheet.print_options target_print = self.current_sheet.print_options