python 编辑Excel 原来这么方便

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['colum1'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['英文'].astype(str) + '";'
        df['colum2'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['简体中文'].astype(str) + '";'
        df['colum3'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['繁体中文'].astype(str) + '";'
        df['colum4'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['越南(vi)'].astype(str) + '";'
        df['colum5'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['法语(fr)'].astype(str) + '";'
        df['colum6'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['韩语(ko)'].astype(str) + '";'
        df['colum7'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['德语(de)'].astype(str) + '";'
        df['colum8'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['印度尼西亚(in)'].astype(str) + '";'
        df['colum9'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['西班牙语(es)'].astype(str) + '";'
        df['colum10'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['日语(ja)'].astype(str) + '";'
        df['colum11'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['泰语(th)'].astype(str) + '";'
        df['colum12'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['意大利语(it)'].astype(str) + '";'
        df['colum13'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['土耳其语(tr)'].astype(str) + '";'
        df['colum14'] = '"' + df['翻译索引'].astype(str) + '" = "' + df['葡萄牙语(pt)'].astype(str) + '";'
            
        # 保存修改后的 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 = "input0519.xlsx"  # 替换为你的输入 Excel 文件路径
output_excel = "output0519.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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容