Import Cartesian coordinates from Excel and count its area

I'm new and struggling with calculating xy area from Excel data ( or it can be any type of data with xy coordinates). Moreover according to task it must use all existing coordinates in Excel file.

My code

import openpyxl

sheet = book.active

for row_i in range(2, sheet.max_row + 1):
x = sheet.cell(row=row_i, column=1).value
y = sheet.cell(row=row_i, column=2).value
polygon = [[x,y]]
print(polygon)
area = 0.0
n = len(polygon)
for i in range(n):
i1 = (i+1)%n
area += polygon[i]*polygon[i1] - polygon[i1]*polygon[i]
area *= 0.5
print('area = ', area)

I don't know how to make that polygon = [[x,y]] would be like polygon [[608....,5105...],[6085.....,517...]], my code only picks last row data, I need that it pick all data and use it to calculate. Or if you have better idea, please share it.

It is picking the last because you are not appending to the array. Check below code:

import openpyxl

sheet = book.active
polygon = []

for row_i in range(2, sheet.max_row + 1):
x = sheet.cell(row=row_i, column=1).value
y = sheet.cell(row=row_i, column=2).value
polygon.append([x,y])

print(polygon)
area = 0.0
n = len(polygon)
for i in range(n):
i1 = (i+1)%n
area += polygon[i]*polygon[i1] - polygon[i1]*polygon[i]
area *= 0.5
print('area = ', area)

If you have access to the arcpy python module you can solve this by creating an arcpy.Array object of your coordinates. You would then use this as an input to create an arcpy.Polygon object. To get the area of the Polygon object you then use the call .area on your Polygon object to get the area.

If I understand your question correctly, the basic workflow would be:

1. Loop over coordinates and add them to your Array
2. Once all coordinates have been loaded to the Array use this as input to create a Polygon object
3. Call .area on the Polygon object to get the area of the polygon

I can provide more specific code examples if needed. More information can be found about the arcpy.Polygon object here. Additionally, this same workflow could be accomplished using the python packages of gdal.