Monday, January 25, 2021

How to intergrate openpyxl with django and using the excel sheet as the database to query data

Hello Everyone,
I have a python file which I run on the terminal and give me the result that I want. I want to to do the same via a web app whereby instead of interacting with the terminal, I can do that via a browser through a form. I have decided to choose django as the server. Below is the file that I need to integrate to django. It works well in the terminal. I also have the excel file.
Now my concern was how can I integrate it with django such that fields like link_name can be filed through a form and when I click a button should give the required results. Thank you fo your time.

def find_fault(request):
wb = xl.load_workbook("Find Fault App Documentation.xlsx")
link_name = input("Link Name: ")
test_location = input("Test Location: ")
distance = float(input("Distance of Fault(km): "))
error = int(input("Error margin(m): "))
nature = input("Nature of Fault: ").lower()
for sheet in wb.worksheets:
if sheet.title == link_name:
for i in range(2, sheet.max_row + 1):
cell0 = sheet.cell(i, 1)
if cell0.value == test_location:
distance1 = distance * 1000 + sheet.cell(i, 2).value
for value in range(2, sheet.max_row + 1):
cell = sheet.cell(value, 2)
cell1 = sheet.cell(value, 1)
cell2 = sheet.cell(value, 3)
if cell.value in range(int(distance1) - error, int(distance1) + error):
if nature == "kink":
print()
print("Kink at {cell1.value}")
print("Pairs at this point:")
print(cell2.value)
elif nature == "break":
print()
print("Break at {cell1.value}")
print("Pairs at this point:")
print(cell2.value)
else:
print()
print("Fault at {cell1.value}")
print("Pairs at this point:")
print(cell2.value)

for value in range(2, sheet.max_row):
cell1 = sheet.cell(value, 2)
cell2 = sheet.cell(value + 1, 2)
cell3 = sheet.cell(value, 4)
cell4 = sheet.cell(value, 1)

if distance1 in range(cell1.value, cell2.value):
if distance1 in range(cell1.value + error, cell2.value - error):
if nature == "kink":
print()
print("Kink at {(distance1 - cell1.value) / 1000} "
"km from {cell4.value}")
print(
"Pair at this point >> {cell3.value}")
elif nature == "break":
print()
print("Break at {(distance1 - cell1.value) / 1000} "
"km from {cell4.value}")
print(
"Pair at this point >> {cell3.value}")
else:
print()
print("Fault at {(distance1 - cell1.value) / 1000} "
"km from {cell4.value}")
print(
"Pair at this point >> {cell3.value}")

if distance * 1000 > (sheet.cell(sheet.max_row, 2).value - (sheet.cell(i, 2).value + error)):
print()
print(
"The Distance from {test_location} to {sheet.cell(sheet.max_row, 1).value} ")
print("is about "
"{(sheet.cell(sheet.max_row, 2).value - sheet.cell(i, 2).value) / 1000} km")

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPsfuofr_6gYz20tjU%3De751rcV%2Bi%3D7j6VfzTppCwmKfsajhO%3DA%40mail.gmail.com.

No comments:

Post a Comment