Sending Data from Excel via Email
Using the openpyxl
library, you can read data from Excel files and process it to send emails or send emails to multiple recipients.
When sending emails to multiple recipients, you can use a loop to send each email.
In this practice, we will look at an example of reading sales data from an Excel file, converting it to an HTML table, and sending it via email.
Sending Sales Data from Excel via Email
The code in the practice screen uses the openpyxl
library to read sales data from an Excel file, convert it to an HTML table, and send it via email.
By multiplying the Unit Price
by the Quantity
of the data read from the Excel file, the sales for each product are calculated and reflected in the HTML table to be sent by email.
# Calculate sales amount and add as an HTML row
# [1:] means excluding the first row (header)
# Execute the for loop for each row except the header
for row in sales_data[1:]:
# Extract row data
product_name, unit_price, quantity = row
# Calculate sales amount
sales_amount = unit_price * quantity
html_table += f"""
<tr>
<td>{product_name}</td>
<td>{unit_price}</td>
<td>{quantity}</td>
<td>{sales_amount}</td>
</tr>
"""
Pay special attention to the related code and try executing the code in the code editor.
Additionally, by making good use of Python's loops
, openpyxl
, and smtplib
libraries, you can even create an automated program that reads multiple emails from an Excel file and sends sales data to multiple recipients.
Notes
In the practice program, sending emails to multiple recipients is restricted to prevent indiscriminate email dissemination.
If you want to send emails to multiple recipients, you can install the Python program on your computer and modify the code to meet your requirements with the help of AI.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.