-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoldsOlderThan5DaysWithAvailableItems.PY
114 lines (74 loc) · 2.86 KB
/
HoldsOlderThan5DaysWithAvailableItems.PY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
#!/usr/bin/python2.7
#
# Use XlsxWriter to create spreadsheet from SQL Query
#
#
import psycopg2
import xlsxwriter
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
from datetime import datetime
excelfile = 'HoldsOlderThan5DaysWithAvailableItems.xlsx'
#Set variables for email
emailhost = 'mail.greenwichlibrary.org'
emailport = '25'
emailsubject = 'Holds Older than 5 days'
emailmessage = '''Attached are all bib level holds placed more than 3 days ago with available items attached.'''
emailfrom = 'nallen@greenwichlibrary.org'
emailto = ['nallen@greenwichlibrary.org','jeaton@greenwichlibrary.org']
try:
conn = psycopg2.connect("dbname= user= host= port= password= sslmode=require")
except psycopg2.Error as e:
print ("Unable to connect to database: " + str(e))
cursor = conn.cursor()
cursor.execute(open("HoldsOlderThan5DaysWithAvailableItems.sql","r",).read())
rows = cursor.fetchall()
conn.close()
workbook = xlsxwriter.Workbook(excelfile, {'remove_timezone': True})
worksheet = workbook.add_worksheet()
worksheet.set_landscape()
worksheet.hide_gridlines(0)
eformat= workbook.add_format({'text_wrap': True, 'valign': 'top' , 'num_format': 'mm/dd/yy'})
eformatlabel= workbook.add_format({'text_wrap': False, 'valign': 'top', 'bold': True})
worksheet.set_column(0,0,8.67)
worksheet.set_column(1,1,17.67)
worksheet.set_column(2,2,51.33)
worksheet.set_column(3,3,25.78)
worksheet.set_column(4,4,18)
worksheet.set_header('Holds Older Than 3 Days')
worksheet.write(0,0,'Location', eformatlabel)
worksheet.write(0,1,'Call Number', eformatlabel)
worksheet.write(0,2,'Title', eformatlabel)
worksheet.write(0,3,'Author', eformatlabel)
worksheet.write(0,4,'Barcode', eformatlabel)
for rownum, row in enumerate(rows):
worksheet.write(rownum+1,0,row[0], eformat)
worksheet.write(rownum+1,1,row[1], eformat)
worksheet.write(rownum+1,2,row[2])
worksheet.write(rownum+1,3,row[3], eformat)
worksheet.write(rownum+1,4,row[4], eformat)
workbook.close()
#Creating the email message
msg = MIMEMultipart()
msg['From'] = emailfrom
if type(emailto) is list:
msg['To'] = ','.join(emailto)
else:
msg['To'] = emailto
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = emailsubject
msg.attach (MIMEText(emailmessage))
part = MIMEBase('application', "octet-stream")
part.set_payload(open(excelfile,"rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename=%s' % excelfile)
msg.attach(part)
#Sending the email message
smtp = smtplib.SMTP(emailhost, emailport)
smtp.sendmail(emailfrom, emailto, msg.as_string())
smtp.quit()