Windows 10 Pro + VsCode + Python3 + xlrd を使ってみる

Windows 10 Pro + VsCode + Python3 + xlrd を使ってみる

 

参考にしたサイトはこちら

PythonでExcel

xlrd API Reference

 

セットアップはこちら

Windows 10 pro + VsCode + Python3 を使ってみる

 

PowerShellのコンソールを起動後

> chcp 65001

Active code page: 65001

 

> pip3 install xlrd

Collecting xlrd

Downloading xlrd-1.1.0-py2.py3-none-any.whl (108kB)

100% |████████████████████████████████| 112kB 547kB/s

Installing collected packages: xlrd

Successfully installed xlrd-1.1.0

 

使用したデータのダウンロードはこちら郵便番号検索の17ISHIKA.CSV

ワークシート(ISHSIKAWA)に17ISHIKA.CSVを張り付けたデータを使用する。

# -*- coding: utf-8 -*-

import sys
import io
import xlrd
import os.path

sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')

xlfile = "samp.xlsx"
if os.path.exists(xlfile):
    xls = xlrd.open_workbook(xlfile)
    sheet1 = xls.sheet_by_index(0)
    nrows = sheet1.nrows-1
    ncols = sheet1.ncols 
    for r in range(1, nrows+1):
        print(sheet1.cell(r, 1).value, sheet1.cell(r, 8).value)

920   青草町
92013 相合谷町
920   暁町
~

/path/to/Python36\Lib\site-packages\xlrd\examples はコードはまだ見てない・・