-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.py
236 lines (187 loc) · 7.82 KB
/
calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import math
from pathlib import Path
from openpyxl import load_workbook
from openpyxl_image_loader import SheetImageLoader
import openpyxl
from openpyxl.workbook.workbook import Workbook
import xlsxwriter as xw
import os
from tkinter import filedialog
from utils.clear_entry import ClearEntry
from utils.insert_default_value import InsertDefault
class Calculate:
def __init__(self, obj):
self.obj = obj
'''
method: calculate all the sheet value
target_file: already created file with values
we wil retreive all the value to
calculate pure value
new file: which we will create using target file
calculate value
'''
def calculate_purity(self):
# getting current date
self.date = self.obj.entry7.get()
# getting the target file path from tkinter entry
self.path_excel_sheet = self.obj.entry0.get()
# saved file location
self.path_saved_location = self.obj.entry1.get()
# if file path is specified than
# file will save on specified location
# otherwise it will save on default loaction (Desktop/current_date/file.xlsx)
if self.path_saved_location == "":
self.file_saved_location(r"C:\Users\lenovo\Desktop\PurityCalculatorFiles")
else:
self.file_saved_location(f"{self.path_saved_location}\PurityCalculatorFiles")
# getting target file sheet name
self.sheet_name = self.obj.entry2.get()
# getting target file raw parcent positon
self.raw_percent_position = self.obj.entry3.get()
# getting target file weight position
self.raw_weight_position = self.obj.entry4.get()
# getting target file raw position
# where number is start
# after header
self.raw_position = int(self.obj.entry5.get())
# substraction value
self.value = self.obj.entry6.get()
# method: creating new excel file
self.create_xlsx_file()
# method: retrieve data from target file
self.open_target_sheet()
# method: adding default data to new file
# addind column header
self.adding_default_data_to_xlsx()
# method: adding calculated value to the
# new xlsx file
self.adding_value_to_xlsx()
'''
location of new xlsx file
if input_path_is_empty:
create_new_path
else:
default_path
'''
def file_saved_location(self, location):
self.newPath = location + f"\{self.date}"
# if folder_exists:
# saved_file_on_available_path
# else:
# create_new_path
if not os.path.exists(self.newPath):
os.makedirs(self.newPath)
'''
create new xlsx file using
xlswriter
'''
def create_xlsx_file(self):
# retreiving target path
self.retrieving_file_path = Path(self.path_excel_sheet)
# create a workbook
# save the file
# named [location/(substract_value)(target_file_name)]
self.create_xlsx = xw.Workbook(f"{self.newPath}\(-{self.value}){os.path.basename(self.retrieving_file_path)}")
# adding new sheet to the new xlsx file
self.add_sheet_to_xlsx = self.create_xlsx.add_worksheet()
print("calc: create_xlsx_file")
'''
opening the target sheet using openpyxl
getting sheet name
'''
def open_target_sheet(self):
# open target sheet
self.open_sheet_from_path = load_workbook(f'{self.path_excel_sheet}')
# getting target sheet
self.get_sheet = self.open_sheet_from_path[self.sheet_name]
print("calc: open_target_sheet")
'''
adding default value to new xlsx file
'''
def adding_default_data_to_xlsx(self):
self.add_sheet_to_xlsx.write("B1","Purity")
self.add_sheet_to_xlsx.write("C1",f"Purity(-{self.value})")
self.add_sheet_to_xlsx.write("D1","Weight")
self.add_sheet_to_xlsx.write("E1","Pure Weight")
print("calc: adding_default_data_to_xlsx")
'''
getting each value from target file
adding the value to the new xlsx file
'''
def adding_value_to_xlsx(self):
# total raw on target sheet
self.sheet_raw_count = self.get_sheet.max_row + 1
print(self.sheet_raw_count)
# var: total impure gold
total_impure = 0
# var: total pure gold
total_pure = 0
# iteration for getting all the values and add values
# it will starts from raw_postion(where numerical value starts
# on target file)
# ends sheet_raw_count(maximum raw on target sheet)
for i in range(self.raw_position, self.sheet_raw_count):
# parcent raw on target sheet
# getting positon (column A/B/C) from tkinter entry(entry3)
get_percent_raw = self.get_sheet[f'{self.raw_percent_position}{i}']
# weight raw on (column A/B/C) target sheet
# getting position from tkinter entry(entry4)
get_weight_raw = self.get_sheet[f'{self.raw_weight_position}{i}']
# get parcent value
percent_value = get_percent_raw.value
# get weight value
weight_value = get_weight_raw.value
# -------------- Formula ----------------
# pure_value = ((percent - .50) * weight)%
# step-1: less_value = percent - input_value(.20/.50)
# step-2: pure_value = (less_value * weight)/100
# step-1
if (percent_value is not None) or (weight_value is not None):
less_value = float(percent_value) - float(self.value)
else:
print("-----")
# step-2
pure_value = (less_value * weight_value)/100
# write/add the value to the new file
self.add_sheet_to_xlsx.write(f"A{i+1}", i)
self.add_sheet_to_xlsx.write(f"B{i+1}", percent_value)
self.add_sheet_to_xlsx.write(f"C{i+1}", less_value)
self.add_sheet_to_xlsx.write(f"D{i+1}", weight_value)
self.add_sheet_to_xlsx.write(f"E{i+1}", self.truncate(pure_value, 2))
print(f"calc[{i}]: Creating done!")
# getting all impure gold
# impure gold: all target sheet weight value
# getting sum by: adding each value on every itaration
total_impure += weight_value
# getting all pure gold
# pure gold: pure weight value after calculation
# getting sum by: adding each value on every itaration
total_pure += self.truncate(pure_value, 2)
# write/add impure_gold and pure gold to new sheet
self.add_sheet_to_xlsx.write(f"D{self.sheet_raw_count + 2}", total_impure)
self.add_sheet_to_xlsx.write(f"E{self.sheet_raw_count + 2}", total_pure)
# closing the new xlsx file
self.create_xlsx.close()
ClearEntry(self.obj).clear()
# if value is
# > weight: 78.879
# after floor
# > return: 78.88
# but we need
# > return: 78.87
# created the method to solve this problem
def truncate(self, f, n):
return math.floor(f*10**n) / 10**n
# def print_excel_sheet_hard_copy(self):
# self.path = filedialog.askopenfilename()
# # loading the Excel File and the sheet
# self.load_file = openpyxl.load_workbook(self.path)
# self.load_file_sheet = self.load_file["Sheet1"]
# # calling the image_loader
# self.img_loader = SheetImageLoader(self.load_file_sheet)
# # get the image
# self.img = self.img_loader.get("A1")
# # showing the image
# self.img.show()
# # saving the image
# self.img.save('sheet.jpg')