style: normalize code formatting across the codebase
- Standardize quote style (single to double quotes) - Improve code formatting consistency - Apply formatting to utilities, GUI components, and tools - Update imports and docstrings for consistency Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,12 +15,18 @@ def number_to_excel_col(n):
|
||||
result = ""
|
||||
while n > 0:
|
||||
n -= 1
|
||||
result = chr(n % 26 + ord('A')) + result
|
||||
result = chr(n % 26 + ord("A")) + result
|
||||
n //= 26
|
||||
return result
|
||||
|
||||
|
||||
def excel_to_markdown(input_file, output_file=None, sheet_name=0, include_row_numbers=True, include_col_numbers=True):
|
||||
def excel_to_markdown(
|
||||
input_file,
|
||||
output_file=None,
|
||||
sheet_name=0,
|
||||
include_row_numbers=True,
|
||||
include_col_numbers=True,
|
||||
):
|
||||
"""
|
||||
将Excel文件转换为Markdown表格
|
||||
|
||||
@@ -39,7 +45,7 @@ def excel_to_markdown(input_file, output_file=None, sheet_name=0, include_row_nu
|
||||
|
||||
# 设置默认输出文件名
|
||||
if output_file is None:
|
||||
output_file = input_path.with_suffix('.md')
|
||||
output_file = input_path.with_suffix(".md")
|
||||
else:
|
||||
output_file = Path(output_file)
|
||||
|
||||
@@ -80,7 +86,7 @@ def excel_to_markdown(input_file, output_file=None, sheet_name=0, include_row_nu
|
||||
|
||||
for sheet_key, df in dfs.items():
|
||||
# 转换数据为字符串,处理NaN值
|
||||
df = df.fillna('')
|
||||
df = df.fillna("")
|
||||
df = df.astype(str)
|
||||
|
||||
# 生成Markdown表格
|
||||
@@ -92,10 +98,14 @@ def excel_to_markdown(input_file, output_file=None, sheet_name=0, include_row_nu
|
||||
|
||||
# 添加列号行
|
||||
if include_col_numbers:
|
||||
col_headers = [''] if include_row_numbers else []
|
||||
col_headers.extend(number_to_excel_col(i + 1) for i in range(len(df.columns)))
|
||||
markdown_lines.append('| ' + ' | '.join(col_headers) + ' |')
|
||||
markdown_lines.append('| ' + ' | '.join(['---' for _ in col_headers]) + ' |')
|
||||
col_headers = [""] if include_row_numbers else []
|
||||
col_headers.extend(
|
||||
number_to_excel_col(i + 1) for i in range(len(df.columns))
|
||||
)
|
||||
markdown_lines.append("| " + " | ".join(col_headers) + " |")
|
||||
markdown_lines.append(
|
||||
"| " + " | ".join(["---" for _ in col_headers]) + " |"
|
||||
)
|
||||
|
||||
# 添加数据行
|
||||
for idx, row in df.iterrows():
|
||||
@@ -103,15 +113,17 @@ def excel_to_markdown(input_file, output_file=None, sheet_name=0, include_row_nu
|
||||
if include_row_numbers:
|
||||
row_data.append(str(idx + 1))
|
||||
row_data.extend(row)
|
||||
markdown_lines.append('| ' + ' | '.join(row_data) + ' |')
|
||||
markdown_lines.append("| " + " | ".join(row_data) + " |")
|
||||
|
||||
# 添加统计信息
|
||||
markdown_lines.append(f"\n**统计信息:**")
|
||||
markdown_lines.append(f"- 总行数: {len(df)}")
|
||||
markdown_lines.append(f"- 总列数: {len(df.columns)}")
|
||||
markdown_lines.append(f"- 数据范围: A1:{number_to_excel_col(len(df.columns))}{len(df)}")
|
||||
markdown_lines.append(
|
||||
f"- 数据范围: A1:{number_to_excel_col(len(df.columns))}{len(df)}"
|
||||
)
|
||||
|
||||
all_sheets_content[sheet_key] = '\n'.join(markdown_lines)
|
||||
all_sheets_content[sheet_key] = "\n".join(markdown_lines)
|
||||
|
||||
# 写入文件
|
||||
if len(dfs) == 1:
|
||||
@@ -120,7 +132,7 @@ def excel_to_markdown(input_file, output_file=None, sheet_name=0, include_row_nu
|
||||
output_content = f"# {input_path.stem}\n\n"
|
||||
output_content += f"从 `{input_file}` (工作表: {sheet_key}) 转换\n\n"
|
||||
output_content += all_sheets_content[sheet_key]
|
||||
output_file.write_text(output_content, encoding='utf-8')
|
||||
output_file.write_text(output_content, encoding="utf-8")
|
||||
|
||||
print(f"✓ 转换成功!")
|
||||
print(f" 输入文件: {input_file}")
|
||||
@@ -133,11 +145,19 @@ def excel_to_markdown(input_file, output_file=None, sheet_name=0, include_row_nu
|
||||
except Exception as e:
|
||||
print(f"错误: {str(e)}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, include_row_numbers=True, include_col_numbers=True, merge_to_one_file=True):
|
||||
def convert_multiple_sheets(
|
||||
input_file,
|
||||
output_file=None,
|
||||
sheet_names=None,
|
||||
include_row_numbers=True,
|
||||
include_col_numbers=True,
|
||||
merge_to_one_file=True,
|
||||
):
|
||||
"""
|
||||
转换多个工作表
|
||||
|
||||
@@ -157,7 +177,7 @@ def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, incl
|
||||
|
||||
# 设置默认输出文件名
|
||||
if output_file is None:
|
||||
output_file = input_path.with_suffix('.md')
|
||||
output_file = input_path.with_suffix(".md")
|
||||
else:
|
||||
output_file = Path(output_file)
|
||||
|
||||
@@ -200,7 +220,7 @@ def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, incl
|
||||
|
||||
for sheet_key, df in dfs.items():
|
||||
# 转换数据为字符串,处理NaN值
|
||||
df = df.fillna('')
|
||||
df = df.fillna("")
|
||||
df = df.astype(str)
|
||||
|
||||
# 生成Markdown表格
|
||||
@@ -212,10 +232,14 @@ def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, incl
|
||||
|
||||
# 添加列号行
|
||||
if include_col_numbers:
|
||||
col_headers = [''] if include_row_numbers else []
|
||||
col_headers.extend(number_to_excel_col(i + 1) for i in range(len(df.columns)))
|
||||
markdown_lines.append('| ' + ' | '.join(col_headers) + ' |')
|
||||
markdown_lines.append('| ' + ' | '.join(['---' for _ in col_headers]) + ' |')
|
||||
col_headers = [""] if include_row_numbers else []
|
||||
col_headers.extend(
|
||||
number_to_excel_col(i + 1) for i in range(len(df.columns))
|
||||
)
|
||||
markdown_lines.append("| " + " | ".join(col_headers) + " |")
|
||||
markdown_lines.append(
|
||||
"| " + " | ".join(["---" for _ in col_headers]) + " |"
|
||||
)
|
||||
|
||||
# 添加数据行
|
||||
for idx, row in df.iterrows():
|
||||
@@ -223,15 +247,17 @@ def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, incl
|
||||
if include_row_numbers:
|
||||
row_data.append(str(idx + 1))
|
||||
row_data.extend(row)
|
||||
markdown_lines.append('| ' + ' | '.join(row_data) + ' |')
|
||||
markdown_lines.append("| " + " | ".join(row_data) + " |")
|
||||
|
||||
# 添加统计信息
|
||||
markdown_lines.append(f"\n**统计信息:**")
|
||||
markdown_lines.append(f"- 总行数: {len(df)}")
|
||||
markdown_lines.append(f"- 总列数: {len(df.columns)}")
|
||||
markdown_lines.append(f"- 数据范围: A1:{number_to_excel_col(len(df.columns))}{len(df)}")
|
||||
markdown_lines.append(
|
||||
f"- 数据范围: A1:{number_to_excel_col(len(df.columns))}{len(df)}"
|
||||
)
|
||||
|
||||
all_sheets_content[sheet_key] = '\n'.join(markdown_lines)
|
||||
all_sheets_content[sheet_key] = "\n".join(markdown_lines)
|
||||
|
||||
# 写入文件
|
||||
if merge_to_one_file:
|
||||
@@ -244,7 +270,7 @@ def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, incl
|
||||
if sheet_key in all_sheets_content:
|
||||
output_content += all_sheets_content[sheet_key] + "\n\n---\n\n"
|
||||
|
||||
output_file.write_text(output_content, encoding='utf-8')
|
||||
output_file.write_text(output_content, encoding="utf-8")
|
||||
|
||||
print(f"✓ 转换成功!")
|
||||
print(f" 输入文件: {input_file}")
|
||||
@@ -252,7 +278,9 @@ def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, incl
|
||||
print(f" 工作表数量: {len(dfs)}")
|
||||
for sheet_key in sheet_names:
|
||||
if sheet_key in dfs:
|
||||
print(f" - {sheet_key}: {len(dfs[sheet_key])}行 x {len(dfs[sheet_key].columns)}列")
|
||||
print(
|
||||
f" - {sheet_key}: {len(dfs[sheet_key])}行 x {len(dfs[sheet_key].columns)}列"
|
||||
)
|
||||
else:
|
||||
# 分别输出到多个文件
|
||||
output_stem = output_file.stem
|
||||
@@ -271,7 +299,7 @@ def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, incl
|
||||
output_content += f"从 `{input_file}` (工作表: {sheet_key}) 转换\n\n"
|
||||
output_content += all_sheets_content[sheet_key]
|
||||
|
||||
sheet_output_file.write_text(output_content, encoding='utf-8')
|
||||
sheet_output_file.write_text(output_content, encoding="utf-8")
|
||||
|
||||
print(f"✓ 转换成功!")
|
||||
print(f" 输入文件: {input_file}")
|
||||
@@ -280,13 +308,16 @@ def convert_multiple_sheets(input_file, output_file=None, sheet_names=None, incl
|
||||
for sheet_key in sheet_names:
|
||||
if sheet_key in dfs:
|
||||
sheet_filename = f"{output_stem}_{sheet_key}{output_suffix}"
|
||||
print(f" - {sheet_key} -> {sheet_filename} ({len(dfs[sheet_key])}行 x {len(dfs[sheet_key].columns)}列)")
|
||||
print(
|
||||
f" - {sheet_key} -> {sheet_filename} ({len(dfs[sheet_key])}行 x {len(dfs[sheet_key].columns)}列)"
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"错误: {str(e)}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
@@ -305,7 +336,7 @@ OUTPUT_FILE = None # 或指定 r"D:\path\to\output.md"
|
||||
# 可以是单个值: 0 或 "Sheet1"
|
||||
# 可以是列表: [0, 1, 2] 或 ["Sheet1", "Sheet2", "Sheet3"]
|
||||
# 可以是 None 表示读取所有工作表
|
||||
SHEET_NAMES = [0, 1,2,3] # 指定多个工作表索引
|
||||
SHEET_NAMES = [0, 1, 2, 3] # 指定多个工作表索引
|
||||
|
||||
# 是否包含行号(可选,默认为True)
|
||||
INCLUDE_ROW_NUMBERS = True
|
||||
@@ -332,19 +363,23 @@ def main():
|
||||
sheet_names=SHEET_NAMES,
|
||||
include_row_numbers=INCLUDE_ROW_NUMBERS,
|
||||
include_col_numbers=INCLUDE_COL_NUMBERS,
|
||||
merge_to_one_file=MULTI_SHEETS_TO_ONE_FILE
|
||||
merge_to_one_file=MULTI_SHEETS_TO_ONE_FILE,
|
||||
)
|
||||
else:
|
||||
# 单个工作表
|
||||
sheet_name = SHEET_NAMES if isinstance(SHEET_NAMES, list) and len(SHEET_NAMES) == 1 else SHEET_NAMES
|
||||
sheet_name = (
|
||||
SHEET_NAMES
|
||||
if isinstance(SHEET_NAMES, list) and len(SHEET_NAMES) == 1
|
||||
else SHEET_NAMES
|
||||
)
|
||||
excel_to_markdown(
|
||||
input_file=INPUT_FILE,
|
||||
output_file=OUTPUT_FILE,
|
||||
sheet_name=sheet_name,
|
||||
include_row_numbers=INCLUDE_ROW_NUMBERS,
|
||||
include_col_numbers=INCLUDE_COL_NUMBERS
|
||||
include_col_numbers=INCLUDE_COL_NUMBERS,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user