Generate Excel stage-2

This commit is contained in:
2024-11-06 18:29:16 +09:00
parent 768dd6e261
commit 15815d5f06
2 changed files with 25 additions and 4 deletions

View File

@ -416,11 +416,22 @@ class SumasenExcel:
print(f"min_col, min_row, max_col, max_row = {target_min_col}, {target_min_row}, {target_max_col}, {target_max_row}")
# 新しいシートを作成(必要な場合)
if 'Sheet' not in self.workbook.sheetnames:
self.current_sheet = self.workbook.create_sheet('Sheet')
# Get template sheet name from ini file
section_config = self.conf.get_section(self.section_list[0]) # 現在の処理中のセクション
template_sheet_name = section_config.get("template_sheet")
if not template_sheet_name:
raise ValueError("Template sheet name not found in configuration")
# Create new sheet with template name if it doesn't exist
if template_sheet_name not in self.workbook.sheetnames:
self.current_sheet = self.workbook.create_sheet(template_sheet_name)
else:
self.current_sheet = self.workbook['Sheet']
self.current_sheet = self.workbook[template_sheet_name]
# Remove default sheet if it exists
if 'Sheet' in self.workbook.sheetnames:
del self.workbook['Sheet']
# Copy column widths
for col in range(orig_min_col, orig_max_col + 1):
@ -429,6 +440,16 @@ class SumasenExcel:
self.current_sheet.column_dimensions[col_letter].width = \
self.template_sheet.column_dimensions[col_letter].width
# Copy row heights
for row in range(orig_min_row, orig_max_row + 1):
target_row = row - orig_min_row + target_min_row
if row in self.template_sheet.row_dimensions:
source_height = self.template_sheet.row_dimensions[row].height
if source_height is not None:
if target_row not in self.current_sheet.row_dimensions:
self.current_sheet.row_dimensions[target_row] = openpyxl.worksheet.dimensions.RowDimension(target_row)
self.current_sheet.row_dimensions[target_row].height = source_height
# 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))