Last active
August 29, 2015 14:03
-
-
Save okaram/bb3f279bfcb6fac0895b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"sheets": [ | |
{ | |
"title": "P&P", | |
"queries": [ | |
{ | |
"db_driver": "psycopg2", | |
"db_conn_str": "dbname=curri user=curri", | |
"title": "People", | |
"query": "SELECT * FROM Person" | |
}, | |
{ | |
"db_driver": "psycopg2", | |
"db_conn_str": "dbname=curri user=curri", | |
"title": "Products", | |
"query": "SELECT * FROM Products" | |
} | |
] | |
}, | |
{ | |
"title": "Rev", | |
"queries": [ | |
{ | |
"db_driver": "psycopg2", | |
"db_conn_str": "dbname=curri user=curri", | |
"title": "People", | |
"query": "SELECT * FROM Person ORDER BY Id DESC" | |
}, | |
{ | |
"db_driver": "psycopg2", | |
"db_conn_str": "dbname=curri user=curri", | |
"title": "Products", | |
"query": "SELECT * FROM Products" | |
} | |
] | |
} | |
] | |
}c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import json | |
from openpyxl import Workbook | |
#from openpyxl.cell import get_column_letter | |
from openpyxl.styles import Color, Style, PatternFill | |
headerStyle=Style(fill=PatternFill(patternType='solid', fgColor=Color('008080FF'))) | |
def query2xl(query, sheet, firstRow, headerStyle): | |
driver=__import__(query['db_driver']) | |
db=driver.connect(query['db_conn_str']) | |
cur=db.cursor() | |
cur.execute(query['query']) | |
theRow=firstRow+1; | |
sheet.cell(row=theRow,column=1).value=query['title'] | |
#theRow=theRow+1 | |
for f,field in enumerate(cur.description): | |
sheet.cell(row=theRow,column=f+2).value=field.name | |
sheet.cell(row=theRow,column=f+2).style=headerStyle | |
for r,row in enumerate(cur.fetchall()): | |
theRow=theRow+1 | |
for c,field in enumerate(row): | |
sheet.cell(row=theRow,column=c+2).value=field | |
cur.close() | |
db.close() | |
return theRow+2; | |
def addSheetToWorkbook(sheet, wkBook): | |
theRow=0 | |
ws=wkBook.create_sheet() | |
ws.title=sheet['title'] | |
for q,query in enumerate(sheet['queries']): | |
theRow=query2xl(query, ws,theRow,headerStyle) | |
def mkWorkBook(sheets): | |
wb = Workbook() | |
for s,sheet in enumerate(sheets): | |
addSheetToWorkbook(sheet,wb) | |
return wb | |
if __name__ == '__main__': | |
spec=json.loads(open(sys.argv[1],'r').read()) | |
wb=mkWorkBook(spec['sheets']) | |
wb.save(filename = sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment