본문 바로가기

프로그래밍(공통)

[python] 엑셀에 저장된 주소 정보를 위도, 경도로 변환하기

환경 설정

$ pip install requests
$ pip install openpyxl

추가로 아래 파이썬 코드가 작성된 폴더에 엑셀 파일을 위치시킨다.

첫번째 열에 주소 정보를 입력

 

API

 

공간정보 오픈플랫폼 오픈API

Geocoder API 2.0 레퍼런스 Geocoder API 2.0 레퍼런스입니다. API 버전 : Geocoder API 2.0 레퍼런스 Geocoder API 1.0 레퍼런스 소개 주소를 좌표로 변환하는 서비스를 제공합니다. 요청URL을 전송하면 지오코딩 서

www.vworld.kr

사이트 회원가입 후 API를 발급받고, Geocoder API 1.0 레퍼런스를 활용

 

geo.py

#-*-coding: utf-8 -*-
import requests
import openpyxl
import json

## 엑셀 파일 오픈
filename = "address.xlsx"
exelFile = openpyxl.load_workbook(filename)

## 시트 설정
sheet = exelFile.worksheets[0]

## 데이터 가져오기
rowCount = 1
for row in sheet.rows:	

	try:
		## geocoder 호출
		r = requests.get("http://apis.vworld.kr/new2coord.do?q="+row[0].value+"&output=json&epsg=epsg:4326&apiKey=INPUT_YOUR_API_KEY")

		## cell 설정 [ (10)J1 ~ J* : 위도 / (11)K1 ~ K* : 경도]
		lat_cell = sheet.cell(row = rowCount, column = 2)
		lng_cell = sheet.cell(row = rowCount, column = 3)

		## 데이터 추가
		lat_cell.value = r.json()["EPSG_4326_Y"]
		lng_cell.value = r.json()["EPSG_4326_X"]
	except KeyError as ke:
		lat_cell.value = 0
		lng_cell.value = 0
	except TypeError as te:
		lat_cell.value = 0
		lng_cell.value = 0
		
	rowCount = rowCount + 1

## 데이터 저장
exelFile.save("address.xlsx")