Created
June 29, 2019 08:15
-
-
Save PAG-ASAP/7feddd3cedfd5e9d34f2eb71d552813e to your computer and use it in GitHub Desktop.
Automated Image/PDF creation from excel and delivery of it via OUTLOOK.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import win32com.client as win32 | |
| from PIL import ImageGrab | |
| from PIL import Image | |
| import img2pdf | |
| import subprocess | |
| import traceback | |
| import time | |
| class MailDispatcher: | |
| global file_location, filename, mail | |
| file_location = 'C:/Data/' | |
| filename = "Battle GOT.xlsx" # in case you are using an macro based excel update the extension accordingly. | |
| # Kill all excel process | |
| subprocess.call(["taskkill", "/f", "/im", "EXCEL.EXE"]) | |
| # This is the function creating PDF from image, skip this if not needed. | |
| def pdf_maker(image_path, name): | |
| # storing pdf path | |
| pdf_path = file_location + name + ".pdf" | |
| # opening image | |
| image = Image.open(image_path) | |
| # converting into chunks using img2pdf | |
| pdf_bytes = img2pdf.convert(image.filename) | |
| # opening or creating pdf file | |
| file = open(pdf_path, "wb") | |
| # writing pdf files with chunks | |
| file.write(pdf_bytes) | |
| # closing image file | |
| image.close() | |
| # closing pdf file | |
| file.close() | |
| try: | |
| # This portion of the code deals with triggering of an macro, since we donot have a macro in our excel we are not using this. | |
| #xlApp = win32.DispatchEx('Excel.Application') | |
| #wb = xlApp.Workbooks.Open(file_location + filename) | |
| # Give the proper macro name | |
| #xlApp.Application.Run("Module.Macroname") | |
| #wb.Save() | |
| #wb.Close() | |
| #xlApp.Application.Quit() | |
| #xlApp = win32.gencache.EnsureDispatch('Excel.Application') ## use this to trouble shoot | |
| xlApp = win32.DispatchEx('Excel.Application') | |
| wb = xlApp.Workbooks.Open(file_location + filename) | |
| ws = wb.Worksheets('Graph') # name of the sheet in which graph is present. | |
| win32c = win32.constants | |
| ws.Range("B2:I16").CopyPicture(Format=win32c.xlBitmap) # give the cells for which you need the image | |
| img = ImageGrab.grabclipboard() | |
| img.save(file_location + 'Graph.jpeg',quality=55) # image quality and file size is directly linked | |
| pdf_maker(file_location + 'Graph.jpeg', 'Graph') # second parameter contains the name with which the pdf will be saved | |
| # This portion deals with the mail part | |
| outlook = win32.Dispatch('outlook.application') | |
| ## Workaround to send the mail from a different mail id----------- skip this part if you want to send mail from your default mail box | |
| sendfromAC = None | |
| for oacc in outlook.Session.Accounts: | |
| if oacc.SmtpAddress == "[email protected]": # Mail id from which to send the mail | |
| sendfromAC = oacc | |
| break | |
| ##---------------------------------------------------------------- | |
| mail = outlook.CreateItem(0) | |
| ##---------------------------------------------------------------- | |
| if sendfromAC: | |
| mail._oleobj_.Invoke(*(64209, 0, 8, 0, sendfromAC)) # Msg.SendUsingAccount = oacctouse | |
| ##---------------------------------------------------------------- | |
| mail.To = '[email protected]' | |
| mail.Cc = '[email protected];[email protected]' | |
| mail.Subject = 'Demo Outlook Automation mail' | |
| mail.Attachments.Add(file_location + "Graph.pdf") | |
| attachment1 = mail.Attachments.Add(file_location + 'Graph.jpeg') | |
| attachment1.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "Graph") | |
| mail.HTMLBody = "<HTML lang='en' xmlns='http://www.w3.org/1999/xhtml' xmlns:o='urn:schemas-microsoft-com:office:office'> " \ | |
| + "<head>" \ | |
| + "<!--[if gte mso 9]><xml> \ | |
| <o:OfficeDocumentSettings> \ | |
| <o:Allowjpeg/> \ | |
| <o:PixelsPerInch>96</o:PixelsPerInch> \ | |
| </o:OfficeDocumentSettings> \ | |
| </xml> \ | |
| <![endif]-->" \ | |
| + "</head>" \ | |
| + "<BODY>" | |
| mail.HTMLBody = mail.HTMLBody + "<BR>Hello,<b> </b>" \ | |
| + "<BR><BR> Refer to the image below: </b> ."\ | |
| + "<html><body><img src='cid:Graph'></body></html>" | |
| mail.Send() | |
| wb.Close(True) | |
| xlApp.Quit() | |
| time.sleep(10) # waiting for the mail to be sent from outlook | |
| print("Mail delivery completed successfully.") | |
| except Exception as ex: | |
| subprocess.call(["taskkill", "/f", "/im", "EXCEL.EXE"]) | |
| print("Something went wrong! Detailed error message is given below : ") | |
| traceback.print_tb(ex.__traceback__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment