daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Mar 25th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. import numpy as np
  5. import pandas as pd
  6. import xlsxwriter
  7. from sqlalchemy import create_engine, event
  8. from urllib.parse import quote_plus
  9. from optparse import OptionParser
  10.  
  11. from sqlalchemy.engine import create_engine
  12. con =  "DRIVER={SQL Server Native Client 11.0};SERVER=127.0.0.1;DATABASE=GSOD;UID=GSOD;PWD=GSOD!Pass"
  13. Iteration = 3 # Used to increment a file number manually for testing
  14. xlsxoutput = "c:/data/pandas_simple_" + str(Iteration) + ".xlsx"
  15. quoted = quote_plus(con)
  16. new_con = 'mssql+pyodbc:///?odbc_connect={}'.format(quoted)
  17. #engine = create_engine(new_con)  
  18. Q = [["SELECT top 10 * from [DBX].[dbo].[MY_RAW_Order_Item_Stats_RAW]","Sheet_10_Items"],
  19.      ["SELECT top 5 * from [DBX].[dbo].[MY_RAW_Order_Item_Stats_RAW]","Sheet_5_Items"],
  20.      ["SELECT top 1 * from [DBX].[dbo].[MY_RAW_Order_Item_Stats_RAW]","Sheet_1_Items"]]
  21.  
  22. writer = pd.ExcelWriter(xlsxoutput, engine='xlsxwriter')
  23. # Create a Pandas Excel writer using XlsxWriter as the engine.
  24. for Query in Q:
  25.    # worksheet1 = workbook.add_worksheet()
  26.    # worksheet1.Name=Query[1]
  27.     df = pd.read_sql(Query[0],new_con)
  28.     # Convert the dataframe to an XlsxWriter Excel object.
  29.     df.to_excel(writer, sheet_name=Query[1])
  30.  
  31. # Close the Pandas Excel writer and output the Excel file.
  32. writer.save()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top