文書更新:2019年07月23日(火) 午前9時18分52秒

Home > 備忘録 > 言語関連 > python に関すること > pyexcel-ods3 で LibreOffice データを扱う( 35 )

python3 で LibreOffice の操作をする。こちらのページを参考にしました。

必要なパッケージを install する

  1. pyexcel を install する
  2. [root@server]# pip3 install pyexcel pyexcel-ods3
    excel データを扱う場合は、pyexcel-xls を install する

calc データを読み込む

  1. 単純に読み込む
  2. demo.ods のデータを単純に読み込む
    [root@server]# vi demo.py
    #!/usr/bin/env python3
    # coding: utf-8
    import pyexcel
    book = pyexcel.get_book(file_name="path/demo.ods")
    print(book.sheet_names())	# sheet name の表示
    sheet=book.sheet_names()[3]	# sheet name の選択
    
    for i,row in reversed(list(enumerate(book[sheet]))):	# 空白行を削除する(末行から削除していく)
    	if not row[0]:
    		del book[sheet].row[i]
    
    print(book[sheet])	# データの表示
    
    print(book[sheet][5,1])	# 5行1列のデータ表示
    ※実行結果
    ['シート1', 'シート2', 'シート3', '旧暦', 'シート4', 'シート5']
    旧暦:
    +------+----------------+
    | 月日 | 呼称           |
    +------+----------------+
    | 101  | 旧正月         |
    +------+----------------+
    | 116  | ジュロクニチー |
    +------+----------------+
    | 120  | 二十日正月     |
    +------+----------------+
    | 303  | ハマウリー     |
    +------+----------------+
    | 504  | ハーリー       |
    +------+----------------+
    | 615  | 六月ウマチー   |
    +------+----------------+
    | 615  | 綱引き         |
    +------+----------------+
    | 625  | 六月カシチー   |
    +------+----------------+
    | 707  | 旧七夕         |
    +------+----------------+
    | 713  | ウンケー       |
    +------+----------------+
    | 715  | 旧盆           |
    +------+----------------+
    | 715  | ウークイ       |
    +------+----------------+
    | 808  | トーカチ       |
    +------+----------------+
    | 815  | ジュゥグヤー   |
    +------+----------------+
    | 907  | カジマヤー     |
    +------+----------------+
    | 913  | 十三夜         |
    +------+----------------+
    | 1010 | 十日夜         |
    +------+----------------+
    | 1208 | ムーチー       |
    +------+----------------+
    ハーリー
  3. 辞書式データとして読み込む
  4. demo.ods データの1行目がデータの見出し(header)の場合、辞書型として読み込むと便利な場合がある
    [root@server]# vi demo.py
    #!/usr/bin/env python3
    # coding: utf-8
    
    import pyexcel
    
    book = pyexcel.get_book(file_name="path/demo.ods")
    name=book.sheet_names()[3]	# sheet name の取得
    sheet=book[name]	# sheet の選択
    sheet.name_columns_by_row(0)	# header 行の設定
    print(sheet.to_dict())
    ※実行結果
    OrderedDict([('月日', [101, 116, 120, 303, 504, 615, 615, 625, 707, 713, 715, 715, 808, 815, 907, 913, 1010, 1208, '', '']), ('呼称', ['旧正月', 'ジュロクニチー', '二十日正月', 'ハマウリー', 'ハーリー', '六月ウマチー', '綱引き', '六月カシチー', '旧七夕', 'ウンケー', '旧盆', 'ウークイ', 'トーカチ', 'ジュゥグヤー', 'カジマヤー', '十三夜', '十日夜', 'ムーチー', '', ''])])
  5. 辞書式データとして読み込む(空白行を除く)
  6. demo.ods データの1行目がデータの見出し(header)の場合、辞書型として読み込むと便利な場合がある
    [root@server]# vi demo.py
    #!/usr/bin/env python3
    # coding: utf-8
    
    import pyexcel
    
    book = pyexcel.get_book(file_name="path/demo.ods")
    name=book.sheet_names()[3]	# sheet name の取得
    sheet=book[name]	# sheet の選択
    
    data={}
    for i in sheet.row_range():
    	if i==0:	# header 行の処理
    		data = {cell:[] for cell in sheet.row[i]}
    	else:
    		if sheet.row[i][0]:	# 1列目が空白でない場合
    			for j in sheet.column_range():	# header 行以外の処理
    				data[sheet[0,j]].append(sheet[i,j])
    print(data)
    ※実行結果
    {'月日': [101, 116, 120, 303, 504, 615, 615, 625, 707, 713, 715, 715, 808, 815, 907, 913, 1010, 1208], '呼称': ['旧正月', 'ジュロクニチー', '二十日正月', 'ハマウリー', 'ハーリー', '六月ウマチー', '綱引き', '六月カシチー', '旧七夕', 'ウンケー', '旧盆', 'ウークイ', 'トーカチ', 'ジュゥグヤー', 'カジマヤー', '十三夜', '十日夜', 'ムーチー']}