title: how to execute sql script and save output in text file using python and sqlserver
body: <p>I have written this script in python which is reading a column from excel sheets and moving values into sqlserver table.</p>
  <pre><code>import pypyodbc as odbc
  import pandas as pd
  
  df = pd.read_excel(r'd:\automate\2021062.xlsx',sheet_name =None)
  mdf = pd.concat(df, axis=0, ignore_index=True)
  
  columns = ['AXIO']
  df_data = mdf[columns]
  records = df_data.values.tolist()
  
  DRIVER = 'SQL SERVER'
  SERVER_NAME='PKKHICS01L3012\SQL2K12'
  DATABASE_NAME='ATL'
  
  
  def connection_string(driver, server_name, database_name):
      conn_string = f&quot;&quot;&quot;
          DRIVER={{{driver}}};
          SERVER={server_name};
          DATABASE={database_name};
          Trust_Connection=yes;        
      &quot;&quot;&quot;
      return conn_string
  
  try:
      conn = odbc.connect(connection_string(DRIVER, SERVER_NAME, DATABASE_NAME))
  except odbc.DatabaseError as e:
      print('Database Error:')    
      print(str(e.value[1]))
  except odbc.Error as e:
      print('Connection Error:')
      print(str(e.value[1]))
  
  
  try:
      conn = odbc.connect(connection_string(DRIVER, SERVER_NAME, DATABASE_NAME))
  except odbc.DatabaseError as e:
      print('Database Error:')    
      print(str(e.value[1]))
  except odbc.Error as e:
      print('Connection Error:')
      print(str(e.value[1]))
  
  sql_insert = '''
      INSERT INTO atlnew 
      VALUES (?)
  '''
  
  try:
      cursor = conn.cursor()
      cursor.executemany(sql_insert, records)
      cursor.commit();    
  except Exception as e:
      cursor.rollback()
      print(str(e[1]))
  finally:
      print('Task is complete.')
      cursor.close()
      conn.close()
  </code></pre>
  <p>now 2nd part is when the TASK IS COMPLETED i want to run some sql script on the table where data has been moved from excel and one executed want to save the output as text file so need guidance here. I wanted to paste the script but its not letting me so please bare with me.</p>