I have a current script that fills out a template word doc using MailMerge. The issue I noticed is that after iterating through 15 rows/documents, the process works correctly, but in my Task Manager, there is 15 open idle Microsoft Word applications open.. This is not ideal as this will be used daily and should shut down the app after the doc has been created. Any idea on what to do? Will the Word app close after being idle for x amount of time or should I try to programmatically fix this?
for i in range(len(exampleData)):
#Open Template
template = r'exampletemplate.docx'
#Create Editable Doc
document = MailMerge(template)
#Create Word Naming Convention - Whatever the files should be named with
InsuredName = exampleData.loc[i, "InsuredName"]
PolicyNumber = exampleData.loc[i, "PolicyNumber"]
#Write the values into the doc
document.merge(
InsuredName = InsuredName,
Address1 = exampleData.loc[i, "Address1"],
Address2 = exampleData.loc[i, "Address2"],
ProducerName = exampleData.loc[i, "ProducerName"],
PolicyNumber = PolicyNumber,
ExpirationDate = datetime.strptime((str(exampleData.loc[i, "ExpirationDate"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y'),
DueDate = datetime.strptime((str(exampleData.loc[i, "DueDate"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y'),
EffectiveDate = datetime.strptime((str(exampleData.loc[i, "EffectiveDate"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y'),
ReportPeriodBegin = datetime.strptime((str(exampleData.loc[i, "ReportPeriodBegin"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y'),
ReportPeriodEnd = datetime.strptime((str(exampleData.loc[i, "ReportPeriodEnd"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y')
)
document.write(r'\\endpath\\' + PolicyNumber + '_' + InsuredName + '.docx')
I was facing the same problem. You're doing the process but you're not closing the template file.
try this:
document.close()
documentation
Related
I'm trying to get all tweets from 2018-01-01 until now from various firms.
My code works, however I do not get the tweets from the time range. Sometimes I only get the tweets from today and yesterday or from mid April up to now, but not since the beginning of 2018. I've got then the message: [!] No more data! Scraping will stop now.
ticker = []
#read in csv file with company ticker in a list
with open('C:\\Users\\veron\\Desktop\\Test.csv', newline='') as inputfile:
for row in csv.reader(inputfile):
ticker.append(row[0])
#Getting tweets for every ticker in the list
for i in ticker:
searchstring = (f"{i} since:2018-01-01")
c = twint.Config()
c.Search = searchstring
c.Lang = "en"
c.Panda = True
c.Custom["tweet"] = ["date", "username", "tweet"]
c.Store_csv = True
c.Output = f"{i}.csv"
twint.run.Search(c)
df = pd. read_csv(f"{i}.csv")
df['company'] = i
df.to_csv(f"{i}.csv", index=False)
Does anyone had the same issues and has some tip?
You need to add the configuration parameter Since separately. For example:
c.Since = "2018-01-01"
Similarly for Until:
c.Until = "2017-12-27"
The official documentation might be helpful.
Since (string) - Filter Tweets sent since date, works only with twint.run.Search (Example: 2017-12-27).
Until (string) - Filter Tweets sent until date, works only with twint.run.Search (Example: 2017-12-27).
I can get all appointments from my main calendar, like this:
def getCalendarEntries():
Outlook = win32com.client.Dispatch("Outlook.Application")
ns = Outlook.GetNamespace("MAPI")
appointments = ns.GetDefaultFolder(9).Items
appointments.Sort("[Start]")
appointments.IncludeRecurrences = "True"
begin = datetime.date.today()
end = begin + datetime.timedelta(days = 100);
restriction = "[Start] >= '" + begin.strftime("%m/%d/%Y") + "' AND [End] <= '" +end.strftime("%m/%d/%Y") + "'"
restrictedItems = appointments.Restrict(restriction)
events={'Start':[],'End':[],'Organizer':[],'Subject':[],'Duration':[]}
for a in restrictedItems:
events['Start'].append(a.Start)
events['End'].append(a.End)
events['Organizer'].append(a.Organizer)
events['Subject'].append(a.Subject)
events['Duration'].append(a.Duration)
return events
And I can save events into my main calendar, like this:
def addevent(start, subject, duration):
Outlook = win32com.client.Dispatch("Outlook.Application")
appointment = Outlook.CreateItem(1) # 1=outlook appointment item
appointment.Start = start
appointment.Subject = subject
appointment.Duration = duration
appointment.Save()
My issue is that I don't know how to connect to another calendar folder. I don't want the "DefaultFolder" but a specific one. I would be really greatful if someone could help me.
Found the answer myself. One has to replace
appointments = ns.GetDefaultFolder(9).Items
with
recipient = ns.createRecipient("foo#outlook.com")
resolved = recipient.Resolve()
appointments = ns.GetSharedDefaultFolder(recipient, 9).Items
another way of read Items in another calender with subfolders i.e. \abc#outlook.com\Calendar\Work_Exchange
could be:
privateol = ns.Folders('abc#outlook.com')
privatecal = privateol.Folders('Calendar')
privatesubfolder = privatecal.Folders('Work_Exchange')
What i don't know is, how to write in this specific Subfolder, so if anyone wants to provide...
lg
Edit:
to write to the specific subfolder, one has to replace appointment.Save() with appointment.Move(privatesubfolder)
maybe it's not the best solution, but it works
Had exact the same issue but got brilliant help: use the Items.Add() Method and it perfectly works in whatever folder you are in. See here: Create Outlook appointment in subfolder/subcalendar with python
I try to make an application to read events from Outlook shared calendar. I use Python 3.8.0 on win10. Here is my function to do this.
def getSharedCalendarEntries(TS_name, days=1000): #TS_name is name of shared calendar
MailboxToAccess = 'owner#gmail.com'
Outlook = win32com.client.Dispatch("Outlook.Application")
namespace = Outlook.GetNamespace("MAPI")
recipient = namespace.createRecipient(MailboxToAccess)
resolved = recipient.Resolve()
sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9).Folders(TS_name).Items
sharedCalendar.Sort("[Start]")
sharedCalendar.IncludeRecurrences = 'True'
today = datetime.datetime(2019,1,1)
begin = today.date().strftime('%d/%m/%Y')
tomorrow = datetime.timedelta(days=days)+today
end = tomorrow.date().strftime('%d/%m/%Y')
sharedCalendar = sharedCalendar.Restrict("[Start] >= '" +begin+ "' AND [END] <= '" +end+ "'")
events = {'Start':[],'End':[],'Subject':[],'Duration':[]}
mEv = []
for app in sharedCalendar: #petla po rezerwacjach
adate = datetime.datetime(app.Start.year, app.Start.month, app.Start.day).date()
events['Start'].append(adate)
aend = datetime.datetime(app.End.year, app.End.month, app.End.day).date()
events['End'].append(aend)
events['Duration'].append(int(app.Duration/1440))
events['Subject'].append(app.Subject)
mEvent = Event(adate, aend, int(app.Duration/1440), app.Subject)
mEv.append(mEvent)
return mEv
Everything was working and I was able to read events, but suddenly something happened (I didn't change anything in code) and I have such error:
File "C:\Users\user_catalog\Desktop\outread.py", line 60, in
getSharedCalendarEntries
sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9).Folders(TS_name).Items
File
"C:\Users\user_catalog\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\dynamic.py",
line 197, in call
return self._get_good_object_(self.oleobj.Invoke(*allArgs),self.olerepr.defaultDispatchName,None)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096,
'Microsoft Outlook', ' An attempt to perform the operation failed.
Could not find the object.', None, 0, -2147221233), None)
I had read-only access to shared calendars. Owner of shared calendars said that she logged-out of network, and time of logged out was the same time my application stopped working.
Have any of you had such problem or have some tips for me?
Thank you in advance!
Pio
Would you mind trying the code below? It will get you a dataframe with subject, occurence of the meeting, start time and end time.
import win32com.client, datetime
import pandas as pd
from datetime import time, timedelta
#connect to outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
appointments = outlook.GetDefaultFolder(9).Items #Calendário
#use these 2 lines to short the list
appointments.Sort('[Start]')
#Recurrent events only show the first start date, use the following to get the REAL occurrence of the event.
appointments.IncludeRecurrences = 'True'
#Beginning of the week(monday) to end of week(friday)
today = datetime.date.today()
start = today - timedelta(days=today.weekday())
end = start + timedelta(days=5)
#String of restriction time that will be used to filter dates on outlook
restriction = "[Start] >= '" + start.strftime("%d/%m/%Y") + "' AND [End] < '" +end.strftime("%d/%m/%Y") + "'"
print(restriction)
restrictedItems = appointments.Restrict(restriction)
#create empty data frame with columns to be fetched
i = 0
calendario = pd.DataFrame(columns=['Subject', 'When', 'Start Time','End Time'])
#Loop on items to fill the dataframe
for appointmentItem in restrictedItems:
calendario.loc[i] = [appointmentItem.Subject,appointmentItem.Start.strftime("%m/%d/%Y"),appointmentItem.Start.strftime("%H:%M"),appointmentItem.End.strftime("%H:%M")]
i = i + 1
#display dataframe
calendario
I'm fairly new to AWS and for the past week, been following all the helpful documentation on the site.
I am currently stuck on bring unable to pull the External Image Id data from a Reko collection after a 'search face by image', I just need to be able to put that data into a variable or to print it, does anybody know how I could do that?
Basically, this is my code:
import boto3
if name == "main":
bucket = 'bucketname'
collectionId = 'collectionname'
fileName = 'test.jpg'
threshold = 90
maxFaces = 2
admin = 'test'
targetFile = "%sTarget.jpg" % admin
imageTarget = open(targetFile, 'rb')
client = boto3.client('rekognition')
response = client.search_faces_by_image(CollectionId=collectionId,
Image={'Bytes': imageTarget.read()},
FaceMatchThreshold=threshold,
MaxFaces=maxFaces)
faceMatches = response['FaceMatches']
print ('Matching faces')
for match in faceMatches:
print ('FaceId:' + match['Face']['FaceId'])
print ('Similarity: ' + "{:.2f}".format(match['Similarity']) + "%")
at the end of it, I receive:
Matching faces
FaceId:8081ad90-b3bf-47e0-9745-dfb5a530a1a7
Similarity: 96.12%
Process finished with exit code 0
What I need is the External Image Id instead of the FaceId.
Thanks!
So I have an entry in the DB.
article = BlogPost.objects.get(id=1)
I can successfully verify the date of it via day, month and year
article.game_date.day = 26
article.game_date.month = 11
article.game_date.year = 2015
When I run a query to get this entry:
from datetime import date
todays_date = date.today()
articles2 = BlogPost.objects.get(game_date__year = todays_date.year, game_date__month = todays_date.month, game_date__day = todays_date.day)
It finds nothing. However when I run :
articles2 = BlogPost.objects.get(game_date__year = todays_date.year)
It does find it. But If I run:
articles2 = BlogPost.objects.get(game_date__day = todays_date.day)
or
articles2 = BlogPost.objects.get(game_date__day = 26)
It wont find it. It was working for a bit, but I can't figure out why it did this all of a sudden.