Files
hr-position-system/scripts/convert_to_table.py
DonaldFang 方士碩 a6af297623 backup: 完成 HR_position_ 表格前綴重命名與欄位對照表整理
變更內容:
- 所有資料表加上 HR_position_ 前綴
- 整理完整欄位顯示名稱與 ID 對照表
- 模組化 JS 檔案 (admin.js, ai.js, csv.js 等)
- 專案結構優化 (docs/, scripts/, tests/ 等)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-09 12:05:20 +08:00

94 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 讀取原始文件
with open('excel.md', 'r', encoding='utf-8') as f:
lines = f.readlines()
# 跳過表頭,處理數據
data_lines = lines[1:] # 跳過第一行表頭
result = []
current_values = {
'事業體': '',
'處級單位': '',
'部級單位': '',
}
for line in data_lines:
line = line.rstrip('\n\r')
if not line.strip():
continue
# 分割 Tab
parts = line.split('\t')
# 確保至少有4個元素
while len(parts) < 4:
parts.append('')
# 處理每一列
# 第1列事業體
if parts[0].strip():
current_values['事業體'] = parts[0].strip()
# 第2列處別處級單位
if parts[1].strip():
current_values['處級單位'] = parts[1].strip()
# 第3列單位名稱可能是處級、部級或其他
if parts[2].strip():
unit_name = parts[2].strip()
# 判斷單位級別
if unit_name.endswith(''):
# 如果是處級單位,更新處級單位,清空部級單位
current_values['處級單位'] = unit_name
current_values['部級單位'] = ''
elif unit_name.endswith(''):
# 如果是部級單位,更新部級單位
current_values['部級單位'] = unit_name
elif '' in unit_name:
# 如果包含"部",視為部級單位
current_values['部級單位'] = unit_name
elif '' in unit_name:
# 如果包含"處",視為處級單位
current_values['處級單位'] = unit_name
current_values['部級單位'] = ''
# 第4列崗位名稱
position_name = parts[3].strip() if len(parts) > 3 else ''
# 如果沒有崗位名稱,嘗試從其他列找(可能崗位名稱在其他位置)
if not position_name:
# 從後往前找第一個非空值作為崗位名稱
for i in range(len(parts) - 1, -1, -1):
if parts[i].strip() and i != 0 and i != 1 and i != 2:
position_name = parts[i].strip()
break
# 只有當有崗位名稱時才加入結果
if position_name:
result.append([
current_values['事業體'],
current_values['處級單位'],
current_values['部級單位'],
position_name
])
# 生成 Markdown 表格
output = []
output.append('| 事業體 | 處級單位 | 部級單位 | 崗位名稱 |')
output.append('|--------|----------|----------|----------|')
for row in result:
# 轉義管道符號
row_escaped = [cell.replace('|', '\\|') if cell else '' for cell in row]
output.append(f"| {row_escaped[0]} | {row_escaped[1]} | {row_escaped[2]} | {row_escaped[3]} |")
# 寫入新文件
with open('excel_table.md', 'w', encoding='utf-8') as f:
f.write('\n'.join(output))
print(f"轉換完成!共生成 {len(result)} 行數據。")
print("輸出文件excel_table.md")