Skip to content

Instantly share code, notes, and snippets.

@git2samus
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save git2samus/a21088ebf582aeaa9a30 to your computer and use it in GitHub Desktop.

Select an option

Save git2samus/a21088ebf582aeaa9a30 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import csv, sys
from xlrd import open_workbook, XL_CELL_TEXT
#from PyQt4 import QtCore, QtGui, uic
def extract_xls(startCell, endCell, tank, term, maxH, unit, input, output):
startCol = startCell[:1]
startRow = startCell[1:]
endCol = endCell[:1]
endRow = endCell[1:]
##Convert raw values to XLRD values##
colConv = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
colBegin = colConv.index(startCol)
rowBegin = int(startRow) - 1
colEnd = colConv.index(endCol)
rowEnd = int(endRow)
##Open *.xls file and assign sheet##
book = open_workbook(input)
sheet = book.sheet_by_index(0)
##Populate array with volumes from *.xls##
rawVol = []
if unit == 'G':
while colBegin <= colEnd:
test = sheet.col_values(colBegin, rowBegin, rowEnd)
for i in test:
if i:
i = round(i)
rawVol.append(int(i))
colBegin += 2
else:
while colBegin <= colEnd:
test = sheet.col_values(colBegin, rowBegin, rowEnd)
for i in test:
if i:
i = ('%.2f' %i)
i = str(i).replace(".","")
rawVol.append(i)
colBegin += 2
padVol = []
for i in rawVol:
if i:
padVol.append(str(i).zfill(9))
feet = maxH[:2]
ftHt = int(feet)+1
tail = [unit, 'N', '']
empty = ['']
with open(output, 'wb') as outFile:
writeFile = csv.writer(outFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
##Generate empty array for sorted volumes and strapping table##
sortVol = [[0]*12]*ftHt
##Insert sorted volumes into 2D array and pad as needed##
ft = 0
for i in sortVol:
if i:
i = padVol[0:12]
del padVol[0:12]
if len(i) < 12:
diff = 12 - len(i)
if diff > 0:
i.extend(empty*diff)
elif diff == 0:
i.extend(empty*12)
i.insert(0,maxH)
i.insert(0, str(ft).zfill(3))
i.insert(0, tank.zfill(4))
i.insert(0, term.zfill(7))
i.extend(tail)
writeFile.writerow(i)
ft += 1
if __name__ == '__main__':
startCell = raw_input('Start Cell: ')
endCell = raw_input('End Cell: ')
tank = raw_input('tank #: ')
term = raw_input('Terminal ID: ')
maxH = raw_input('Max Height: ')
unit = raw_input('Unit of Measure: ')
input = raw_input('Input file: ') #QtGui.QFileDialog.getOpenFileName(None, 'Open File')
output = raw_input('Output file: ') #QtGui.QFileDialog.getSaveFileName(None, 'Save File')
extract_xls(startCell, endCell, tank, term, maxH, unit, input, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment