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:
@@ -1,14 +1,16 @@
|
||||
"""
|
||||
分析 Excel 文件的数据结构
|
||||
"""
|
||||
|
||||
import pandas as pd
|
||||
import openpyxl
|
||||
import sys
|
||||
|
||||
# 设置输出编码
|
||||
if sys.platform == 'win32':
|
||||
if sys.platform == "win32":
|
||||
import io
|
||||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||||
|
||||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
|
||||
|
||||
# 读取 Excel 文件
|
||||
file_path = "data/导出文件.xlsx"
|
||||
@@ -52,12 +54,12 @@ for sheet_name in sheet_names:
|
||||
for i, col in enumerate(df.columns):
|
||||
print(f" 列 {i}: {col}")
|
||||
print(f"\n数据预览:")
|
||||
pd.set_option('display.max_rows', 25)
|
||||
pd.set_option('display.max_columns', 20)
|
||||
pd.set_option('display.width', 200)
|
||||
pd.set_option('display.max_colwidth', 30)
|
||||
pd.set_option("display.max_rows", 25)
|
||||
pd.set_option("display.max_columns", 20)
|
||||
pd.set_option("display.width", 200)
|
||||
pd.set_option("display.max_colwidth", 30)
|
||||
print(df)
|
||||
pd.reset_option('display.max_rows')
|
||||
pd.reset_option('display.max_columns')
|
||||
pd.reset_option('display.width')
|
||||
pd.reset_option('display.max_colwidth')
|
||||
pd.reset_option("display.max_rows")
|
||||
pd.reset_option("display.max_columns")
|
||||
pd.reset_option("display.width")
|
||||
pd.reset_option("display.max_colwidth")
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
元素定位辅助工具 - 用于快速验证定位是否有效
|
||||
"""
|
||||
|
||||
from playwright.sync_api import Page, Frame, Locator
|
||||
|
||||
|
||||
@@ -38,7 +39,9 @@ def debug_locator(frame: Frame, locator: Locator, timeout: int = 5000):
|
||||
try:
|
||||
if element.is_visible(timeout=1000):
|
||||
text = element.inner_text(timeout=1000)
|
||||
print(f" 元素{i + 1}文本: {text[:100] if len(text) > 100 else text}")
|
||||
print(
|
||||
f" 元素{i + 1}文本: {text[:100] if len(text) > 100 else text}"
|
||||
)
|
||||
else:
|
||||
print(f" 元素{i + 1}: 存在但不可见")
|
||||
except:
|
||||
@@ -53,7 +56,9 @@ def debug_locator(frame: Frame, locator: Locator, timeout: int = 5000):
|
||||
return False
|
||||
|
||||
|
||||
def try_multiple_locators(frame: Frame, selectors: list[str], timeout: int = 5000) -> Locator:
|
||||
def try_multiple_locators(
|
||||
frame: Frame, selectors: list[str], timeout: int = 5000
|
||||
) -> Locator:
|
||||
"""
|
||||
尝试多个选择器,返回第一个有效的定位器
|
||||
|
||||
@@ -73,7 +78,9 @@ def try_multiple_locators(frame: Frame, selectors: list[str], timeout: int = 500
|
||||
try:
|
||||
locator = frame.locator(selector)
|
||||
count = locator.count()
|
||||
visible_count = sum(1 for j in range(count) if locator.nth(j).is_visible(timeout=1000))
|
||||
visible_count = sum(
|
||||
1 for j in range(count) if locator.nth(j).is_visible(timeout=1000)
|
||||
)
|
||||
|
||||
print(f" 找到 {count} 个元素,其中 {visible_count} 个可见")
|
||||
|
||||
@@ -101,7 +108,7 @@ def interactive_locate(frame: Frame):
|
||||
selector = input("\n>>> ")
|
||||
selector = selector.strip()
|
||||
|
||||
if selector.lower() in ('q', 'quit'):
|
||||
if selector.lower() in ("q", "quit"):
|
||||
break
|
||||
|
||||
if not selector:
|
||||
@@ -128,7 +135,9 @@ if __name__ == "__main__":
|
||||
page = context.new_page()
|
||||
|
||||
# 登录
|
||||
page.goto("https://68.11.34.30:8082/yonbip/resources/uap/rbac/login/main/index.html")
|
||||
page.goto(
|
||||
"https://68.11.34.30:8082/yonbip/resources/uap/rbac/login/main/index.html"
|
||||
)
|
||||
main_frame = page.locator("#forwardFrame").content_frame
|
||||
main_frame.get_by_role("textbox", name="用户名").fill("BLDpengqiangqiang")
|
||||
main_frame.get_by_role("textbox", name="密码").fill("Cqbld123456.")
|
||||
@@ -158,10 +167,10 @@ if __name__ == "__main__":
|
||||
|
||||
# 询问是否继续
|
||||
choice = input("\n是否继续下一轮调试?(y/n/q): ").strip().lower()
|
||||
if choice in ('n', 'q', 'quit'):
|
||||
if choice in ("n", "q", "quit"):
|
||||
print("退出程序")
|
||||
break
|
||||
elif choice in ('y', ''): # 默认继续
|
||||
elif choice in ("y", ""): # 默认继续
|
||||
continue
|
||||
else:
|
||||
print("未知选项,退出程序")
|
||||
|
||||
Reference in New Issue
Block a user