Auto Export Data into Excel from SQL using Python Pyodbc | Python SQL Automation |Task Scheduler #16
41.7 هزار بار بازدید -
3 سال پیش
-
In this video you will
In this video you will learn or explore something new related to Python and SQL Server Automation.
Auto Export Data into Excel from SQL using Python Pyodbc | Python SQL Automation | Task Scheduler
First of all we will establish the connection between Python and SQL Server using pyodbc (To create the SQL connection is very easy as compare to other programming language). After that we will read the data from SQL and store in Pandas DataFrame and very interest part is we will display the notification status on user's screen and that notification you can customize based on your need.
And at the last, we will schedule the script using Task Scheduler to run on the specific date and time. So Let's go!!!
Python SQL Server Script:
------------------------------------------------------------------------------------------------------------------
import pyodbc
import pandas as pd
import os
from datetime import datetime
from plyer import notification
create SQL connection
connection = pyodbc.connect(driver = '{ODBC Driver 17 for SQL Server}',
host = 'DESKTOP-NAKP5E5',
database = "Test",
trusted_connection = 'yes')
SQL Command to read the data
sqlQuery = "select * from dbo.SalesOrder where Region = 'South'"
Getting the data from sql into pandas dataframe
df = pd.read_sql(sql = sqlQuery, con = connection)
Export the data on the Desktop
df.to_csv(os.environ["userprofile"] + "\\Desktop\\PythonScript\\" + "SQL_OrderData_" +
datetime.now().strftime("%d-%b-%Y %H%M%S")
+ ".csv", index = False)
Display Notifiction to User
notification.notify(title="Report Status!!!",
message=f"Sales data has been successfully saved into Excel.\
\nTotal Rows: {df.shape[0]}\nTotal Columns: {df.shape[1]}",
timeout = 10)
-------------------------------------------------------------------------------------------------------------------
Last Video:
Python Excel Automation: Excel Automation Using Python
Python Teaser: A Beautiful Python Programming Teaser...
Python Pandas Tutorial: Python Pandas Tutorial | What is Pand...
Python Playlist: Python Tutorial for Beginners
Python Data Structure Playlist: Python Data Structure
Python OOPs Playlist: Object Oriented Programming Tutorials...
Auto Export Data into Excel from SQL using Python Pyodbc | Python SQL Automation | Task Scheduler
First of all we will establish the connection between Python and SQL Server using pyodbc (To create the SQL connection is very easy as compare to other programming language). After that we will read the data from SQL and store in Pandas DataFrame and very interest part is we will display the notification status on user's screen and that notification you can customize based on your need.
And at the last, we will schedule the script using Task Scheduler to run on the specific date and time. So Let's go!!!
Python SQL Server Script:
------------------------------------------------------------------------------------------------------------------
import pyodbc
import pandas as pd
import os
from datetime import datetime
from plyer import notification
create SQL connection
connection = pyodbc.connect(driver = '{ODBC Driver 17 for SQL Server}',
host = 'DESKTOP-NAKP5E5',
database = "Test",
trusted_connection = 'yes')
SQL Command to read the data
sqlQuery = "select * from dbo.SalesOrder where Region = 'South'"
Getting the data from sql into pandas dataframe
df = pd.read_sql(sql = sqlQuery, con = connection)
Export the data on the Desktop
df.to_csv(os.environ["userprofile"] + "\\Desktop\\PythonScript\\" + "SQL_OrderData_" +
datetime.now().strftime("%d-%b-%Y %H%M%S")
+ ".csv", index = False)
Display Notifiction to User
notification.notify(title="Report Status!!!",
message=f"Sales data has been successfully saved into Excel.\
\nTotal Rows: {df.shape[0]}\nTotal Columns: {df.shape[1]}",
timeout = 10)
-------------------------------------------------------------------------------------------------------------------
Last Video:
Python Excel Automation: Excel Automation Using Python
Python Teaser: A Beautiful Python Programming Teaser...
Python Pandas Tutorial: Python Pandas Tutorial | What is Pand...
Python Playlist: Python Tutorial for Beginners
Python Data Structure Playlist: Python Data Structure
Python OOPs Playlist: Object Oriented Programming Tutorials...
3 سال پیش
در تاریخ 1400/05/02 منتشر شده
است.
41,701
بـار بازدید شده