1、MAC 安装 python
brew install python
python3 --version
python3 -m pip --version
2、安装 python 所需库
// pip install pandas openpyxl
pip3 install pandas openpyxl
3、编写脚本(需求合并AB列到C列)
列名是第一行的名字 而不是Excel自带的ABC
import pandas as pd
def merge_excel_columns(input_file, output_file):
"""
将 Excel 文件中 A 列和 B 列的内容合并到 C 列。
Args:
input_file (str): 输入 Excel 文件路径。
output_file (str): 输出 Excel 文件路径。
"""
try:
# 读取 Excel 文件
df = pd.read_excel(input_file)
print(df.columns)
# 合并 A 列和 B 列,并添加到 C 列
df['1'] = '"' + df['翻译索引'] + '" = "' + df['英文'] + '";'
df['2'] = '"' + df['翻译索引'] + '" = "' + df['简体中文'] + '";'
df['3'] = '"' + df['翻译索引'] + '" = "' + df['繁体中文'] + '";'
df['4'] = '"' + df['翻译索引'] + '" = "' + df['越南(vi)'] + '";'
df['5'] = '"' + df['翻译索引'] + '" = "' + df['法语(fr)'] + '";'
df['6'] = '"' + df['翻译索引'] + '" = "' + df['韩语(ko)'] + '";'
df['7'] = '"' + df['翻译索引'] + '" = "' + df['德语(de)'] + '";'
df['8'] = '"' + df['翻译索引'] + '" = "' + df['印度尼西亚(in)'] + '";'
df['9'] = '"' + df['翻译索引'] + '" = "' + df['西班牙语(es)'] + '";'
df['10'] = '"' + df['翻译索引'] + '" = "' + df['日语(ja)'] + '";'
df['11'] = '"' + df['翻译索引'] + '" = "' + df['泰语(th)'] + '";'
df['12'] = '"' + df['翻译索引'] + '" = "' + df['意大利语(it)'] + '";'
df['13'] = '"' + df['翻译索引'] + '" = "' + df['土耳其语(tr)'] + '";'
df['14'] = '"' + df['翻译索引'] + '" = "' + df['葡萄牙语(pt)'] + '";'
# 保存修改后的 Excel 文件
df.to_excel(output_file, index=False)
print(f"成功将 A 列和 B 列合并到 C 列,并保存到 {output_file}")
except FileNotFoundError:
print(f"错误:找不到文件 {input_file}")
except Exception as e:
print(f"发生错误:{e}")
# 使用示例
input_excel = "无标题电子表格.xlsx" # 替换为你的输入 Excel 文件路径
output_excel = "output.xlsx" # 替换为你的输出 Excel 文件路径
merge_excel_columns(input_excel, output_excel)
4、执行脚本
1741930579593.jpg
python3 merge_excel.py
二次执行报找不到库:
//创建虚拟环境:
//激活虚拟环境:
//在虚拟环境中安装 pandas 和 openpyxl:
python3 -m venv myenv
source myenv/bin/activate
python3 -m pip install pandas openpyxl
// 正这就正常了
// 停用虚拟环境:
deactivate
挑选特定的行执行,(需求:组件工程用到的国际化 摘出去)
import pandas as pd
def merge_excel_columns_specified_rows(input_file, output_file, specified_indices):
"""
将 Excel 文件中指定的 '翻译索引' 行的内容合并到新列。
Args:
input_file (str): 输入 Excel 文件路径。
output_file (str): 输出 Excel 文件路径。
specified_indices (list): 需要处理的 '翻译索引' 行名列表。
"""
try:
# 读取 Excel 文件
df = pd.read_excel(input_file, header=1)
print(df.columns)
print(df.head())
# 筛选指定的行
df_filtered = df[df['翻译索引'].isin(specified_indices)].copy() # 添加 .copy() 以避免 SettingWithCopyWarning
# 合并指定行的列,并添加到新的列
df_filtered['1'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['英文'] + '";'
df_filtered['2'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['简体中文'] + '";'
df_filtered['3'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['繁体中文'] + '";'
df_filtered['4'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['越南(vi)'] + '";'
df_filtered['5'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['法语(fr)'] + '";'
df_filtered['6'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['韩语(ko)'] + '";'
df_filtered['7'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['德语(de)'] + '";'
df_filtered['8'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['印度尼西亚(in)'] + '";'
df_filtered['9'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['西班牙语(es)'] + '";'
df_filtered['10'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['日语(ja)'] + '";'
df_filtered['11'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['泰语(th)'] + '";'
df_filtered['12'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['意大利语(it)'] + '";'
df_filtered['13'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['土耳其语(tr)'] + '";'
df_filtered['14'] = '"' + df_filtered['翻译索引'] + '" = "' + df_filtered['葡萄牙语(pt)'] + '";'
# 使用 loc 进行条件更新
for index, row in df_filtered.iterrows():
df.loc[df['翻译索引'] == row['翻译索引'], '1'] = row['1']
df.loc[df['翻译索引'] == row['翻译索引'], '2'] = row['2']
df.loc[df['翻译索引'] == row['翻译索引'], '3'] = row['3']
df.loc[df['翻译索引'] == row['翻译索引'], '4'] = row['4']
df.loc[df['翻译索引'] == row['翻译索引'], '5'] = row['5']
df.loc[df['翻译索引'] == row['翻译索引'], '6'] = row['6']
df.loc[df['翻译索引'] == row['翻译索引'], '7'] = row['7']
df.loc[df['翻译索引'] == row['翻译索引'], '8'] = row['8']
df.loc[df['翻译索引'] == row['翻译索引'], '9'] = row['9']
df.loc[df['翻译索引'] == row['翻译索引'], '10'] = row['10']
df.loc[df['翻译索引'] == row['翻译索引'], '11'] = row['11']
df.loc[df['翻译索引'] == row['翻译索引'], '12'] = row['12']
df.loc[df['翻译索引'] == row['翻译索引'], '13'] = row['13']
df.loc[df['翻译索引'] == row['翻译索引'], '14'] = row['14']
# 保存修改后的 Excel 文件
df.to_excel(output_file, index=False)
print(f"成功将指定的行名的列合并,并保存到 {output_file}")
except FileNotFoundError:
print(f"错误:找不到文件 {input_file}")
except KeyError as e:
print(f"错误:列名错误,请检查Excel文件是否包含指定的列。详细错误信息:{e}")
except Exception as e:
print(f"发生错误:{e}")
# 使用示例
input_excel = "merge_excel_widget.xlsx"
output_excel = "outputWidget8.xlsx"
specified_indices = ["translation_idx_2495", "translation_idx_2474"]
merge_excel_columns_specified_rows(input_excel, output_excel, specified_indices)