title: Extracting data in separate sheet excel using Python
body: <p>I want to write a python script from which I can execute multiple sql queries and the output of that query is saved in excel.
  Suppose I have 4 sql query i.e Script1, Script2, Script3 &amp; Script4 and I want to save the generated excel workbook in E:\Test, In that workbook sheet1 contains Script1 output, sheet2 contains Script2 output, sheet3 contains Script3 output, and so on. I have written a query but its working for only one script.
  By using this script I am able to generate excel sheet with Test name, but How I run the remaining script so that their output will show in other sheet of same workbook
  Please Help</p>
  
  <pre><code>import psycopg2
  import sys
  import pprint
  import pandas as pd
  import os
  import openpyxl.cell
  
  COMMASPACE = ', '
  def main():
      conn_string = "dbname='abc' user='qwerty' host='pqr' password='******' port='1234'"
  
      script1 = """
  select * From something1    
  """
  script2 = """
  select * From something2    
  """
  script3 = """
      select * From something3    
      """
  script4 = """
      select * From something4    
      """
      pprint.pprint ('Making connection to the Database...')    
      con1 = psycopg2.connect(conn_string)
      cur = con1.cursor()
      pprint.pprint ('Execution Start')
      cur.execute(script) 
      if not cur.rowcount:
          pprint.pprint ('Oops! Error Occured')
      else:
          columns = [desc[0] for desc in cur.description]
          data = cur.fetchall()
          df = pd.DataFrame(list(data), columns=columns)
          df.columns = map(str.upper, df.columns)
          writer = pd.ExcelWriter('E:\\Test.xlsx')
          df.to_excel(writer, sheet_name='Sheet1')
          def hide_column(ws, column_id):
              if isinstance(column_id, int):
                  assert column_id &gt;= 1, "Column numbers must be 1 or greater"
                  column_id = openpyxl.cell.get_column_letter(column_id)
              column_dimension = ws.column_dimensions[column_id]
              column_dimension.hidden = True
          writer.save()
      print ("END of extraction")    
  if __name__ == "__main__":
      main()
  </code></pre>