# parse-sheet.py
from datetime import datetime, timedelta, date
import json
book_days = 15
now = datetime.now()
date_format = "%m/%d/%Y"
outstring=""" Preamble text
"""
# Set the Header
# Location, Date, Type, Due Date
line_format ="{0:<25}{1:<25}{2:<25}{3:<25}\n"
outstring+=line_format.format("Location:", "Date:", "Type:", "Due Date:")
data=json.loads(input_data['rows'])
for row in data:
try:
event_date=datetime.strptime(row[0], date_format)
due_date=event_date - timedelta(days=book_days)
event_out=event_date.strftime(date_format)
due_out=due_date.strftime(date_format)
except:
pass
location=row[1]
type=row[2]
if event_date > now:
line=line_format.format(location,
event_out,
type,
due_out
)
outstring+=line
output={'output':outstring}
I’m using the code above to grab some data from a spreadsheet, do some basic conversions and determine whether to output the data.
It doesn’t seem the Python string formatting is working as expected, though I suspect it has something to do with the JSON serialization happening with the output dictionary. The output doesn’t align properly for the fields.:
'Location:' 'Date:' 'Type:' 'Due Date:'
'Raleigh,NC' '05/21/2024' 'Direct Customer' '05/06/2024'
'Cary, NC' '05/21/2024' 'Field Marketing' '05/06/2024'
'Durham, NC' '05/22/2024' 'Direct Customer' '05/07/2024'
Is there a better “Zapier” way to be doing this? Totally fine just passing a bunch of values and letting Zapier format the output.
Or is there a way to keep consistent string formats in the output string.