I’m engaged on making a solution to proper click on on a .eml file, use “fast actions” and have python pull two datum from it and paste two datum into a brand new xlsx file and auto open the xlsx file .
A way is to have a shellscript in Automator name a .py file which calls a .ipynb file.
The motion “Run Shell Script” encountered an error: After I run it on a .eml file I see, “/Purposes/Xcode.app/Contents/Developer/usr/bin/python3: cannot open file '/customers///Paperwork/run_notebook1.py': [Errno 1] Operation not permitted”
After I use automator to check the script with “get specified finder objects”
The motion “Run Shell Script” encountered an error: “Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program identify="/Customers//choose/anaconda3/bin/python"
remoted = 0
surroundings = 1
consumer web site = 1
import web site = 1
sys._base_executable="/Customers//choose/anaconda3/bin/python"
sys.base_prefix = '/Customers//choose/anaconda3'
sys.base_exec_prefix = '/Customers//choose/anaconda3'
sys.platlibdir="lib"
sys.executable="/Customers//choose/anaconda3/bin/python"
sys.prefix = '/Customers//choose/anaconda3'
sys.exec_prefix = '/Customers//choose/anaconda3'
sys.path = [
'/Users//opt/anaconda3/lib/python39.zip',
'/Users//opt/anaconda3/lib/python3.9',
'/Users//opt/anaconda3/lib/python3.9/lib-dynload',
]
Deadly Python error: init_fs_encoding: did not get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
>Present thread 0x00000001ef41cc00 (most up-to-date name first):
<no Python body>
Traceback (most up-to-date name final):
File "/customers//Paperwork/run_notebook1.py", line 27, in <module>
run_notebook(eml_file)
File "/customers//Paperwork/run_notebook1.py", line 16, in run_notebook
subprocess.run(command, shell=True,verify=True)
File "/Purposes/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Variations/3.9/lib/python3.9/subprocess.py", line 528, in run
elevate CalledProcessError(retcode, course of.args,
subprocess.CalledProcessError: Command '/Customers//choose/anaconda3/bin/jupyter nbconvert --to pocket book --execute --inplace --ExecutePreprocessor.timeout=-1 /Customers//Paperwork/process_email1.ipynb' returned non-zero exit standing 1.
Is the reply the place is one is meant to search out python39.zip?
sys.path = [
'/Users//opt/anaconda3/lib/python39.zip',
'/Users//opt/anaconda3/lib/python3.9',
'/Users//opt/anaconda3/lib/python3.9/lib-dynload'
I ran the .py file in terminal using python3 file.py without problems.
Shellscript in Automator (xcode is installed so I don’t need to specify python directory):
python3 /users//Documents/run_notebook1.py "$1"
The .py file:
import sys
import subprocess
import os
def run_notebook(eml_file):
# Path to the notebook
notebook_path="/Users//Documents/process_email1.ipynb"
# Set environment variable
os.environ['EML_FILE_PATH'] = eml_file
# Command to run the pocket book with nbconvert
command = f'/Customers//choose/anaconda3/bin/jupyter nbconvert --to pocket book --execute --inplace --ExecutePreprocessor.timeout=-1 {notebook_path}'
# Run the command
subprocess.run(command, shell=True,verify=True)
# Make sure the file path is handed as argument
if len(sys.argv) != 2:
print("Utilization: python run_notebook.py <path_to_eml_file>")
sys.exit(1)
# Get the EML file path
eml_file = sys.argv[1]
# Run the pocket book with the EML file path
run_notebook(eml_file)
The .ipynb file
import e-mail
import pandas as pd
import os
from e-mail import coverage
from e-mail.parser import BytesParser
from datetime import datetime
import subprocess
# Retrieve the EML file path from the surroundings variable
eml_file = os.getenv('EML_FILE_PATH')
# Verify if the surroundings variable is ready
if not eml_file:
elevate ValueError("EML_FILE_PATH surroundings variable not set. Please cross the file path.")
# Perform to extract e-mail file info
def extract_eml_info(eml_file):
# Parse the .eml file
with open(eml_file, 'rb') as f:
msg = BytesParser(coverage=coverage.default).parse(f)
# Extract the file identify (with out extension)
file_name = os.path.splitext(os.path.basename(eml_file))[0]
# Extract the latest date of the e-mail dialog
date_str = msg['date']
if date_str:
strive:
email_date = e-mail.utils.parsedate_to_datetime(date_str)
besides Exception as e:
email_date = None
else:
email_date = None
return file_name, email_date
# Perform to take away timezone info from DataFrame
def remove_timezone_from_df(df):
# Iterate over all columns
for col in df.columns:
if pd.api.varieties.is_datetime64_any_dtype(df[col]):
# Take away timezone information if it is a datetime column
df[col] = df[col].dt.tz_localize(None)
return df
# Perform to course of EML information
def process_eml_files(eml_files):
# Listing to retailer extracted information
information = []
for eml_file in eml_files:
file_name, email_date = extract_eml_info(eml_file)
information.append([file_name, email_date])
# Create DataFrame
df = pd.DataFrame(information, columns=['File Name', 'Most Recent Date'])
df = remove_timezone_from_df(df)
# Write to Excel
output_file="output_eml_data.xlsx"
df.to_excel(output_file, index=False, engine="openpyxl")
print(f"Information written to {output_file}")
# Open the newly created Excel file
if os.identify == 'nt': # For Home windows
os.startfile(output_file)
elif os.identify == 'posix': # For macOS/Linux
subprocess.name(['open', output_file])
return output_file
# Course of the offered EML file
process_eml_files([eml_file])