{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Fill Excel Spreadsheet\n", "Uses values in the WaterBalanceData.csv file to populate values in the USWaterBalanceSheet.xlsx file\n", "\n", "Requires the openyxl module: https://openpyxl.readthedocs.io/en/default/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Import modules (including xlwt: http://xlwt.readthedocs.io/en/latest/)\n", "import sys, os\n", "import pandas as pd\n", "from openpyxl import load_workbook" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Get the file names\n", "dataDir = '../../Data'\n", "inDataFN = dataDir + os.sep + 'WaterBalanceData.csv'\n", "inXlsxFN = dataDir + os.sep + 'USWaterBalanceSheet.xlsx'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Load the data into a pandas dataframe\n", "dfData = pd.read_csv(inDataFN)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Row and column indices\n", "Aq = 2\n", "Do = 3\n", "In = 4\n", "Ic = 5\n", "Ig = 6\n", "Li = 7\n", "Mi = 8\n", "PS = 9\n", "TC = 10\n", "TR = 11\n", "Supply = 12\n", "Sf = 21 #Surface\n", "Gw = 22 #Groundwater" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Create the dictionary of GroupNames and cell locations\n", "celLocs = {'Aquaculture_Surface':(Sf,Aq),\n", " 'Aquaculture_Groundwater':(Gw,Aq),\n", " 'Domestic_Surface':(Sf,Do),\n", " 'Domestic_Groundwater':(Gw,Do),\n", " 'Industrial_Surface':(Sf,In),\n", " 'Industrial_Groundwater':(Gw,In),\n", " 'Irrigation_Crop_Surface':(Sf,Ic),\n", " 'Irrigation_Crop_Groundwater':(Gw,Ic),\n", " 'Irrigation_Golf_Surface':(Sf,Ig),\n", " 'Irrigation_Golf_Groundwater':(Gw,Ig),\n", " 'Livestock_Surface':(Sf,Li),\n", " 'Livestock_Groundwater':(Gw,Li),\n", " 'Mining_Surface':(Sf,Mi),\n", " 'Mining_Groundwater':(Gw,Mi),\n", " 'PublicSupply_Surface':(Sf,PS),\n", " 'PublicSupply_Groundwater':(Gw,PS),\n", " 'ThermoElec_OnceThru_Surface':(Sf,TC),\n", " 'ThermoElec_OnceThru_Groundwater':(Gw,TC),\n", " 'ThermoElec_Recirc_Surface':(Sf,TR),\n", " 'ThermoElec_Recirc_Groundwater':(Gw,TR),\n", " 'Supply':(4,12)\n", " } " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Create the workbook object\n", "wb = load_workbook(filename = inXlsxFN)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for year in (2000,2005, 2010):\n", " #Get the year worksheet in the workbook\n", " ws = wb.get_sheet_by_name(str(year))\n", " \n", " #Label the sheet\n", " ws.cell(column=1,row=1,value=\"US Water Balance: {}. Values in MGal/Year\".format(year))\n", " \n", " #use the dictionary to insert values\n", " for name, cellLoc in celLocs.items():\n", " #Get the value for selected year\n", " val = dfData[(dfData.Group == name) & (dfData.YEAR == year)]['MGal'].iloc[0]\n", " #insert it into the Excel file\n", " ws.cell(column = cellLoc[1],row = cellLoc[0],value = val)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "wb.save(dataDir+os.sep+'BalanceSheet.xlsx')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 2 }