Skip to content

Instantly share code, notes, and snippets.

@giorgi-ghviniashvili
Created April 20, 2018 15:11
Show Gist options
  • Save giorgi-ghviniashvili/6a2cb6bf424b01ec0f91fb4e78d206dc to your computer and use it in GitHub Desktop.
Save giorgi-ghviniashvili/6a2cb6bf424b01ec0f91fb4e78d206dc to your computer and use it in GitHub Desktop.
reads excel files using docx
from openpyxl import load_workbook
from os import listdir
from os.path import isfile, join
from purchaseModel import Purchase
from investmentModel import Investment
from docx import Document
from shutil import copyfile
from docx.enum.dml import MSO_THEME_COLOR
from docx.shared import RGBColor
import datetime
INPUT_PATH = 'input/'
OUTPUT_PAHT_DOC = 'output/doc/'
def round_numbers(number):
try:
return '£' + format(int(round(number)), ",")
except Exception as e:
return 'NA'
def round_percent(number):
try:
return str(round(number*100,2)) + '%'
except Exception as e:
return 'NA'
def getAllFileNames():
onlyfiles = [f for f in listdir(INPUT_PATH) if f[0] != '~' and isfile(join(INPUT_PATH, f))] # exclude files that start with ~
return onlyfiles
class Reader(object):
"""docstring for Read"""
def __init__(self):
super(Reader, self).__init__()
def readSheet(self, sheet, fileName):
rows = list(sheet.rows)
name = rows[0][1].value
prospectiveEarning = rows[20][6].value
numberOfProperties = rows[12][9].value
sheetDataEntry = {
'fileName': fileName,
'purchases': [],
'investments': [],
'personName': name,
'numberOfProperties': numberOfProperties,
'prospectiveEarning': prospectiveEarning,
}
for i in range(4,10):
row = rows[i]
if row[0].value is not None:
p = Purchase({
'purchaseName': row[0].value,
'purchasePrice': round_numbers(row[1].value),
'purchaseDate': row[2].value.strftime('%d/%m/%Y') if row[0].value != 'Totals' and type(row[2].value) == datetime.datetime else "NA",
'monthsOwned': str(row[3].value) if str(row[3].value) != '#VALUE!' else 'NA',
'firstRent': round_numbers(row[4].value),
'currentRent': round_numbers(row[5].value),
'currentValue': round_numbers(row[6].value),
'averageRent': round_numbers(row[7].value),
'capitalGain': round_numbers(row[8].value),
'monthlyCapitalGains': round_numbers(row[9].value),
'monthlyNetRetBeforeInterest': round_numbers(row[10].value),
'totalNetRentReceived': round_numbers(row[11].value),
'netAfterInterestCashPurchase': round_numbers(row[12].value),
'netAfterInterestFifty': round_numbers(row[13].value),
'netAfterInterestTwenty': round_numbers(row[14].value)
})
sheetDataEntry['purchases'].append(p)
for i in range(13, 19):
row = rows[i]
if row[0].value is not None:
invest = Investment({
'name': row[0].value,
'cash': round_numbers(row[1].value),
'annualised1': round_percent(row[2].value),
'fiftyDeposit': round_numbers(row[3].value),
'annualised2': round_percent(row[4].value),
'twentyDeposit': round_numbers(row[5].value),
'annualised3': round_percent(row[6].value)
})
sheetDataEntry['investments'].append(invest)
return sheetDataEntry
def proceed(self, fileNames):
sheetsData = []
for fileName in fileNames:
wb = load_workbook(filename=join(INPUT_PATH, fileName), read_only=True, data_only=True)
ws = wb['Sheet1']
sheetDataEntry = self.readSheet(ws, fileName)
sheetsData.append(sheetDataEntry)
return sheetsData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment