Mac系统下提取800个Excel文件同单元格值并生成列的高效方法
批量提取Excel文件同一单元格值的快速方案(Mac系统)
方法1:用AppleScript(无需额外装软件)
Mac自带的脚本工具,直接操控Excel,步骤简单:
- 打开「脚本编辑器」(启动台→其他里找)
- 替换脚本里的参数(目标单元格、源文件文件夹、输出文件路径),然后点击运行:
set targetCell to "A1" -- 改成你要提取的单元格,比如"B2" set folderPath to "/Users/你的用户名/Documents/Excel文件" -- 源文件所在文件夹的绝对路径 set outputFile to "/Users/你的用户名/Documents/提取结果.xlsx" -- 结果文件保存路径 tell application "Microsoft Excel" activate set newWorkbook to make new workbook set resultSheet to active sheet of newWorkbook set currentRow to 1 set fileList to list folder folderPath without invisibles repeat with fileName in fileList if fileName ends with ".xlsx" or fileName ends with ".xls" then set fullFilePath to folderPath & "/" & fileName open fullFilePath set cellValue to value of range targetCell of active sheet close active workbook saving no -- 不修改原文件 set value of range ("A" & currentRow) of resultSheet to cellValue set value of range ("B" & currentRow) of resultSheet to fileName -- 可选:保留文件名对应关系 set currentRow to currentRow + 1 end if end repeat save as newWorkbook filename outputFile file format Excel XML file format close newWorkbook saving yes end tell
注意:运行前确保Excel是正常打开状态,脚本会自动处理所有文件,不会改动原文件。
方法2:用Python(效率更高,适合大量文件)
用pandas库后台批量处理,速度比AppleScript快很多,适配800个文件的场景:
- 打开「终端」,先安装依赖:
pip3 install pandas openpyxl - 创建一个
extract_values.py文件,粘贴代码并修改参数:
import os import pandas as pd target_cell = "A1" # 目标单元格,比如"B3" input_folder = "/Users/你的用户名/Documents/Excel文件" # 源文件文件夹路径 output_file = "/Users/你的用户名/Documents/提取结果.xlsx" # 输出文件路径 results = [] for filename in os.listdir(input_folder): if filename.lower().endswith((".xlsx", ".xls")): file_path = os.path.join(input_folder, filename) try: # 解析单元格位置 col = target_cell[0] row = int(target_cell[1:]) - 1 df = pd.read_excel(file_path, usecols=[col], header=None) cell_value = df.iloc[row, 0] results.append({"文件名": filename, "提取值": cell_value}) except Exception as e: print(f"{filename} 处理失败: {str(e)}") results.append({"文件名": filename, "提取值": "读取错误"}) # 保存结果到新Excel pd.DataFrame(results).to_excel(output_file, index=False) print(f"搞定!结果存到 {output_file} 了")
- 终端里运行脚本:
python3 extract_values.py
内容的提问来源于stack exchange,提问作者H Pea
相关产品推荐
相关产品推荐

