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['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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,828评论 6 526
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,669评论 3 412
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,467评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,238评论 1 306
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,039评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,561评论 1 319
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,658评论 3 433
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,806评论 0 285
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,316评论 1 329
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,293评论 3 353
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,443评论 1 364
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,998评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,698评论 3 342
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,097评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,333评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,012评论 3 385
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,434评论 2 370

推荐阅读更多精彩内容