Guest User

Untitled

a guest
Mar 29th, 2018
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. # It is recommended that you extract your username, password and database variables from an external file rather than having them directly within your code else you maybe exposed
  2.  
  3. username = "#"
  4. password = "#"
  5. database = "database:port /servicename"
  6. directory = r'#:\...\file.xlsx'
  7.  
  8. SQL = """SELECT * FROM Table"""
  9.  
  10.  
  11. def SQL_Excel_Data_Generator(username, password, database, query, path, create=True, sheetname='Sheet 1',
  12. disclaimer=True):
  13. # Disclaimer to warn user not to use username, password and database connection within this code
  14. if disclaimer == True:
  15. print(
  16. 'When running queries please ensure that your username, password and databse connection is not visible within your python code when put into production. Use , disclaimer=False to disable this warning.')
  17. elif disclaimer == False:
  18. pass
  19.  
  20. # Import required packages
  21. import os
  22. import cx_Oracle
  23. import pandas as pd
  24. from openpyxl import load_workbook
  25. import time
  26.  
  27. # Connect to Oracle using required variables
  28. connection = cx_Oracle.connect(username, password, database)
  29.  
  30. # Read SQl query into pandas dataframe
  31. df = pd.read_sql_query(query, connection)
  32.  
  33. # Experimental code that aims to remove time from datetimes within Excel
  34. # for date in df.date:
  35. # df[date] = df[date].dt.floor('d')
  36.  
  37. # Create an Excel document from scratch if create parameter is True, removing the index and starting at row 0 (A1:##)
  38. if create == True:
  39. writer = pd.ExcelWriter(path)
  40. df.to_excel(writer, index=False, sheet_name=sheetname, startrow=0)
  41. writer.save()
  42.  
  43. # Sometimes Excel takes some time to update, wait 3 seconds to check if the file has been prepared
  44. time.sleep(3)
  45. if os.path.isfile(path) == True:
  46. pass
  47.  
  48. # If it isn't ready try waiting another 3 seconds and check again
  49. else:
  50. time.sleep(3)
  51. if os.path.isfile(path) == True:
  52. pass
  53.  
  54. # If Excel hasn't saved correctly raise the below value error
  55. else:
  56. raise ValueError('Cannot write to path specified')
  57.  
  58. # Update an existing Excel dashboard if create variable is False.
  59. elif create == False:
  60. if os.path.isfile(path) == True:
  61.  
  62. # Load the existing workbook from the path variable into the book variable
  63. book = load_workbook(path)
  64.  
  65. # Tell Pandas to use the ExcelWriter function to read from the path variable. Excel writer is needed to create a new workbook
  66. writer = pd.ExcelWriter(path, engine='openpyxl')
  67.  
  68. # Tell Pandas that the book attribute is equal to the book variable
  69. writer.book = book
  70.  
  71. # Tell Pandas to put the dataframe into an Excel instance
  72. df.to_excel(writer, index=False, sheet_name=sheetname, startrow=0)
  73.  
  74. # Now save the Excel instance
  75. writer.save()
  76.  
  77. # If it cant save then the file must not exist within the directory so raise a value error
  78. else:
  79. raise ValueError('File not found within directory when specifying the path')
  80.  
  81. # If create parameter has been entered incorrectly then raise value error
  82. else:
  83. raise ValueError('create parameter unknown')
  84.  
  85. # Close all open connections to Oracle when finished
  86. connection.close()
  87.  
  88.  
  89. print('Loading Data Into Excel')
  90. SQL_Excel_Data_Generator(username=username, password=password, database=database, create=True, query=SQL, path=directory, sheetname="my_sheet")
Add Comment
Please, Sign In to add comment