excel-mapping-replace
Installation
Review the source and dependencies with a prompt. A package is available to download.
Install with an AI assistant
Copy the prompt so your assistant can review the pinned revision and dependencies first.
View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "excel-mapping-replace" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-8560055ea4f2d1ea2221525aa735fa0794af2f0272c5ef765e511a200afc958f. Verify the upstream source and version against https://skillhub.cn/skills/user_8f803df1/excel-mapping-replace before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.
Source and setup
skillhub.cn
Setup guidance pending
See the original source for supported tools, dependencies, and permissions.
Installation test pending
Batch-replace values in an Excel column by a mapping table: the user supplies the mapping (via conversation, pasted list, or mapping file), and the skill automatically matches and replaces corresponding values in the target column; values not present in the mapping are left unchanged. Trigger phrases include “mapping replace”, “batch replace”, “by lookup table”, “convert to code”, and “corresponding replace”.
Introduction source: third_party · user_8f803df1 · 1.0.0 Reference
SkillPi interpretation
- Tasks
- Translation pending; see source documentation.
- Limits and differences
- Translation pending; see source documentation.
- Testing
- Not tested
Browse files
Frontmatter
name: excel-mapping-replace
description: |
Batch replace column values by mapping table. User provides mapping relationships (spoken in conversation / pasted list / mapping file), and the skill auto-matches and replaces corresponding values in the target column. Values not in the mapping table are preserved as-is.
按映射表批量替换某列的值。用户提供映射关系(对话直说/粘贴列表/映射文件),技能自动匹配并替换目标列中对应的值。映射表中不存在的值保留原样。
Trigger keywords: "mapping replace" "batch replace" "by lookup table" "convert codes" "corresponding replace"
触发词包括"映射替换""批量替换""按对照表""换成代码""对应替换"。This skill follows [[excel-safe-workflow]] four-step method. Mapping matching uses pandas, value replacement uses openpyxl (small files) or XML (large files). 本技能遵循 [[excel-safe-workflow]] 四步法。映射匹配用 pandas,值替换用 openpyxl(小文件)或 XML(大文件)。
Excel Mapping Replace / Excel 映射替换
功能
给一张映射表,把目标列中匹配的值全部替换。
映射表: 目标列替换前 → 替换后:
中国 → CN 中国 → CN
日本 → JP 中国 → CN
美国 → US 日本 → JP
德国 → DE 中国 → CN
... ...
映射表中不存在的值保留原样,不会丢失数据。
第零步:需求解析
| 要素 | 用户说 | 默认值 |
|---|---|---|
| 目标列 | "公开国别""状态列" | 必须明确 |
| 映射关系 | "中国→CN,日本→JP" / 粘贴列表 / 映射文件 | 必须明确 |
| 映射来源 | 对话口述 / 粘贴文本 / xlsx文件 | 对话口述 |
映射关系格式
# 对话直说(几个映射)
"中国换成CN,日本换成JP,美国换成US"
# 粘贴列表(几十个映射)
中国 → CN
日本 → JP
美国 → US
...
# 映射文件(几百个映射)
"用 国家代码表.xlsx 的 A列→B列 做映射"
第一步:勘察
import pandas as pd
FILE = '目标文件.xlsx'
TARGET_COL = '列名'
df = pd.read_excel(FILE)
print(f'总行数: {len(df)}')
vc = df[TARGET_COL].value_counts()
print(f'唯一值: {len(vc)}')
for k, v in vc.head(20).items():
print(f' {k}: {v}')
第二步:规划
- 确认目标列和映射表
- 统计有多少行会受影响(映射表 ∩ 列中的值)
- 列出映射表中不存在的值(不会被改动)
- 确认无误后执行
第三步:执行
⚠️ 禁止在 sharedStrings 层做全局替换。必须走 sheet 层 + 列号限定,只改目标列的 cell。
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
import os, shutil, re, time
FILE = '目标文件.xlsx'
TARGET_COL = '列名'
MAPPING = {'旧值1': '新值1', '旧值2': '新值2', ...}
# ====== 3.1 勘察 ======
df = pd.read_excel(FILE)
col_idx = list(df.columns).index(TARGET_COL) + 1 # 列号(1-based)
col_letter = get_column_letter(col_idx)
# 统计影响
affected = {k: v for k, v in df[TARGET_COL].value_counts().items() if k in MAPPING}
unmatched = {k: v for k, v in df[TARGET_COL].value_counts().items() if k not in MAPPING}
print(f'目标列: {TARGET_COL} ({col_letter}), 将替换:')
for k, v in affected.items():
print(f' {k} → {MAPPING[k]}: {v} 行')
if unmatched:
print(f'\n不在映射表中(保留原值):')
for k, v in unmatched.items():
print(f' {k}: {v} 行')
# ====== 3.2 执行 ======
USE_XML = os.path.getsize(FILE) > 10 * 1024 * 1024 # >10MB
if USE_XML:
# ====== XML 方案:sheet 层 + 列号限定 + inline 写入 ======
print('\n替换中(XML sheet 层方案)...')
import zipfile
from lxml import etree
t0 = time.time()
TMP = FILE.replace('.xlsx', '_mp_tmp')
if os.path.exists(TMP): shutil.rmtree(TMP)
os.makedirs(TMP)
with zipfile.ZipFile(FILE, 'r') as z:
z.extractall(TMP)
S_NS = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
parser = etree.XMLParser(remove_blank_text=False, huge_tree=True)
ns = {'s': S_NS}
# 读 sharedStrings 建立 si→text 映射(只读,用于解析 t="s" 的 cell)
ss_path = os.path.join(TMP, 'xl', 'sharedStrings.xml')
si_lookup = {}
if os.path.exists(ss_path):
ss_tree = etree.parse(ss_path, parser)
for idx, si_elem in enumerate(ss_tree.findall('.//s:si', ns)):
t_elem = si_elem.find('s:t', ns)
si_lookup[idx] = t_elem.text if t_elem is not None else ''
# 处理 sheet XML — 只在目标列上改值
ws_dir = os.path.join(TMP, 'xl', 'worksheets')
replaced = 0
for sf in sorted(os.listdir(ws_dir)):
if not sf.endswith('.xml'): continue
sp = os.path.join(ws_dir, sf)
tree = etree.parse(sp, parser)
root = tree.getroot()
for row_elem in root.findall('.//s:row', ns):
if row_elem.get('r') == '1': continue # 跳过表头
for cell in row_elem.findall('s:c', ns):
# 限定列号
if not cell.get('r', '').startswith(col_letter):
continue
# 获取当前文本值
cell_type = cell.get('t', '')
val = None
if cell_type == 's':
v_elem = cell.find('s:v', ns)
if v_elem is not None and v_elem.text:
val = si_lookup.get(int(v_elem.text), '')
else:
is_elem = cell.find('s:is', ns)
if is_elem is not None:
t_elem = is_elem.find('s:t', ns)
val = t_elem.text if t_elem is not None else ''
if val is None or val not in MAPPING:
continue
# 改为 inline 字符串(不创建新的 sharedString 引用)
new_val = MAPPING[val]
cell.set('t', 'inlineStr')
for child in list(cell):
tag = child.tag.split('}')[-1]
if tag in ('v', 'f', 'is'): cell.remove(child)
is_new = etree.SubElement(cell, '{'+S_NS+'}is')
t_new = etree.SubElement(is_new, '{'+S_NS+'}t')
t_new.text = new_val
replaced += 1
sheet_xml = etree.tostring(root, xml_declaration=True, encoding='UTF-8', standalone=True)
with open(sp, 'wb') as f: f.write(sheet_xml)
print(f' 替换 {replaced} 个单元格')
# 打包
with zipfile.ZipFile(FILE, 'w', zipfile.ZIP_DEFLATED) as zout:
for dirpath, _, filenames in os.walk(TMP):
for fn in filenames:
full = os.path.join(dirpath, fn)
zout.write(full, os.path.relpath(full, TMP).replace('\\\\', '/'))
shutil.rmtree(TMP)
print(f' 耗时: {time.time()-t0:.0f}s')
else:
# ====== openpyxl 方案(小文件,简单可靠)======
print('\n替换中(openpyxl 方案)...')
# 备份
bak = FILE.replace('.xlsx', '_backup.xlsx')
if not os.path.exists(bak):
shutil.copy2(FILE, bak)
t0 = time.time()
wb = load_workbook(FILE)
ws = wb.active
replaced = 0
for row in range(2, ws.max_row + 1):
cell = ws.cell(row=row, column=col_idx)
if cell.value in MAPPING:
cell.value = MAPPING[cell.value]
replaced += 1
if row % 50000 == 0:
print(f' 进度: {row}/{ws.max_row}')
wb.save(FILE)
wb.close()
print(f' 替换: {replaced} 个, 耗时: {time.time()-t0:.1f}s')
第四步:验证
df2 = pd.read_excel(FILE)
print(f'\n替换后 [{TARGET_COL}] 分布:')
for k, v in df2[TARGET_COL].value_counts().items():
marker = ' ← 新' if k in MAPPING.values() else ''
print(f' {k}: {v}{marker}')
# 确认未映射值没被修改
for old_val in unmatched:
still_there = (df2[TARGET_COL] == old_val).sum()
if still_there != unmatched[old_val]:
print(f' ❌ {old_val}: 预期{unmatched[old_val]}行, 实际{still_there}行')
从映射文件读取
# 从另一个 xlsx/csv 读取映射表
map_df = pd.read_excel('映射文件.xlsx')
MAPPING = dict(zip(map_df.iloc[:, 0], map_df.iloc[:, 1]))
# 或从 csv
# map_df = pd.read_csv('映射文件.csv')
# MAPPING = dict(zip(map_df['中文'], map_df['代码']))
注意事项
- 映射表不匹配的值不动:只替换映射表中存在的值,其余原样保留
- 精确匹配:不是包含匹配。
中国只匹配中国,不匹配中国北京 - ⚠️ XML 方案只在目标列上改值:通过列号限定
cell.get('r').startswith(col_letter),不会误伤其他列。禁止在 sharedStrings 层做全局替换 - 改值后写 inline string:替换后的值写为
<is><t>内联字符串,不产生新的 sharedString 引用 - 操作前必备份:遵循 [[excel-safe-workflow]] 第零步——操作前自动备份(时间戳命名),成功后保留最新3份,失误后立即删除损坏文件并从备份恢复
- 文件被占用:如果目标文件正在 Excel 中打开,会保存失败。提示用户关闭后重试
- 大小写敏感:
China≠china,如需不敏感需预处理
Current listing version
1.0.0
Safety and assessment details
Quality assessment
Out of 100 · Assessment report from SkillPi LAB.
Highlights
- 解决 Excel 数据处理中的高频刚需场景(如国家名称转代码、状态标准化)
- 映射表不匹配的值保留原样,避免数据丢失,实用性强
Highlights
- 触发词覆盖合理,中英文关键词完整("映射替换"、"batch replace"、"按对照表"等)
- 边界清晰,能有效区分同类数据处理工具
Areas to improve
-
description 内容重复 Reported severity: Minor
顶层 frontmatter 和 skill_body 内部均包含完整 description,内容一致但存在冗余
Recommendation: 建议保留一处 description,避免重复维护
Highlights
- 文档结构完整,遵循四步法流程(需求解析→勘察→规划→执行→验证)
- 提供端到端代码示例和多种映射输入方式说明
- 注意事项清晰,涵盖安全操作要点
Highlights
- 工具来源明确(pandas、openpyxl、lxml),参数使用正确
- 覆盖主要异常场景(文件备份、大小判断、列号限定、sharedStrings 安全处理)
- XML 方案避免全局替换风险,确保只修改目标列
The arithmetic mean of the first four original scores, rounded to a whole number.
Static text checks
100 / 100 · skillpi-20261123-no-r3-v1
No current rules matched the text.
This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.
Permissions and checks before use
Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.
-
Save or modify files
Confirm the output directory and whether existing files will be replaced. Back up important files and prefer a new output directory.
Mentioned in main document · lines 182, 217 -
Delete files
Confirm the absolute path and scope and keep a recoverable backup. Do not run example commands against important directories.
Mentioned in main document · lines 120, 192
Domains or addresses referenced in the document
These addresses may be documentation links or examples, not an approved network allowlist. Verify actual destinations and outgoing data before use.
- schemas.openxmlformats.org
Security rating and access scope
safe · Static evidence review; not a runtime safety guarantee.
Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.
User discussion
No verified reviews yet.