You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python+Openpyxl实现Excel单元格字母倒换且无需查看原名称列表

实现方案

核心转换逻辑

不用提前构建替换字典,直接通过ASCII码计算实现字母倒序映射,全程无需读取输出原名称,满足隐私要求:

  • 大写字母A-Z的ASCII值范围为65~90,倒序对应公式:chr(155 - ord(原字母))(A+Z=65+90=155)
  • 小写字母a-z的ASCII值范围为97~122,倒序对应公式:chr(219 - ord(原字母))(a+z=97+122=219)
  • 非字母字符(数字、符号、中文等)直接保留不转换

完整代码

import openpyxl
from openpyxl import load_workbook

print("Type the file name:")
DF = input()
wb = load_workbook(DF + '.xlsx')
print("Sheet Name:")
sht = input()
ws = wb[sht]

NC = str(input("Where is the Name Column?"))
column = ws[NC]

# 批量转换,全程不输出原内容
for cell in column:
    # 跳过空单元格
    if not cell.value:
        continue
    converted_chars = []
    for c in str(cell.value):
        if c.isupper():
            converted_chars.append(chr(155 - ord(c)))
        elif c.islower():
            converted_chars.append(chr(219 - ord(c)))
        else:
            converted_chars.append(c)
    cell.value = ''.join(converted_chars)

wb.save(DF + '.xlsx')
print("转换完成")

功能说明

  • 自动识别有效行范围,跳过空单元格,无需提前配置行数
  • 全程不打印、不缓存原始名称内容,满足隐私保护需求
  • 自动保留原字母大小写,非字母内容原样保留
  • 无需预配置替换规则,转换逻辑自动计算,适配所有字母组合的名称

内容的提问来源于stack exchange,提问作者R4f

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 04:45:03