fix: rebase

This commit is contained in:
chengzongjie 2026-05-21 10:45:05 +08:00
parent 42ad3135e6
commit b609593906

20
main.py
View File

@ -15,8 +15,6 @@ try:
except ImportError: except ImportError:
win32com = None win32com = None
BASE_WIDTH = 5.83
# openpyxl 未设置时的默认列宽/行高 # openpyxl 未设置时的默认列宽/行高
DEFAULT_COLUMN_WIDTH = 8.43 DEFAULT_COLUMN_WIDTH = 8.43
DEFAULT_ROW_HEIGHT_PT = 15.0 DEFAULT_ROW_HEIGHT_PT = 15.0
@ -25,7 +23,9 @@ MAX_WIDTH_PT = 1702.0
def get_char_size( def get_char_size(
font_path: str, font_size_pt: float, char: str = "" font_path: str, font_size_pt: float, char: str = ""
) -> tuple[float, float]: ) -> tuple[float, float]:
"""返回指定字体和字号下单个字符的宽度和高度,单位为磅(pt)。""" """返回指定字体和字号下单个字符的宽度和高度,单位为磅(pt)。"""
dpi = 96 dpi = 96
@ -231,7 +231,7 @@ def can_fit_chinese_chars(col_width_chars: float, zh_font_pt: float) -> int:
pt = px * 72 / dpi pt = px * 72 / dpi
基准字符宽度Calibri 11pt '0' = 5.83 基准字符宽度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: def default_row_height_pt(sheet) -> float:
@ -263,12 +263,12 @@ def set_column_width_for_hanzi(
zh_font_pt: float = 8.0, zh_font_pt: float = 8.0,
) -> None: ) -> None:
"""设置指定列宽,使其仅能容纳指定数量的汉字宽度。""" """设置指定列宽,使其仅能容纳指定数量的汉字宽度。"""
hanwidth = get_char_size("simsun.ttc", zh_font_pt)[0] han_width = get_char_size("simsun.ttc", zh_font_pt)[0]
width = hanzi_count * hanwidth / base_width() width = hanzi_count * han_width / base_width()
print("设置宽度:", width)
for column_letter in column_letters: 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].bestFit = True
sheet.column_dimensions[column_letter].hidden = False 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): for col_idx in range(min_col, max_col + 1):
col_letter = get_column_letter(col_idx) col_letter = get_column_letter(col_idx)
col_width = sheet.column_dimensions[col_letter].width or DEFAULT_COLUMN_WIDTH # pyright: ignore[reportAttributeAccessIssue] 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 height_pt = 0.0
default_height = default_row_height_pt(sheet) 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) chinese_capacity = can_fit_chinese_chars(avail_col_width, 8.0)
print("列宽为", avail_col_width, "可容纳汉字", chinese_capacity, "") print("列宽为", avail_col_width, "可容纳汉字", chinese_capacity, "")
cell_height_pt = cell_total_height_pt(cell) cell_height_pt = cell_total_height_pt(cell)
print("高度:", cell_height_pt) # print("高度:", cell_height_pt)
# 2. 不开自动换行:只判宽度溢出 # 2. 不开自动换行:只判宽度溢出
if not align.wrap_text: if not align.wrap_text:
@ -369,7 +369,7 @@ def is_cell_content_overflow(cell: Cell) -> bool:
lines = text.split("\n") lines = text.split("\n")
total_wrap_lines = 0 total_wrap_lines = 0
for line in lines: 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), "") print(line, "->", len(wrapped), "")
for i, wrapped_line in enumerate(wrapped, start=1): for i, wrapped_line in enumerate(wrapped, start=1):
print(f" {i:02d}: {wrapped_line}") print(f" {i:02d}: {wrapped_line}")