chore: move scripts to tools directory

Move analyze_excel.py, excel_to_markdown.py, and locator_helper.py
into the tools/ subdirectory to improve project organization.
This commit is contained in:
Misaka_Company
2026-01-23 15:12:33 +08:00
parent bbf84b7f8c
commit 60b6fecbb3
4 changed files with 0 additions and 23 deletions

63
tools/analyze_excel.py Normal file
View File

@@ -0,0 +1,63 @@
"""
分析 Excel 文件的数据结构
"""
import pandas as pd
import openpyxl
import sys
# 设置输出编码
if sys.platform == 'win32':
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# 读取 Excel 文件
file_path = "data/导出文件.xlsx"
print("=" * 80)
print("1. 读取所有工作表名称")
print("=" * 80)
wb = openpyxl.load_workbook(file_path)
sheet_names = wb.sheetnames
print(f"工作表数量: {len(sheet_names)}")
print(f"工作表名称: {sheet_names}")
print("\n" + "=" * 80)
print("2. 逐行读取原始数据(使用 openpyxl")
print("=" * 80)
for sheet_name in sheet_names:
print(f"\n--- 工作表: {sheet_name} ---")
ws = wb[sheet_name]
for idx, row in enumerate(ws.iter_rows(values_only=True), 1):
# 检查是否为空行
is_empty = all(cell is None or str(cell).strip() == "" for cell in row)
if is_empty:
print(f"{idx}: [空行]")
else:
# 过滤掉 None 和空字符串,只显示有效数据
valid_data = [str(cell) if cell is not None else "" for cell in row]
print(f"{idx}: {valid_data}")
print("\n" + "=" * 80)
print("3. 使用 pandas 读取数据(观察列结构)")
print("=" * 80)
for sheet_name in sheet_names:
print(f"\n--- 工作表: {sheet_name} ---")
df = pd.read_excel(file_path, sheet_name=sheet_name)
print(f"DataFrame 形状: {df.shape}")
print(f"\n列名:")
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)
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')

350
tools/excel_to_markdown.py Normal file
View File

@@ -0,0 +1,350 @@
"""
Excel to Markdown Converter
将Excel文件转换为Markdown格式的表格包含行号和列号英文字母
"""
import pandas as pd
import argparse
import sys
from pathlib import Path
def number_to_excel_col(n):
"""将数字转换为Excel列号A, B, ..., Z, AA, AB, ..."""
result = ""
while n > 0:
n -= 1
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):
"""
将Excel文件转换为Markdown表格
Args:
input_file: 输入的Excel文件路径
output_file: 输出的Markdown文件路径可选默认为同名.md文件
sheet_name: 工作表名称或索引,或列表(默认为第一个工作表)
include_row_numbers: 是否包含行号
include_col_numbers: 是否包含列号
"""
input_path = Path(input_file)
if not input_path.exists():
print(f"错误: 文件 '{input_file}' 不存在")
return False
# 设置默认输出文件名
if output_file is None:
output_file = input_path.with_suffix('.md')
else:
output_file = Path(output_file)
try:
# 读取Excel文件
print(f"正在读取文件: {input_file}")
# 获取所有工作表名称(用于索引转换)
xls = pd.ExcelFile(input_file)
all_sheet_names = xls.sheet_names
# 将索引转换为实际的工作表名称
def resolve_sheet_name(name):
if isinstance(name, int):
if 0 <= name < len(all_sheet_names):
return all_sheet_names[name]
else:
raise ValueError(f"工作表索引 {name} 超出范围")
return name
# 处理多个工作表的情况
if isinstance(sheet_name, (list, tuple)):
# 转换索引为名称
resolved_names = [resolve_sheet_name(s) for s in sheet_name]
dfs = pd.read_excel(input_file, sheet_name=resolved_names, header=None)
# 如果只有一个工作表转换为单个DataFrame
if len(resolved_names) == 1:
dfs = {resolved_names[0]: dfs}
sheet_name = resolved_names
else:
resolved_name = resolve_sheet_name(sheet_name)
df = pd.read_excel(input_file, sheet_name=resolved_name, header=None)
dfs = {resolved_name: df}
sheet_name = resolved_name
# 生成所有工作表的Markdown内容
all_sheets_content = {}
for sheet_key, df in dfs.items():
# 转换数据为字符串处理NaN值
df = df.fillna('')
df = df.astype(str)
# 生成Markdown表格
markdown_lines = []
# 添加工作表标题
sheet_title = f"工作表: {sheet_key}"
markdown_lines.append(f"## {sheet_title}\n")
# 添加列号行
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]) + ' |')
# 添加数据行
for idx, row in df.iterrows():
row_data = []
if include_row_numbers:
row_data.append(str(idx + 1))
row_data.extend(row)
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)}")
all_sheets_content[sheet_key] = '\n'.join(markdown_lines)
# 写入文件
if len(dfs) == 1:
# 单个工作表:直接写入
sheet_key = list(dfs.keys())[0]
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')
print(f"✓ 转换成功!")
print(f" 输入文件: {input_file}")
print(f" 输出文件: {output_file}")
print(f" 工作表: {sheet_key}")
print(f" 行数: {len(dfs[sheet_key])}, 列数: {len(dfs[sheet_key].columns)}")
else:
return all_sheets_content, dfs, input_path, input_file
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):
"""
转换多个工作表
Args:
input_file: 输入的Excel文件路径
output_file: 输出的Markdown文件路径
sheet_names: 工作表名称或索引列表
include_row_numbers: 是否包含行号
include_col_numbers: 是否包含列号
merge_to_one_file: 是否合并到一个文件True或分别输出False
"""
input_path = Path(input_file)
if not input_path.exists():
print(f"错误: 文件 '{input_file}' 不存在")
return False
# 设置默认输出文件名
if output_file is None:
output_file = input_path.with_suffix('.md')
else:
output_file = Path(output_file)
try:
# 读取Excel文件
print(f"正在读取文件: {input_file}")
# 获取所有工作表名称
xls = pd.ExcelFile(input_file)
all_sheet_names = xls.sheet_names
if sheet_names is None or len(sheet_names) == 0:
# 读取所有工作表
sheet_names = all_sheet_names
print(f"找到 {len(sheet_names)} 个工作表")
else:
# 将索引转换为实际的工作表名称
resolved_names = []
for name in sheet_names:
if isinstance(name, int):
# 索引转换为名称
if 0 <= name < len(all_sheet_names):
resolved_names.append(all_sheet_names[name])
else:
print(f"警告: 工作表索引 {name} 超出范围,已跳过")
else:
# 直接使用名称
resolved_names.append(name)
sheet_names = resolved_names
# 使用实际的工作表名称读取数据
dfs = pd.read_excel(input_file, sheet_name=sheet_names, header=None)
# 确保返回的是字典格式
if not isinstance(dfs, dict):
dfs = {sheet_names[0]: dfs}
# 生成所有工作表的Markdown内容
all_sheets_content = {}
for sheet_key, df in dfs.items():
# 转换数据为字符串处理NaN值
df = df.fillna('')
df = df.astype(str)
# 生成Markdown表格
markdown_lines = []
# 添加工作表标题
sheet_title = f"工作表: {sheet_key}"
markdown_lines.append(f"## {sheet_title}\n")
# 添加列号行
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]) + ' |')
# 添加数据行
for idx, row in df.iterrows():
row_data = []
if include_row_numbers:
row_data.append(str(idx + 1))
row_data.extend(row)
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)}")
all_sheets_content[sheet_key] = '\n'.join(markdown_lines)
# 写入文件
if merge_to_one_file:
# 合并到一个文件
output_content = f"# {input_path.stem}\n\n"
output_content += f"从 `{input_file}` 转换,共 {len(dfs)} 个工作表\n\n"
output_content += "---\n\n"
for sheet_key in sheet_names:
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')
print(f"✓ 转换成功!")
print(f" 输入文件: {input_file}")
print(f" 输出文件: {output_file}")
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)}")
else:
# 分别输出到多个文件
output_stem = output_file.stem
output_suffix = output_file.suffix
output_dir = output_file.parent
for sheet_key in sheet_names:
if sheet_key not in all_sheets_content:
continue
# 为每个工作表创建单独的文件
sheet_filename = f"{output_stem}_{sheet_key}{output_suffix}"
sheet_output_file = output_dir / sheet_filename
output_content = f"# {input_path.stem} - {sheet_key}\n\n"
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')
print(f"✓ 转换成功!")
print(f" 输入文件: {input_file}")
print(f" 输出目录: {output_dir}")
print(f" 工作表数量: {len(dfs)}")
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)}列)")
return True
except Exception as e:
print(f"错误: {str(e)}")
import traceback
traceback.print_exc()
return False
# ============================================================
# 配置区域 - 直接修改下面的参数
# ============================================================
# 输入文件路径(必填)
INPUT_FILE = r"D:\python\playwrite\data\YTHN-100.A0.532-BOM-1.2版.xlsx"
# 输出文件路径(可选,默认为输入文件同名.md文件
OUTPUT_FILE = None # 或指定 r"D:\path\to\output.md"
# 工作表名称或索引可选默认为0即第一个工作表
# 可以是单个值: 0 或 "Sheet1"
# 可以是列表: [0, 1, 2] 或 ["Sheet1", "Sheet2", "Sheet3"]
# 可以是 None 表示读取所有工作表
SHEET_NAMES = [0, 1,2,3] # 指定多个工作表索引
# 是否包含行号可选默认为True
INCLUDE_ROW_NUMBERS = True
# 是否包含列号可选默认为True
INCLUDE_COL_NUMBERS = True
# 如果指定了多个工作表是否合并到一个文件可选默认为True
# True: 所有工作表合并到一个文件
# False: 每个工作表生成单独的文件(文件名格式: 原文件名_工作表名.md
MULTI_SHEETS_TO_ONE_FILE = True
# ============================================================
def main():
"""主函数 - 使用上方配置区域定义的参数"""
# 判断是否为多个工作表
if isinstance(SHEET_NAMES, list) and len(SHEET_NAMES) > 1:
# 多个工作表
convert_multiple_sheets(
input_file=INPUT_FILE,
output_file=OUTPUT_FILE,
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
)
else:
# 单个工作表
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
)
if __name__ == '__main__':
main()

171
tools/locator_helper.py Normal file
View File

@@ -0,0 +1,171 @@
"""
元素定位辅助工具 - 用于快速验证定位是否有效
"""
from playwright.sync_api import Page, Frame, Locator
def debug_locator(frame: Frame, locator: Locator, timeout: int = 5000):
"""
调试定位器 - 检查定位是否有效并显示详细信息
参数:
frame: 页面或iframe对象
locator: 定位器对象
timeout: 超时时间(毫秒)
返回:
bool: 定位是否成功
"""
print(f"\n验证定位: {locator}")
print("=" * 50)
try:
# 检查数量
count = locator.count()
print(f"元素数量: {count}")
if count == 0:
print("✗ 未找到任何匹配元素")
return False
# 检查第一个元素是否可见
first_visible = locator.first.is_visible(timeout=timeout)
print(f"第一个元素可见: {first_visible}")
# 获取文本内容
for i in range(min(3, count)): # 最多显示3个
element = locator.nth(i)
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}")
else:
print(f" 元素{i + 1}: 存在但不可见")
except:
print(f" 元素{i + 1}: 无法获取信息")
print("=" * 50)
return True
except Exception as e:
print(f"✗ 验证失败: {e}")
print("=" * 50)
return False
def try_multiple_locators(frame: Frame, selectors: list[str], timeout: int = 5000) -> Locator:
"""
尝试多个选择器,返回第一个有效的定位器
参数:
frame: 页面或iframe对象
selectors: 选择器列表
timeout: 超时时间(毫秒)
返回:
第一个有效的 Locator如果没有则返回 None
"""
print(f"\n尝试 {len(selectors)} 个定位方式...")
print("-" * 50)
for i, selector in enumerate(selectors):
print(f"[{i + 1}] 尝试: {selector}")
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))
print(f" 找到 {count} 个元素,其中 {visible_count} 个可见")
if visible_count > 0:
print(f" ✓ 使用此定位器")
print("-" * 50)
return locator
except Exception as e:
print(f" ✗ 失败: {e}")
print("-" * 50)
return None
def interactive_locate(frame: Frame):
"""
交互式定位调试 - 进入交互模式测试定位表达式
"""
print("\n进入交互式定位调试模式")
print("输入定位表达式,输入 'q''quit' 退出")
print("-" * 50)
while True:
try:
selector = input("\n>>> ")
selector = selector.strip()
if selector.lower() in ('q', 'quit'):
break
if not selector:
continue
debug_locator(frame, frame.locator(selector))
except KeyboardInterrupt:
break
except Exception as e:
print(f"错误: {e}")
print("退出调试模式")
# 示例使用代码(单独运行此文件时的演示)
if __name__ == "__main__":
from playwright.sync_api import sync_playwright
import re
with sync_playwright() as pw:
browser = pw.chromium.launch(headless=False)
context = browser.new_context(ignore_https_errors=True)
page = context.new_page()
# 登录
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.")
main_frame.get_by_role("button", name="登录").click()
confirm_btn = main_frame.get_by_role("button", name="确定")
if confirm_btn.count() > 0:
confirm_btn.click()
# 循环切换 Inspector 和交互式调试
print("\n" + "=" * 60)
print("元素定位调试工具")
print("=" * 60)
print("循环模式:")
print(" 1. Inspector 窗口 - 使用浏览器录制/选择元素")
print(" 2. 交互式调试 - 验证定位表达式")
print(" 输入 'q''quit' 退出程序\n")
while True:
# 1. 打开 Inspector 窗口
print("\n【打开 Inspector 窗口】")
print("请在 Inspector 中获取元素定位器,然后关闭 Inspector 继续...")
page.pause()
# 2. 进入交互式定位调试
print("\n【进入交互式定位调试】")
interactive_locate(main_frame)
# 询问是否继续
choice = input("\n是否继续下一轮调试?(y/n/q): ").strip().lower()
if choice in ('n', 'q', 'quit'):
print("退出程序")
break
elif choice in ('y', ''): # 默认继续
continue
else:
print("未知选项,退出程序")
break
context.close()
browser.close()