728x90

파이썬을 이용하여 엑셀을 읽고 특정 셀에 기록하는 코드 간단 예제이다.

 

# pip install requests
# pip install openpyxl
 
import requests
import openpyxl
import json
 
## 엑셀 파일 오픈
filename = "address.xlsx"
wb = openpyxl.load_workbook(filename)
 
## 시트 설정
# sheet = wb.worksheets[0] # active 시트 열기
sheet = wb['Sheet3']  # 시트명 직접 적어주기
 
## 데이터 가져오기
rowCount = 2
for row in sheet.rows:    
 
    try:
        ## 엑셀 읽어오기
        read_cell = row[1].value
        print(read_cell)
 
        ## cell 설정 
        lat_cell = sheet.cell(row = rowCount, column = 3)  # C열은 3
        lng_cell = sheet.cell(row = rowCount, column = 4)
 
        ## 데이터 추가
        lat_cell.value = "위도"
        lng_cell.value = "경도"
 
    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
 
## 데이터 저장 (엑셀 파일이 열린 상태에서는 저장 실패)
wb.save("address.xlsx")
 
# 닫기
wb.close()
 

 

'파이썬 > Python 활용' 카테고리의 다른 글

[Python] mariadb connection in a class  (0) 2022.03.16
[Python] Naver API 주소 좌표변환 예제  (0) 2022.03.16
블로그 이미지

Link2Me

,