Python · Industrial Automation

Send Excel & PDF Reports by Email Using Python

Send Excel and PDF automation reports by email with Python. Build MIME attachments with the standard email package and send through an approved SMTP service.

Practical Python Automation Context Copy-Paste Examples Code Architecture

Learning Overview

Python Learning SeriesPython for Industrial AutomationPractical

Learning Goal

  • Send Excel and PDF automation reports by email with Python. Build MIME attachments with the standard email package and send through an approved SMTP service.
  • Run the examples in VS Code using the project environment prepared in earlier topics.
  • Understand where this topic fits in the complete SQL, reporting and Industry 4.0 workflow.
Quick answer

Send Excel and PDF automation reports by email with Python. Build MIME attachments with the standard email package and send through an approved SMTP service.

Architecture: Email Excel + PDF Reports

Code-Based Architecture

Email Excel + PDF Reports

1

SQL / Report

Create Excel or PDF

report.xlsx
2

Email Message

Set subject/from/to

EmailMessage
3

Attachment

Read file bytes

add_attachment
4

SMTP

Connect securely

SMTP_SSL / STARTTLS
5

Authenticate

Use approved account method

login / OAuth
6

Send

Deliver report

send_message
SQL / ReportEmail MessageAttachmentSMTPAuthenticateSend

1. Use Python Standard Library email tools

The email, smtplib and ssl modules are included with Python. Your organization may require an app password, OAuth or an internal SMTP relay.

2. Build an email message

from email.message import EmailMessage msg = EmailMessage()
msg["Subject"] = "Daily SQF Production Report"
msg["From"] = "reporting@example.com"
msg["To"] = "production@example.com"
msg.set_content("Please find the attached production report.")

3. Attach an Excel file

from pathlib import Path path = Path("SQF_Report.xlsx")
data = path.read_bytes()
msg.add_attachment( data, maintype="application", subtype="vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename=path.name,
)

4. Attach a PDF file

pdf = Path("SQF_Report.pdf")
msg.add_attachment( pdf.read_bytes(), maintype="application", subtype="pdf", filename=pdf.name,
)

5. Send through SMTP securely

import smtplib
import ssl context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.example.com", 465, context=context) as smtp: smtp.login("reporting@example.com", "APP_PASSWORD_OR_SECRET") smtp.send_message(msg)
Do not hard-code real passwords in production source code. Use your organization’s approved secret-management/authentication method.

6. Log the delivery result

import logging
logging.info("Report email sent successfully")

Frequently Asked Questions

Does Python include email support?

Yes. The Standard Library includes email message construction and SMTP client modules.

Should I hard-code an email password?

No. Use your organization’s approved secret or authentication method, such as environment variables, credential stores, app passwords or OAuth where required.

Can one email contain both Excel and PDF?

Yes. Add both files as separate MIME attachments before sending.

Reviewed by Bhawesh Kumar SinghIndustrial Automation Trainer and Industry 4.0 Consultant · Softwell Automation · 21+ years industry experience
Complete Python for Industrial Automation Learning Path

Use Previous / Next for sequential training, or open any topic directly.

Complete

Continue with the next topic in the Python for Industrial Automation learning path.

Open Learning Path
Python for Industrial Automation

Email Excel + PDF Reports

Send Excel and PDF automation reports by email with Python. Build MIME attachments with the standard email package and send through an approved SMTP service.

Content reviewed: 14 September 2026

☎ Call WhatsApp ✉ Email Enquire Now