Guest User

Untitled

a guest
Oct 16th, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. #!/usr/bin/python
  2. import smtplib
  3. import base64
  4. import os
  5. import sys
  6. import xlswriter
  7. import xlwt
  8. import datetime
  9. import MySQLdb
  10. from pyh import *
  11. from email.MIMEMultipart import MIMEMultipart
  12. from email.MIMEText import MIMEText
  13. db = MySQLdb.connect("192.168.1.118","stp","stp","STP")
  14. cursor = db.cursor()
  15. query = ("""select * from stp_automation_output""")
  16. cursor.execute(query)
  17. myresults = cursor.fetchall()
  18. workbook = xlwt.Workbook()
  19. worksheet = workbook.add_sheet("My Sheet")
  20. #date_format = workbook.add_format({'num_format': 'd mmmm yyyy'})
  21. bold = workbook.add_format({'bold': 1})
  22. worksheet.write('A1','Sno',bold)
  23. worksheet.write('B1','function_name',bold)
  24. worksheet.write('C1','input1',bold)
  25. worksheet.write('D1','input2',bold)
  26. worksheet.write('E1','input3',bold)
  27. worksheet.write('F1','Expected_output',bold)
  28. worksheet.write('G1','Actual_output',bold)
  29. worksheet.write('H1','Result',bold)
  30. row = 1
  31. col = 0
  32. for Sno,function_name,input1,input2,input3,Expected_output,Actual_output,Result in (myresults):
  33. Sno = row[0]
  34. function_name = row[1]
  35. input1 = row[2]
  36. input2 = row[3]
  37. input3 = row[4]
  38. Expected_output = row[5]
  39. Actual_output = row[6]
  40. Result = row[7]
  41. workbook.save()
  42.  
  43. import MySQLdb
  44. import csv
  45.  
  46. user = '' # your username
  47. passwd = '' # your password
  48. host = '' # your host
  49. db = '' # database where your table is stored
  50. table = '' # table you want to save
  51.  
  52. con = MySQLdb.connect(user=user, passwd=passwd, host=host, db=db)
  53. cursor = con.cursor()
  54.  
  55. query = "SELECT * FROM %s;" % table
  56. cursor.execute(query)
  57.  
  58. with open('outfile','w') as f:
  59. writer = csv.writer(f)
  60. for row in cursor.fetchall():
  61. writer.writerow(row)
  62.  
  63. import MySQLdb
  64. from xlsxwriter.workbook import Workbook
  65.  
  66. user = '' # your username
  67. passwd = '' # your password
  68. host = '' # your host
  69. db = '' # database where your table is stored
  70. table = '' # table you want to save
  71.  
  72. con = MySQLdb.connect(user=user, passwd=passwd, host=host, db=db)
  73. cursor = con.cursor()
  74.  
  75. query = "SELECT * FROM %s;" % table
  76. cursor.execute(query)
  77.  
  78. workbook = Workbook('outfile.xlsx')
  79. sheet = workbook.add_worksheet()
  80. for r, row in enumerate(cursor.fetchall()):
  81. for c, col in enumerate(row):
  82. sheet.write(r, c, col)
  83.  
  84. import mysql.connector
  85. from openpyxl import Workbook
  86.  
  87. def main():
  88.  
  89. # Connect to DB -----------------------------------------------------------
  90. db = mysql.connector.connect( user='root', password='', host='127.0.0.1')
  91. cur = db.cursor()
  92.  
  93. # Create table ------------------------------------------------------------
  94. database = 'test_database'
  95. SQL = 'CREATE DATABASE IF NOT EXISTS ' + database + ';'
  96. cur.execute(SQL)
  97. db.commit()
  98.  
  99. SQL = 'USE ' + database + ';'
  100. cur.execute(SQL)
  101.  
  102. # Create car data ---------------------------------------------------------
  103. cars_table_name = 'cars'
  104. SQL = (
  105. 'CREATE TABLE IF NOT EXISTS ' + cars_table_name +
  106. '('
  107. ' model_year YEAR, '
  108. ' manufacturer VARCHAR(40), '
  109. ' product VARCHAR(40)'
  110. ');')
  111. cur.execute(SQL)
  112. db.commit()
  113.  
  114. # Python list of dictionaries
  115. # More info at:
  116. # https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search
  117. car_data = [
  118. { 'model_year': '2010', 'manufacturer': 'Toyota', 'product': 'Prius' },
  119. { 'model_year': '2010', 'manufacturer': 'Honda', 'product': 'CR-V' },
  120. { 'model_year': '1998', 'manufacturer': 'Honda', 'product': 'Civic' },
  121. { 'model_year': '1997', 'manufacturer': 'Ford', 'product': 'F-150' },
  122. { 'model_year': '2017', 'manufacturer': 'Tesla', 'product': 'Model 3' },
  123. ]
  124.  
  125. # Code adapted from:
  126. # https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-transaction.html
  127. add_cars = ('INSERT INTO ' + cars_table_name + ' (model_year, manufacturer, product) '
  128. ' VALUES (%(model_year)s, %(manufacturer)s, %(product)s)')
  129.  
  130. for car_datum in car_data:
  131. cur.execute(add_cars, car_datum);
  132. db.commit()
  133.  
  134. # Create manufacturer data -----------------------------------------------
  135. manufacturer_table_name = 'manufacturer'
  136. SQL = (
  137. 'CREATE TABLE IF NOT EXISTS ' + manufacturer_table_name +
  138. '('
  139. ' name VARCHAR(40), '
  140. ' headquarters VARCHAR(40), '
  141. ' number_of_employees INT, '
  142. ' website VARCHAR(40)'
  143. ');')
  144. cur.execute(SQL)
  145. db.commit()
  146.  
  147. add_manufacturer = (
  148. 'INSERT INTO ' + manufacturer_table_name +
  149. ' (name, headquarters, number_of_employees, website) '
  150. ' VALUES (%s, %s, %s, %s)')
  151.  
  152. # Python list of lists
  153. # More info at:
  154. # https://stackoverflow.com/questions/18449360/access-item-in-a-list-of-lists
  155. # Data from:
  156. # https://en.wikipedia.org/wiki/Toyota
  157. # Honda data from: https://en.wikipedia.org/wiki/Honda
  158. # Ford data from: https://en.wikipedia.org/wiki/Ford
  159. # Tesla data from: https://en.wikipedia.org/wiki/Tesla,_Inc.
  160. manufacture_data = [
  161. [ 'Toyota', 'Toyota, Aichi, Japan', '364445', 'http://toyota-global.com/' ],
  162. [ 'Honda', 'Minato, Tokyo, Japan', '208399', 'http://world.honda.com/' ],
  163. [ 'Ford', 'Dearborn, Michigan, U.S.', '201000', 'http://www.ford.com/' ],
  164. [ 'Tesla, Inc.', 'Palo Alto, California, US', '33000', 'http://www.tesla.com/' ],
  165. ]
  166.  
  167. for manufacturer_datum in manufacture_data:
  168. cur.execute(add_manufacturer, manufacturer_datum);
  169. db.commit()
  170.  
  171. # Create Excel (.xlsx) file -----------------------------------------------
  172. wb = Workbook()
  173.  
  174. SQL = 'SELECT * from '+ cars_table_name + ';'
  175. cur.execute(SQL)
  176. results = cur.fetchall()
  177. ws = wb.create_sheet(0)
  178. ws.title = cars_table_name
  179. ws.append(cur.column_names)
  180. for row in results:
  181. ws.append(row)
  182.  
  183. SQL = 'SELECT * from '+ manufacturer_table_name + ';'
  184. cur.execute(SQL)
  185. results = cur.fetchall()
  186. ws = wb.create_sheet(0)
  187. ws.title = manufacturer_table_name
  188. ws.append(cur.column_names)
  189. for row in results:
  190. ws.append(row)
  191.  
  192. workbook_name = "test_workbook"
  193. wb.save(workbook_name + ".xlsx")
  194.  
  195. # Remove tables and database ----------------------------------------------
  196. SQL = 'DROP TABLE ' + manufacturer_table_name + ';'
  197. cur.execute(SQL)
  198. db.commit()
  199. SQL = 'DROP TABLE ' + cars_table_name + ';'
  200. cur.execute(SQL)
  201. db.commit()
  202. SQL = 'DROP DATABASE ' + database + ';'
  203. cur.execute(SQL)
  204. db.commit()
  205.  
  206. if __name__ =='__main__':main()
Add Comment
Please, Sign In to add comment