I m trying to have an code that permit to add harvest timesheets in excel automatically with a python code (which works fine). So using xlwings, I add this code. It seems working as it takes one minute to work but nothing is imported in excel file. Could some can help me ?
def timed():
# id of the API
headers = {
"Authorization": "Bearer " + "XXXXXXXX",
"Harvest-Account-ID": "XXXXX"
}
# define login infos
headers = headers
# define period for saving info about date of file update
current_time = datetime.datetime.now()
Year = current_time.year
Month = current_time.month
actualMonth = "{:02d}".format(Month) # mis au format 00 du mois
day = current_time.day
period = str(Year) + '-' + str(actualMonth) + '-' + str(day)
# connection to harvest
# premier jour de l'année
begin_period = str(Year) + '-' + '01' + '-' + '01'
url_address = "https://api.harvestapp.com/v2/time_entries?from=" + begin_period + "&per_page=100"
response = requests.get(url_address, headers=headers)
r = requests.get(url=url_address, headers=headers).json()
# add info about number of pages
try:
total_pages = int(r['total_pages'])
except:
total_pages = 1
timeentries = []
# add data to pandas dataframe
for page in range(0, total_pages):
pages = page + 1
url = "https://api.harvestapp.com/v2/time_entries?from=" + begin_period + "&page=" + str(
pages) + "&per_page=100"
response = requests.get(url=url, headers=headers).json()
test = response['time_entries']
for tet in test:
e = dict()
e['id'] = tet['id']
e['client'] = tet['client']['name']
e['user'] = tet['user']['id']
e['user_name'] = tet['user']['name']
e['ID_project'] = tet['project']['id']
e['ProjectName'] = tet['project']['name']
e['hours'] = tet['hours']
e['billable'] = tet['billable']
e['Billed'] = tet['is_billed']
e['spent_date'] = tet['spent_date']
try:
e['invoice'] = tet['invoice']['id']
except:
e['invoice'] = ''
try:
e['invoice_name'] = tet['invoice']['number']
except:
e['invoice_name'] = ''
timeentries.append(e)
# transfer to panda frame
df = pd.DataFrame(timeentries)
@xw.func
@xw.ret(index=False, header=True, expand='table')
def ret_data_frame():
return timed()
Objectif is to update all timesheet in an excel table...
Hin find my error. in first def timed() just add at the end: return df