LoginSignup
1
2

More than 5 years have passed since last update.

Python3 で Excel を取り扱う

Last updated at Posted at 2018-08-09

openpyxl の使用例です。
シート名、コラム数、列数を取得します。

xlsx_count.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#   xlsx_count.py
#
#                       Aug/09/2018
#
# -------------------------------------------------------------------
import sys
import os
from openpyxl import Workbook
from openpyxl import load_workbook
# -------------------------------------------------------------------
# [4]:
def xlsx_read_proc (xlsx_file):
    wb = load_workbook (filename = xlsx_file)
    ws = wb.active
#
    sys.stderr.write ("ws.title = %s\n" % ws.title)
    sys.stderr.write ("ws.max_column = %d\n" % ws.max_column)
    sys.stderr.write ("ws.max_row = %d\n" % ws.max_row)
#
    sheetnames = wb.sheetnames
    count = 0
    for name in sheetnames:
        ws = wb[name]
        sys.stderr.write ("%d\t%s\t%d\t%d\n"
             % (count,name,ws.max_column,ws.max_row))
        count += 1
#
    wb.close()
#
#-------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")

xlsx_file = sys.argv[1]
sys.stderr.write (xlsx_file + "\n")
#
if os.path.exists(xlsx_file):
    xlsx_read_proc(xlsx_file)
else:
    sys.stderr.write ("*** error *** xlsx doesn't exist ***\n")
#
sys.stderr.write ("*** 終了 ***\n")
# -------------------------------------------------------------------

使用方法

./xlsx_count.py ex01.xlsx
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2