From b6095939068aa89ead6ea2c673e891e9ac73d77c Mon Sep 17 00:00:00 2001 From: chengzongjie Date: Thu, 21 May 2026 10:45:05 +0800 Subject: [PATCH] fix: rebase --- main.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index fbf8086..8f62bca 100644 --- a/main.py +++ b/main.py @@ -15,8 +15,6 @@ try: except ImportError: win32com = None -BASE_WIDTH = 5.83 - # openpyxl 未设置时的默认列宽/行高 DEFAULT_COLUMN_WIDTH = 8.43 DEFAULT_ROW_HEIGHT_PT = 15.0 @@ -25,7 +23,9 @@ MAX_WIDTH_PT = 1702.0 def get_char_size( + font_path: str, font_size_pt: float, char: str = "中" + ) -> tuple[float, float]: """返回指定字体和字号下单个字符的宽度和高度,单位为磅(pt)。""" dpi = 96 @@ -231,7 +231,7 @@ def can_fit_chinese_chars(col_width_chars: float, zh_font_pt: float) -> int: pt = px * 72 / dpi 基准字符宽度:Calibri 11pt '0' = 5.83 """ - return int(col_width_chars * BASE_WIDTH / zh_font_pt) + return int((col_width_chars - 0.5) * base_width() / zh_font_pt) def default_row_height_pt(sheet) -> float: @@ -263,12 +263,12 @@ def set_column_width_for_hanzi( zh_font_pt: float = 8.0, ) -> None: """设置指定列宽,使其仅能容纳指定数量的汉字宽度。""" - hanwidth = get_char_size("simsun.ttc", zh_font_pt)[0] - width = hanzi_count * hanwidth / base_width() - print("设置宽度:", width) + han_width = get_char_size("simsun.ttc", zh_font_pt)[0] + width = hanzi_count * han_width / base_width() for column_letter in column_letters: - sheet.column_dimensions[column_letter].width = width + 0.6 + print(column_letter, "设置宽度:", width) + sheet.column_dimensions[column_letter].width = width + 0.5 sheet.column_dimensions[column_letter].bestFit = True sheet.column_dimensions[column_letter].hidden = False @@ -331,7 +331,7 @@ def print_area_size_landscape(sheet) -> tuple[float, float]: for col_idx in range(min_col, max_col + 1): col_letter = get_column_letter(col_idx) col_width = sheet.column_dimensions[col_letter].width or DEFAULT_COLUMN_WIDTH # pyright: ignore[reportAttributeAccessIssue] - width_pt += col_width * BASE_WIDTH + width_pt += col_width * base_width() height_pt = 0.0 default_height = default_row_height_pt(sheet) @@ -358,7 +358,7 @@ def is_cell_content_overflow(cell: Cell) -> bool: chinese_capacity = can_fit_chinese_chars(avail_col_width, 8.0) print("列宽为", avail_col_width, "可容纳汉字", chinese_capacity, "个") cell_height_pt = cell_total_height_pt(cell) - print("高度:", cell_height_pt) + # print("高度:", cell_height_pt) # 2. 不开自动换行:只判宽度溢出 if not align.wrap_text: @@ -369,7 +369,7 @@ def is_cell_content_overflow(cell: Cell) -> bool: lines = text.split("\n") total_wrap_lines = 0 for line in lines: - wrapped = wrap_line_words(line, avail_col_width * BASE_WIDTH) + wrapped = wrap_line_words(line, avail_col_width * base_width()) print(line, "->", len(wrapped), "行") for i, wrapped_line in enumerate(wrapped, start=1): print(f" {i:02d}: {wrapped_line}")