Тёмный

Create and Distribute Excel Files using Python and Outlook | Automate Excel with Python 

Coding Is Fun
Подписаться 100 тыс.
Просмотров 15 тыс.
50% 1

👉 Explore All My Excel Solutions: pythonandvba.c...
𝗗𝗘𝗦𝗖𝗥𝗜𝗣𝗧𝗜𝗢𝗡
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
In this video, I will demonstrate how you can use Python (Pandas) to automate the creation of Excel files, which you can then distribute via email to a list of contacts. To send out the emails, we are going to use the Desktop application of Microsoft Outlook.
▶ Download the Source Code & Sample Files here: github.com/Sve...
𝗧𝗢𝗢𝗟𝗦 𝗔𝗡𝗗 𝗥𝗘𝗦𝗢𝗨𝗥𝗖𝗘𝗦
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
🆓【𝗙𝗥𝗘𝗘】Excel Add-in (𝗠𝘆𝗧𝗼𝗼𝗹𝗕𝗲𝗹𝘁): pythonandvba.c...
📊 Dashboard Excel Add-In (𝗚𝗿𝗮𝗳𝗹𝘆): pythonandvba.c...
🎨 Cartoon Charts Excel Add-In (𝗖𝘂𝘁𝗲𝗣𝗹𝗼𝘁𝘀): pythonandvba.c...
🤪 Fun Emoji Excel Add-In (𝗘𝗺𝗼𝗷𝗶𝗳𝘆): pythonandvba.c...
📑 Excel Templates: pythonandvba.c...
🎓 My Courses: pythonandvba.c...
📚 Books, Tools, and More: pythonandvba.c...
𝗖𝗢𝗡𝗡𝗘𝗖𝗧 𝗪𝗜𝗧𝗛 𝗠𝗘
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
🔗 LinkedIn: / sven-bosau
📸 Instagram: / codingisfun_official
💻 GitHub: github.com/Sve...
💬 Discord: pythonandvba.c...
📬 Contact: pythonandvba.c...
☕ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲?
If you want to support this channel, you can buy me a coffee here: pythonandvba.c...

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 104   
@CodingIsFun
@CodingIsFun 2 года назад
*If you have any questions or want to see more excel automation examples using Python, let me know in the comments.*
@CodingIsFun
@CodingIsFun 2 года назад
@JMan Tam Thanks for watching. In the video, I have used an HTML body with variables. So, I am not sure if I understand your question. Can you perhaps elaborate on what it is you're actually trying to achieve? Please provide some additional info. Thanks!
@CodingIsFun
@CodingIsFun 2 года назад
@JMan Tam That is very similar to what I have demonstrated in the video. Check it out here: 05:17min
@dimaua1830
@dimaua1830 2 года назад
Great video. I hope your channel will grow. It's almost exactly the workflow I am trying to automate. However I am looking for the code to run on Mac OS, which doesn't support win32.
@CodingIsFun
@CodingIsFun 2 года назад
@@dimaua1830 Thank you very much! You are right; win32 will only work on Windows. For implementing something similar on macOS, you might want to check out the following solution: stackoverflow.com/questions/61529817/automate-outlook-on-mac-with-python I hope this helps!
@abdulamirocarlosdecastro8751
@abdulamirocarlosdecastro8751 2 года назад
Hi Sven awesome 😍! If could change year to daily reports, i could i do it automatically? Thanks in advance Abdul
@marco_rock_60s
@marco_rock_60s 8 месяцев назад
I love how you get ahead of people's questions by showing solutions to problems/complications that people may experience. You're once step ahead. Awesome stuff!
@CodingIsFun
@CodingIsFun 8 месяцев назад
Thanks for the positive feedback and your comments! I appreciate it! 🙏
@ermalgashimramori
@ermalgashimramori 2 года назад
It's always a pleasure seeing you up the level of the content, please keep up the good work. Time spent watching your videos is quality time, thank you!
@CodingIsFun
@CodingIsFun 2 года назад
For me, it is always a pleasure reading your valuable comments. Seeing your comments really motivates me. Thank you so much!
@thamilanban
@thamilanban 2 года назад
Thank you. You deserve a million subscribers.
@CodingIsFun
@CodingIsFun 2 года назад
Thank you! 😃❤
@ahmedsalman3294
@ahmedsalman3294 Год назад
This worked as smooth as it was supposed to be. I love your way to explaining the code line by line. It was a use_case for me, and I am glad to say that you are a life saver. This is fantastic! 😊😊
@CodingIsFun
@CodingIsFun Год назад
Wow, thank you for the positive feedback! I'm so glad to hear that the video was smooth and helpful for you. It's always great to know that my explanations were clear and useful. Thank you for taking the time to leave a comment and share your experience with me. :)
@ahmedsalman3294
@ahmedsalman3294 Год назад
@@CodingIsFun How can we format each excel file before distributiion? i.e., setting columns width, number formatting, adding totals etc.?
@CodingIsFun
@CodingIsFun Год назад
@@ahmedsalman3294 When you export the files, you can access the files using the xlsxwriter package: www.screencast.com/t/w2nPwpRmzY21 xlsxwriter.readthedocs.io/working_with_pandas.html Happy Coding!
@ahmedsalman3294
@ahmedsalman3294 Год назад
@@CodingIsFun Thanks a ton. I did and I managed to get the result I wanted.
@rajeevmenon1975
@rajeevmenon1975 2 года назад
As always Sven, real worthwhile content. Keep up the good work!! You are a rockstar
@CodingIsFun
@CodingIsFun 2 года назад
Thanks a ton, Rajeev Menon! 💪🏽 I appreciate your support!
@bc4198
@bc4198 2 года назад
OMG I love your videos! Using coding to solve little tasks like this is *exactly* the kind of thing I aspire to, and perfect for my needs and interests. Thank you!
@CodingIsFun
@CodingIsFun 2 года назад
Thanks for the kind comments, and happy to hear that you are enjoying the videos! Happy Coding & Automating! 🦾
@AbCdEf-vh7jj
@AbCdEf-vh7jj Год назад
Hi Sven! It's admirable how you manage to display your work in just 7 minutes and make it so enjoyable! I got one question: As you work with win32 here (which works with win64 according to another comment of you): Does it also work on the Outlook version of *APPLE*? What's the prerequisite for implementing this? Do you have to download the desktop version of Outlook for this? Glad if you you could add some Know-how on this. Thanks a lot and keep it on. It's a great joy to check out your great progress!
@CodingIsFun
@CodingIsFun Год назад
Hey there! Appreciate the love! 🙏 For Outlook on Apple, you might want to explore using PyUNO or unotools, which are tailored for macOS. However, I don't have a macOS setup, so I can't test it or provide you more information on that. Hope this points you in the right direction! Happy Coding! :)
@AsnakewAssefaNegash
@AsnakewAssefaNegash 2 года назад
I love watching your videos.
@CodingIsFun
@CodingIsFun 2 года назад
*I am glad to hear you are enjoying them, thanks for watching and for the comment!*
@maevabj
@maevabj Год назад
Thank you so much for this incredible video. This was the first video I saw that didn't need to add passwords and settings. Fits perfectly. I would like to know if you have any videos that I can watch that help me create a logic that changes the content of mail.HTMLBody according to values found in the csv that we export. Example, if Value is greater than 60, it shows one content, if it is smaller, another. If zero, a specific... Have a great week and thanks again!
@CodingIsFun
@CodingIsFun Год назад
Thanks so much for watching the video and leaving a comment! Your request is definitely noted. However, I get a ton of requests for custom solutions and, as much as I'd love to help everyone out, I just don't have the time in my schedule to develop and test all of them. I hope you can understand. Happy Coding!
@ryanbuzar5392
@ryanbuzar5392 2 года назад
Great tutorial! That was very helpful! Thank you for sharing!
@CodingIsFun
@CodingIsFun 2 года назад
Glad you liked it. Thanks for watching.
@datalyticsbootcamp
@datalyticsbootcamp 2 года назад
Great video! I could see myself creating something like this.
@CodingIsFun
@CodingIsFun 2 года назад
Thank you! Glad you liked the video :)
@bottega86
@bottega86 10 месяцев назад
Thanks @CodingIsFun , very informative. Are you able to provide a video for the same exact steps but using VBA to execute this process pls? Much appreciated!
@CodingIsFun
@CodingIsFun 9 месяцев назад
Thank you very much for watching the video and your comment. Your requirements are well noted. Yet, I receive many requests for creating individual solutions. As much as I want to help, I simply do not find the time in my daily schedule to develop & test all the different requests. I hope you can understand. Thank you!
@Chrisfagan8881
@Chrisfagan8881 2 года назад
Cool video. Would like to see how you could password protect the files before sending.
@CodingIsFun
@CodingIsFun 2 года назад
Thanks for watching the video & your question. When you are saving the attachments (excel files), you could specify a password. Have a look at the following example: Credit: stackoverflow.com/questions/36122496/password-protecting-excel-file-using-python from pathlib import Path import subprocess import pandas as pd # pip install pandas openpyxl # Locate examples files & create output directory EXCEL_FILE_PATH = Path.cwd() / "Financial_Data.xlsx" ATTACHMENT_DIR = Path.cwd() / "Attachments" ATTACHMENT_DIR.mkdir(exist_ok=True) # Load financial data into dataframe data = pd.read_excel(EXCEL_FILE_PATH, sheet_name="Data") # Get unique values from any particular column column_name = "Country" unique_values = data[column_name].unique() def set_password(excel_file_path, pw): excel_file_path = Path(excel_file_path) vbs_script = \ f"""' Save with password required upon opening Set excel_object = CreateObject("Excel.Application") Set workbook = excel_object.Workbooks.Open("{excel_file_path}") excel_object.DisplayAlerts = False excel_object.Visible = False workbook.SaveAs "{excel_file_path}",, "{pw}" excel_object.Application.Quit """ # write vbs_script_path = excel_file_path.parent.joinpath("set_pw.vbs") with open(vbs_script_path, "w") as file: file.write(vbs_script) #execute subprocess.call(['cscript.exe', str(vbs_script_path)]) # remove vbs_script_path.unlink() return None # Query/Filter the dataframe and export the filtered dataframe as an Excel file for unique_value in unique_values: data_output = data.query(f"{column_name} == @unique_value & Year==2021") output_path = ATTACHMENT_DIR / f"{unique_value}_2021.xlsx" data_output.to_excel(output_path, sheet_name=unique_value, index=False) set_password(output_path, "password")
@abdulamirocarlosdecastro8751
@abdulamirocarlosdecastro8751 2 года назад
Hi Sven awesome 😍! If could change year to daily reports, i can do it automatically? Thanks in advance Abdul
@CodingIsFun
@CodingIsFun 2 года назад
Thanks for watching. You could schedule your Python script to run daily using the Windows Task scheduler. Have a look at the following tutorial: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-n2Cr_YRQk7o.html I hope it helps! Happy Sending!
@mariamazeez7089
@mariamazeez7089 2 года назад
This is helpful and wished i had seen this earlier! New subscriber alert. Question please: what if the master file has two tabs and you want to pull from the two tabs into two tabs in the individual files
@CodingIsFun
@CodingIsFun 2 года назад
Thanks for watching. Sorry, but I am not sure what you are trying to achieve. Please provide some additional information and the code you have tried. Thanks!
@noone370
@noone370 Год назад
AMAZING! Thanks! :)
@CodingIsFun
@CodingIsFun Год назад
Glad you liked it. Thanks for watching. 👍
@sachin.tandon
@sachin.tandon 2 года назад
This is a well created Tutorial!
@CodingIsFun
@CodingIsFun 2 года назад
Thanks a lot!
@sureshpatil553
@sureshpatil553 2 года назад
Please consider , making project on Split, Marge , Deleting some pages in PDF through Python.
@CodingIsFun
@CodingIsFun 2 года назад
Thank you for your video suggestions
@sinnombre429
@sinnombre429 Год назад
Thank you!, this tutorial is really useful. Sven, how We can insert an image of a chart to the body mail?
@CodingIsFun
@CodingIsFun Год назад
Happy to hear that it was useful. You could try to embed the image using the image html tag.
@mrx3327
@mrx3327 2 года назад
I am really enjoying and learning a lot from your videos. I have a question regarding the outlook mailing list. How you can add a different signature for instance each country email? 😊
@CodingIsFun
@CodingIsFun 2 года назад
I am happy to hear that you have been finding the videos helpful. Regarding your question: Instead of the Outlook signature, you could create your own using HTML. Therefore, insert a new column in the email distribution list in Excel. In the new column, insert your HTML signature. An easy way to generate the HTML code is to use the following free online tool: html-online.com/editor/ When creating the email (5:17 min), you want to concatenate your signature column with the actual body. I have not tested it out, but I assume that this should work. Happy Coding!
@jarosaw4013
@jarosaw4013 2 года назад
Perfect
@CodingIsFun
@CodingIsFun 2 года назад
Thanks!
@cristianoalves7322
@cristianoalves7322 7 месяцев назад
Hey there! Thanks so much for the help, really useful! Just one question, how can I adjust this so that I can send it from another mailbox instead? Thank you once again!
@CodingIsFun
@CodingIsFun 7 месяцев назад
Thank you very much for watching the video and your comment. Your requirements are well noted. Yet, I receive many requests for creating individual solutions. As much as I want to help, I simply do not find the time in my daily schedule to develop & test all the different requests. I hope you can understand. Thank you!
@cristianoalves7322
@cristianoalves7322 7 месяцев назад
@@CodingIsFun no worries! I totally understand! In the meantime I believe I solved my question with some further search!
@talivodonos1196
@talivodonos1196 Год назад
Hi, thank you so much for the video😀. I have a qoustion, how can I add a new sheet with different name ( month) at the existing file. Today I overloaded the existing excel file.
@CodingIsFun
@CodingIsFun Год назад
Thanks for watching. In this video, I will explain the different options to export the dataframe to an existing workbook: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-DroafWQXqDw.html Happy Coding!
@lsborba1
@lsborba1 2 года назад
Great video! Congratulations! I have just one question: Does it works fine in 64bit computers? I am asking because of the library win32 you used in this video. And I have one suggestion: More examples of interface between Python and Desktop applications like Microsoft Word.
@CodingIsFun
@CodingIsFun 2 года назад
Glad to hear that you liked the video. Thanks for watching and taking the time to leave a comment. Yes, the pywin32 library also works on a 64bit system. Also, thank you for the video suggestion. I already have one video on my channel on automating Word using Python (and Excel). Perhaps it might be helpful to you: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-T3meVMaV8AA.html Happy Coding!
@chayan885
@chayan885 Год назад
Hi there, Cant this be done without the attachment approach? What if I wish to paste the contents of the excel per unique email IDs in the body?
@CodingIsFun
@CodingIsFun Год назад
That might be possible. Feel free to try it out
@gomesarmando9816
@gomesarmando9816 2 года назад
@Coding Is fun, thank you this is very helpful 🙏 🔥 what if I want to attach multiples files to one or more receivers instead of one attach per receivers. does this method still work ?, should I create as much column in my Excel file (source), as I have files to attach ? Thank you very much !
@CodingIsFun
@CodingIsFun 2 года назад
Thank you very much for watching the video and your comment. Your requirements are well noted. Yet, I receive many requests for creating individual solutions. As much as I want to help, I simply do not find the time in my daily schedule to develop & test all the different requests. I hope you can understand. Thank you!
@Bzman38
@Bzman38 Год назад
Is it possible to do this with each excel having a column which includes a drop down list to select an option (data validation) ?
@CodingIsFun
@CodingIsFun Год назад
Yes, that is possible. After your selection (data validation), save the workbook and run the script.
@tyw321
@tyw321 Год назад
It is possible to do this, but when creating the statement keep a record in another sheet the include the country, date created, and total amount?
@CodingIsFun
@CodingIsFun Год назад
Thank you very much for watching the video and your comment. Your requirements are well noted. Yet, I receive many requests for creating individual solutions. As much as I want to help, I simply do not find the time in my daily schedule to develop & test all the different requests. I hope you can understand. Thank you!
@jshan4621
@jshan4621 2 года назад
had a question, what is unique_value? your unique_values is your list from unique() but i'm not understanding what unique_value is in your "for unique_value in unique_values" line.
@CodingIsFun
@CodingIsFun 2 года назад
2:27 min. It is creating a list with all unique items in a column. Let's say you have column A: A1: Fruits A2: Apple A3: Banana A4: Kiwi A5: Apple A6: Banana The unique list will look like this: Fruits = ["Apple", "Banana", "Kiwi"]. In the loop, I am then iterating over each item. I hope it helps!
@jshan4621
@jshan4621 2 года назад
@@CodingIsFun yes i got it! Thank you!
@vishnumenon7448
@vishnumenon7448 12 дней назад
Hi. What if one email id has 2 or more country’s attachment to be sent?! How can we attach two or more attachment for one email id?!
@CodingIsFun
@CodingIsFun 12 дней назад
Thanks so much for watching the video and leaving a comment! Your request is definitely noted. However, I get a ton of requests for custom solutions and, as much as I'd love to help everyone out, I just don't have the time in my schedule to develop and test all of them. I hope you can understand. Happy Coding!
@cvas-d
@cvas-d 2 года назад
Thank you so much for the brief explanations. Is there any other option other than outlook to send email?.. If yes, could you please help with that which will be much helpful.
@CodingIsFun
@CodingIsFun 2 года назад
Thanks for watching the video & your question. Yes, there are many ways to send out emails using Python. You might want to check out the following blog article: realpython.com/python-send-email/ I hope this helps! Happy Coding! 😀
@cvas-d
@cvas-d 2 года назад
Thanku so so much 😍👍
@SaulEduardoo2
@SaulEduardoo2 2 года назад
Hi Sven, I found this video fully useful as usual 🤓... It came up after applying it to a personal case, What if I already have a signature to use, how can I choose it from outlook to use it on the e-mail?
@CodingIsFun
@CodingIsFun 2 года назад
Glad you find it helpful. If you have already a signature, you could modify the code. Have a look at the following example: stackoverflow.com/a/43331219 I hope it helps! Happy Coding!
@SaulEduardoo2
@SaulEduardoo2 2 года назад
@@CodingIsFun Very helpful answer Sven! :D... However, I can't make that the code gets the image that I have in the signature
@jobingeorge1719
@jobingeorge1719 2 года назад
Great video man, unfortunately for me, the email iteration is not taking place. Only the last row email is being displayed in outlook. Please help
@CodingIsFun
@CodingIsFun 2 года назад
Thanks for watching and for your question. Hard to tell from a distance why you are facing an error. Sorry that I cannot help.
@jobingeorge1719
@jobingeorge1719 2 года назад
@@CodingIsFun Hey thanks, turns out it was just a syntax error in the for loop in the mailing part. I have one other query, is there any way to keep the source formatting of the original excel file? Once again, thank you so much. This video just reduced 3 hours of my workload
@mikekaspari1357
@mikekaspari1357 2 года назад
Interesting Video. I'll would like to try this on a costumer base.
@CodingIsFun
@CodingIsFun 2 года назад
Thank you & happy coding! :)
@mikekaspari1357
@mikekaspari1357 2 года назад
@@CodingIsFun Hi Sven, as mentioned I have modified this script for my costumer based purpose. Now I have following problem. In my Email-List there are 30 customer-mail addresses. But if I have less then 30 excel-lists, for example 25, I get an error-message that python can't not find the attachments. Do you have an idea how to solves this?
@CodingIsFun
@CodingIsFun 2 года назад
@@mikekaspari1357 Hi Mike, you might want to check if the file/attachment exists before sending it. Example below (original script: github.com/Sven-Bo/create-distribute-excel-files/blob/master/create_send_reports.py ): attachment_path = str(ATTACHMENT_DIR / "YourExcelFile.xlsx") if Path(attachment_path).is_file(): mail.Attachments.Add(Source=attachment_path) I hope this helps!
@mikekaspari1357
@mikekaspari1357 2 года назад
@@CodingIsFun thank you very much. I try it tomorrow on work. I give you a feedback
@mikekaspari1357
@mikekaspari1357 2 года назад
Hi Sven, it works. Thank you very much
@SaulEduardoo2
@SaulEduardoo2 2 года назад
Hi Sven, when I replaced the field Year==2021 to Month Number==9 the code sends me an error about "python keyword not valid identifier in numexpr query"
@CodingIsFun
@CodingIsFun 2 года назад
Use backticks to escape the empty space in the column name df.query("`Month Number` == 9")
@SaulEduardoo2
@SaulEduardoo2 2 года назад
@@CodingIsFun Ok, now I see what you mean by "Backticks". At Github aren't displayed since are taken as code... Thanks for the great support Sven :D!!🤗
@CodingIsFun
@CodingIsFun 2 года назад
@@SaulEduardoo2 I edited the GitHub comment ;)
@killua59933
@killua59933 2 года назад
Can you launch a video when you do the same with OS (py-appscript)?
@CodingIsFun
@CodingIsFun 2 года назад
Good suggestion, but I do not have a mac.
@bc4198
@bc4198 2 года назад
You could add code to reformat the Excel file, right?
@CodingIsFun
@CodingIsFun 2 года назад
When exporting the dataframes, you could also reformat the Excel files. One solution is to use xlsxwriter. Check out the docs here: xlsxwriter.readthedocs.io/working_with_pandas.html
@laffiny
@laffiny 2 года назад
Really Good Videos! I have a challenge. I have a dataset with all accountting transactions by GL. Revenues, costs etc. I would like ot be able to create a P&L in pandas. Not only group GLs, but also have Gross Profit and EBITDA calculated as a row in the dataframe. can you do that?
@CodingIsFun
@CodingIsFun 2 года назад
Thanks for watching the video. Could you send over the example dataset to: contact@pythonandvba.com? I will look at your dataset, but I cannot make any promises. Thanks!
@swarpranalimusic6651
@swarpranalimusic6651 Год назад
What if email id is wrong or blank or attachment is missing
@CodingIsFun
@CodingIsFun Год назад
Thanks for watching. Please give it a try and see if you get an error message. I assume, you might need to implement a bit more error handling. Happy Coding!
@deepakl9978
@deepakl9978 4 месяца назад
How to add mail internal option in this code
@CodingIsFun
@CodingIsFun 4 месяца назад
Thank you very much for watching the video and your comment. Your requirements are well noted. Yet, I receive many requests for creating individual solutions. As much as I want to help, I simply do not find the time in my daily schedule to develop & test all the different requests. I hope you can understand. Thank you! Cheers, Sven ✌️
@janshrt
@janshrt 2 года назад
Is there also a way to do this on a Mac?
@CodingIsFun
@CodingIsFun 2 года назад
Thanks for watching the video & your question. To send out the emails via Outlook on a Mac, you could use the Python library "py-appscript": appscript.sourceforge.net/py-appscript/doc_3x/appscript-manual/03_quicktutorial.html Example on how to send emails with attachments: stackoverflow.com/a/62089384 I hope this helps!
@janshrt
@janshrt 2 года назад
Thank you, I tried this already with no luck, but I will have a go at it again.
Далее
How to Send Recurring Emails in Outlook
7:22
Просмотров 114 тыс.
4 Pandas Functions That I Wish I Knew Earlier
4:33
Просмотров 24 тыс.
Automate Boring Office Tasks with ChatGPT and Python
10:06
Introducing Python in Excel
19:01
Просмотров 1,5 млн