Apple Automator 处理 csv 文件并创建新文件

发布于 2024-10-07 04:19:59 字数 631 浏览 2 评论 0原文

是否可以使用 Apple Automator 循环遍历一组选定的文件,处理每个文件,然后将输出保存为新文件?

我有一组 .xls 文件,并且已经使用 Automator

- Ask for Finder Items
- Open Finder Items
- Convert Format of Excel Files #save each .xls file to a .csv

编写了一个 python 脚本,该脚本接受文件名作为参数,对其进行处理,并将其作为 p_filename 保存在脚本运行的目录中。我正在尝试将 Run Shell Script/usr/bin/python shell 和粘贴的 python 脚本一起使用。

不过,有些东西翻译得不太好,特别是因为我不确定它如何处理 python 的 open('filename','w') 命令。它可能没有创建新文件的权限,或者我输入的命令不正确。我的想法是将处理后的文件输出为文本,使用 Automator 捕获它,然后将其保存到新文件中。

为此,我尝试使用新建文本文件,但我无法让它为开始时选择的每个文件创建一个新的文本文件。是否可以循环浏览所有选定的 Finder 项目?

Is it possible to loop through a set of selected files, process each, and save the output as new files using Apple Automator?

I have a collection of .xls files, and I've gotten Automator to

- Ask for Finder Items
- Open Finder Items
- Convert Format of Excel Files #save each .xls file to a .csv

I've written a python script that accepts a filename as an argument, processes it, and saves it as p_filename in the directory the script's being run from. I'm trying to use Run Shell Script with the /usr/bin/python shell and my python script pasted in.

Some things don't translate too well, though, especially since I'm not sure how it deals with python's open('filename','w') command. It probably doesn't have permissions to create new files, or I'm entering the command incorrectly. I had the idea to instead output the processed file as text, capture it with Automator, and then save it to a new file.

To do so, I tried to use New Text File, but I can't get it to create a new text file for each file selected back in the beginning. Is it possible to loop through all the selected Finder Items?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2024-10-14 04:19:59

为什么要在脚本文件夹中完成此操作?或者您是指从 Finder 项目中获取的文件的文件夹?在这种情况下,只需获取传递到 Python 的每个文件的路径即可。

当您运行 open('filename','w') 时,您应该传入完整路径名。可能发生的情况是您实际上正在写入根目录,而不是您认为的位置。

假设您将文件作为参数传递给 Automator 中的 shell 命令,那么您可能会遇到以下情况:

import sys, os

args = sys.argv[1:]
for a in args:
    p = os.path.dirname(a)
    mypath = p + "/" + name
    f = open(mypath, "w") 

Why do you want this done in the folder of the script? Or do you mean the folder of the files you are getting from the Finder items? In that case just get the path for each file passed into Python.

When you run open('filename','w') you should thus pass in a full pathname. Probably what's happening is you are actually writing to the root directory rather than where you think you are.

Assuming you are passing your files to the shell command in Automator as arguments then you might have the following:

import sys, os

args = sys.argv[1:]
for a in args:
    p = os.path.dirname(a)
    mypath = p + "/" + name
    f = open(mypath, "w") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文