Generate Excel stage 2

This commit is contained in:
2024-11-06 17:56:24 +09:00
parent ceb783d6bd
commit 139c0987bc
3 changed files with 206 additions and 9 deletions

View File

@ -418,17 +418,38 @@ class SumasenExcel:
self.current_sheet = self.workbook.create_sheet('Sheet')
else:
self.current_sheet = self.workbook['Sheet']
# セルの内容とスタイルをコピー
for row in range(min_row, max_row + 1):
for col in range(min_col, max_col + 1):
# Copy column widths
for col in range(orig_min_col, orig_max_col + 1):
col_letter = get_column_letter(col)
if col_letter in self.template_sheet.column_dimensions:
self.current_sheet.column_dimensions[col_letter].width = \
self.template_sheet.column_dimensions[col_letter].width
# Copy merged cells
for merged_range in self.template_sheet.merged_cells:
min_col, min_row, max_col, max_row = range_boundaries(str(merged_range))
# Check if merge range intersects with our copy range
if (min_col >= orig_min_col and max_col <= orig_max_col and
min_row >= orig_min_row and max_row <= orig_max_row):
# Calculate target merge range
target_merge_min_row = target_min_row + (min_row - orig_min_row)
target_merge_max_row = target_min_row + (max_row - orig_min_row)
target_merge_range = f"{get_column_letter(min_col)}{target_merge_min_row}:" \
f"{get_column_letter(max_col)}{target_merge_max_row}"
self.current_sheet.merge_cells(target_merge_range)
# Copy cell contents and styles
row_offset = target_min_row - orig_min_row
for row in range(orig_min_row, orig_max_row + 1):
for col in range(orig_min_col, orig_max_col + 1):
source_cell = self.template_sheet.cell(row=row, column=col)
target_cell = self.current_sheet.cell(row=row, column=col)
# 値のコピー
target_cell = self.current_sheet.cell(row=row+row_offset, column=col)
# Copy value
target_cell.value = source_cell.value
# スタイルのコピー
# Copy styles
if source_cell.has_style:
target_cell.font = copy(source_cell.font)
target_cell.border = copy(source_cell.border)
@ -436,6 +457,38 @@ class SumasenExcel:
target_cell.number_format = source_cell.number_format
target_cell.protection = copy(source_cell.protection)
target_cell.alignment = copy(source_cell.alignment)
# Copy page setup
target_page_setup = self.current_sheet.page_setup
source_page_setup = self.template_sheet.page_setup
# Copy all page setup attributes
target_page_setup.paperSize = source_page_setup.paperSize
target_page_setup.orientation = source_page_setup.orientation
target_page_setup.fitToHeight = source_page_setup.fitToHeight
target_page_setup.fitToWidth = source_page_setup.fitToWidth
target_page_setup.zoom = source_page_setup.zoom
target_page_setup.scale = source_page_setup.scale
# Copy margins
target_margins = self.current_sheet.page_margins
source_margins = self.template_sheet.page_margins
target_margins.left = source_margins.left
target_margins.right = source_margins.right
target_margins.top = source_margins.top
target_margins.bottom = source_margins.bottom
target_margins.header = source_margins.header
target_margins.footer = source_margins.footer
# Copy print options
target_print = self.current_sheet.print_options
source_print = self.template_sheet.print_options
target_print.horizontalCentered = source_print.horizontalCentered
target_print.verticalCentered = source_print.verticalCentered
target_print.gridLines = source_print.gridLines
target_print.gridLinesSet = source_print.gridLinesSet
return {"status": True, "message": "Successfully copied template range"}

Binary file not shown.