Auto Export Data into Excel from SQL using Python Pyodbc | Python SQL Automation |Task Scheduler #16

Sdílet
Vložit
  • čas přidán 4. 09. 2024
  • 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.\

    Total Rows: {df.shape[0]}
    Total Columns: {df.shape[1]}",
    timeout = 10)
    -------------------------------------------------------------------------------------------------------------------
    Last Video:
    Python Excel Automation: • Excel Automation Using...
    Python Teaser: • A Beautiful Python Pro...
    Python Pandas Tutorial: • Python Pandas Tutorial...
    Python Playlist: • Python Tutorial for Be...
    Python Data Structure Playlist: • Python Data Structure
    Python OOPs Playlist: • Object Oriented Progra...

Komentáře • 76

  • @raymondjurado9203
    @raymondjurado9203 Před 2 lety +9

    This is a very good tutorial for getting pandas and SQL together. The batch file, notifications, and the task scheduling are a great bonus that you've given us!

  • @arunkumarkesavan6077
    @arunkumarkesavan6077 Před 2 měsíci +1

    Crystal clear bro, please post many videos 🎉

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 2 měsíci

      Sure, tell me what you are looking.

    • @arunkumarkesavan6077
      @arunkumarkesavan6077 Před měsícem

      @@ProgrammingIsFunn , Can you also post a video like downloading the files from the sharepoint, data cleansing and uploading the output files into the sharepoint again !!!

  • @qasimawan3569
    @qasimawan3569 Před rokem +1

    I have been searching something like this for a week now and I've finally come across it. Many, many thanks for this tutorial. Your efforts for this knowledge is truly appreciated. Thanks again, I subbed!

  • @mukeshmishra3026
    @mukeshmishra3026 Před 2 lety +3

    the wonderful session, Thx friend for making so clear short & explanation for server to excel data

  • @rajkumarrajan8059
    @rajkumarrajan8059 Před 11 měsíci +1

    Simple and Quick.
    Excellent

  • @rohitmahato542
    @rohitmahato542 Před 2 lety +2

    thank you so much...you are sharing your great knowledge for free....video was very clear....and helpful too

  • @gemyforson8710
    @gemyforson8710 Před 3 lety +1

    thank you so much for the great sharing knowledge. It is very useful.

  • @sondrad1839
    @sondrad1839 Před 2 lety

    Best video I’ve seen on this subject for us newbies. Thank you ! Now I need to know how to send parameters to call several sql server views and export to 1 excel file different sheets for a particular set of data….eg: export customer info, order info, manufacturing info for a particular order number.

  • @gcmahajan1
    @gcmahajan1 Před 2 měsíci +1

    Great..

  • @rajmathichithramani5864
    @rajmathichithramani5864 Před měsícem

    Really helpful and informative..Thanks a ton...This is the one which i was searched a lots in CZcams....How can i connect with you if i have doubts in Python ?

  • @jobdossantosleite1160
    @jobdossantosleite1160 Před 2 lety

    Very Good my friend

  • @AnuragSingh-vv3qv
    @AnuragSingh-vv3qv Před 2 lety

    Thanks very very informative!

  • @dss1871
    @dss1871 Před 3 lety +1

    Great 👍👍👍👍

  • @vj_singh
    @vj_singh Před 3 lety +1

    Nice

  • @vishalsharda7508
    @vishalsharda7508 Před 2 lety +1

    Thanks a lot bro. 👍👍 Subscribed Ur channel for more solutions

  • @tarak9816
    @tarak9816 Před 2 lety +1

    exporting data to XL sheet or SQL and MYSQL to which format it is easy to convert?

  • @Sanvibhagat937
    @Sanvibhagat937 Před rokem

    @programmingisfun Please make a video on How to count rows of multiple tables in pgadmin server of different database through python code because it takes lot of time to count one by one on daily basis. Is there any shortcut code by using python. Please help me in this.

  • @kofimensah6566
    @kofimensah6566 Před 2 lety

    Great video!!! Are you able to do a tutorial that shows how to link the cron job to a slack channel i.e. schedule runs and post the excel file to a slack bot?

  • @OliverShey
    @OliverShey Před 3 lety

    Very useful

  • @biradarpraveen7643
    @biradarpraveen7643 Před 2 lety +1

    You are the best. Can you please tell me how to send an email to the user (via outlook) with the excel attachment instead of creating desktop notification

    • @keeplearning9897
      @keeplearning9897 Před rokem

      This is very easy using Python to send an email.

    • @moviesworld764
      @moviesworld764 Před 11 měsíci

      Hi Bro.
      Do you know the above process.
      I am also need it.

  • @keeplearning9897
    @keeplearning9897 Před rokem

    🙏

  • @SyedMohammedOmerFarooq

    Very good tutorial
    Can you please made tutorial in which instead of desktop notification an email is sent to the user.

  • @AbdulRahmanMohammed-cc6xv
    @AbdulRahmanMohammed-cc6xv Před 2 měsíci +1

    After reading csv file in jupyter notebook. How to connect with sql server and upload data into database table.

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 2 měsíci

      czcams.com/video/eEVG-A4R9WU/video.htmlsi=pJrF2J5bdY5xH_FQ

  • @MohammedGS-qm7ru
    @MohammedGS-qm7ru Před 2 lety

    Nice and helpful, your efforts well appreciated. Is it possible to automate the database to an online storage or folder? I will really appreciate your tips on this. Many thanks 🙏

  • @sunilkumarchavan925
    @sunilkumarchavan925 Před rokem

    I would thanks for the so much important video you have created for us . My query is below if you answer this for me is very helpful .
    I have jump host server to connect/login database server from my local machine and want to supply run time value like Database server IP and database name. And in sql query need to pass 1 value like account ID while runtime as well ..?

    • @AbhisheakSaraswat
      @AbhisheakSaraswat Před rokem

      You can integrate excel on runtime to get the input

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před rokem

      Hi, you can take input from user at run time, using Python input box or you can integrate excel for input.

    • @sunilkumarchavan925
      @sunilkumarchavan925 Před rokem

      @@ProgrammingIsFunn If you have Any sample code or any youtube video link that would be the great

  • @xst-k6
    @xst-k6 Před rokem

    Notification showed 1620 rows when there were 1621. Remember to display df.shape[0] after incrementing by 1 as it starts from 0.

  • @AnujVerma-kp3md
    @AnujVerma-kp3md Před 9 měsíci

    hie @ProgrammingIsFunn I have code which read data from excel and create a table in ms sql , so in that case how to keep check on duplicate entries in ms sql .Please help

  • @rashmijain302
    @rashmijain302 Před 2 lety

    Thanks for the video. Can we take real time data from excel to sql using python? Also, can we use Apache druid for near to real time data?

  • @maria-ut6xm
    @maria-ut6xm Před 6 měsíci

    I get "Invalid Port number" error.. would you know how to fix it? I am using Idera Rapid SQL

  • @ADITYAKUMAR-gt5vl
    @ADITYAKUMAR-gt5vl Před rokem

    i was trying to connect using your code , but getting error UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.
    df = pd.read_sql(sql=sqlQuery, con=connection) ?

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před rokem

      Ignore that warning. To ignore any warning you can use below script.
      import warnings
      warnings.filterwarning(‘ignore’)
      In future if we will get any new release might be we have to change our approach using sqlAlchemy

  • @vladimir7759
    @vladimir7759 Před 2 lety +1

    Hi, please tell me how to change the encoding to cp1251, when I open a file in excel I have unknown characters.

  • @kiranpatil4968
    @kiranpatil4968 Před rokem

    Please create etl testing automation framework using pytest

  • @isshikalia
    @isshikalia Před 2 lety +1

    can we add more rows into an existing excel file?

  • @kavinkumarloganathan3951

    Please let me know whether the task will also run when the system is in off or it will run only when system is in on

  • @sathyamoorthy2308
    @sathyamoorthy2308 Před 3 lety +1

    How can we write a sql query output to a different sheet in a same excel ?

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 3 lety +1

      Very simple 😊.
      import pandas as pd
      writer = pd.ExcelWriter("YourWorkbookName.xlsx", engine = 'xlsxwriter')
      # here, you can store your query's result data
      df = pd.read_sql(sql = sqlQuery, con = connection)
      df.to_excel(writer, sheet_name = 'HereYourSheetName')
      writer.save()
      writer.close()
      and for more info watch this tutorial.....
      czcams.com/video/P8dpG5arKY0/video.html

  • @karthikb.s.k.4486
    @karthikb.s.k.4486 Před 3 lety

    What is the laptop configuration are you usinh for windows it looks fast .what is the theme for vs code used.Nice session

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 3 lety

      Karthik, I am using T490 Lenovo thinkpad laptop with I7 and SSD.

  • @vishwarajk3
    @vishwarajk3 Před 7 měsíci

    I have a sql query which i need to run daily .
    Sql Query= SELECT * FROM be_dw where date_key ='1-02-2024.' But i need to take the date_key automatically as per today's date instead of manually entering and changing the date. How can i do that ?

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 7 měsíci

      You can take the input at run time from the user, or you can create a mapping file where use can set the input data before executing the script.

    • @vishwarajk3
      @vishwarajk3 Před 7 měsíci

      @@ProgrammingIsFunn Since i working on automating the process,so input from the user won't help. Mapping the file to get the date, I didn't get that ?
      Also,isn't there any SQL function or logic to get the current date?
      Edit : I am a beginner. 🙂

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 7 měsíci

      @@vishwarajk3 To get the current date and time, you can use getdate() function of sql.
      # Get the current date and time
      select getdate() -- It will give you date and time
      # only for date
      select format(getdate(), 'dd/mmm/yyyy')

  • @user-tk5tj5rn9d
    @user-tk5tj5rn9d Před 10 měsíci +1

    can we use this in xampp file?

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 10 měsíci

      Yeah you can try the same, but we never ever try this in xampp.

  • @asvlogs2288
    @asvlogs2288 Před 2 lety

    To automate through task scheduler, do we need to keep our system and python on all the time ?

  • @ee6049
    @ee6049 Před 2 lety

    how to create python file sir......\\desktop\\python..... something

  • @nareshchoudhary9774
    @nareshchoudhary9774 Před 2 lety

    Can we do the inverse of it ?
    I mean can we import multiple Excel files from local disc to sql server using python ??
    Is it possible?

    • @hanumishra5590
      @hanumishra5590 Před 2 měsíci

      Yesss ...
      U can update, insert, delete,alter the table using python

  • @dipaklanjewar4556
    @dipaklanjewar4556 Před 2 lety

    How to compare two tables

  • @netnitin
    @netnitin Před 10 měsíci

    How to do it for Mysql using Python?

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 10 měsíci

      Using mysql.connector we can establish the connection between python and mysql

  • @kirubababu7127
    @kirubababu7127 Před 2 lety +1

    Can't connect

  • @shubhangisharma6202
    @shubhangisharma6202 Před 2 lety +1

    What is userprofile

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 2 lety +1

      userprofile means: c:\users\SystemName
      try to run the below code for better understanding.
      import os
      print(os.environ["userprofile"])

  • @chinmaymule7315
    @chinmaymule7315 Před 3 lety

    Why its not working for MYSQL??

    • @ProgrammingIsFunn
      @ProgrammingIsFunn  Před 3 lety

      For mysql you have to import mysql.connector library and in this video you will find only for sql server.

  • @nicolaconte3887
    @nicolaconte3887 Před 3 lety

    And guys

  • @endowbonapen7028
    @endowbonapen7028 Před 4 měsíci

    ('28000', '[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user \'DESKTOP-76JM819\\endow\'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "Coba" requested by the login. The login failed. (4060); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user \'DESKTOP-76JM819\\endow\'. (18456); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "Coba" requested by the login. The login failed. (4060)') i have problem, can anyone explaine?