Saturday, 16 August 2025

Schedule Python script using windows task Scheduler

 To run a Python program automatically using Windows Task Scheduler, follow these steps:

  1. Create a Python Script:
    Ensure you have a Python script (.py file) that you want to run. For example, example.py.

  2. Open Task Scheduler:

    • Press Win + R to open the Run dialog.
    • Type taskschd.msc and press Enter to open Task Scheduler.
  3. Create a Basic Task:

    • In the Task Scheduler window, click on Create Basic Task in the Actions pane.
    • Enter a name and description for the task, then click Next.
  4. Set the Trigger:

    • Choose when you want the task to start (e.g., Daily, Weekly, One time).
    • Set the specific details for the trigger (e.g., start date and time), then click Next.
  5. Set the Action:

    • Select Start a program and click Next.
  6. Specify the Program to Run:

    • In the Program/script field, enter the path to the Python executable. For example, C:\Python39\python.exe (adjust the path according to your Python installation).
    • Below is an example on how to find the path of python in command prompt


    • In the Add arguments (optional) field, enter the path to your Python script. For example, C:\path\to\your\script\example.py.
    • Optionally, in the Start in (optional) field, enter the directory where your script is located. This can help if your script relies on relative paths.
      • Eg:
        • Program/scriptC:\Python39\python.exe
        • Add arguments (optional)C:\Users\BKa\Desktop\Mail-Test.py
        • Start in (optional)C:\Users\BKa\Desktop


  7. Finish the Task:

    • Review the settings and click Finish.
  8. Optional Settings:

    • You can further customize the task by right-clicking on the task in the Task Scheduler Library and selecting Properties.
    • In the General tab, you can configure the task to run with highest privileges if needed.
    • In the Conditions and Settings tabs, you can adjust additional settings like stopping the task if it runs for too long, or running the task only if the computer is idle.

By following these steps, your Python script will run automatically according to the schedule you set in Windows Task Scheduler.

Tuesday, 12 August 2025

Sending Email from Python


 import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from datetime import datetime

# Convert DataFrame to HTML
html_table = df_errormessage.to_html(index=False)

# Custom statement
custom_statement = "<p>Please find the below error data:</p>"

# Combine statement and HTML table
email_body = f"{custom_statement}<br>{html_table}"

# Get current date
current_date = datetime.now().strftime("%Y-%m-%d")

smail = 'do.not.reply@company.com'
tmail = ['123@company.com', '456@company.com']
sub = f'Error Report - {current_date}'

def send_mail(send_from, send_to, subject, text, isTls=True):
    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = ','.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    # Attach the HTML table to the email body
    msg.attach(MIMEText(email_body, 'html'))

    smtp = smtplib.SMTP('outlook.company.com')
    if isTls:
        smtp.starttls()
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.quit()

# Send the email if the condition is met
send_mail(smail, tmail, sub, text)
print(df_errormessage)

Setting environmental variables using .env file in Python

 STEP-1 - Finding the Root path of your python code


import os
# Get the current working directory
cwd = os.getcwd()
print(cwd)

Output




STEP-2 -Creating Env files

  • After getting root path folder, create a file names .env file in that folder as shown below






STEP-3 -Entering credential details in .env File

  • you can enter all the secure details in env file as shown below.