もがき系プログラマの日常

もがき系エンジニアの勉強したこと、日常のこと、気になっている技術、備忘録などを紹介するブログです。

openpyxl(python)とphpSpreadsheet(php)はどっちが使いやすい?

はじめに

こんにちは。

python勉強中の僕です。

pythonでエクセルを操作するのに使用するツールに openpyxlというのがあるらしいです。

読み方はなんていうんですかね? おーぷんぱいくる?

phpでは、自分はよくPHPExcelを使用していたのですが、これって一体どっちが使いやすいんだろうなー?と、興味が湧いたので、確認してみました。

ちなみに、現在PHPExcelはDEPRECATEDになっているため後継版のPHPSpreadsheetで試してみます。

サンプルのエクセルは、自分が先程まで読んでた、退屈なことはPythonにやらせようで紹介されていたサンプルエクセルを使います。

サンプルエクセル

環境

php

$ php -v
PHP 7.1.5 (cli) (built: May 22 2017 00:14:43) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.5, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.5.3, Copyright (c) 2002-2017, by Derick Rethans

python

$ python --version
Python 3.6.0 :: Anaconda 4.3.1 (x86_64)

事前準備

全体

$ mkdir excel_test
$ mkdir excel_test/php
$ mkdir excel_test/python

php

$ cd excel_test/php
$ composer require phpoffice/phpspreadsheet 

python

$ pip install openpyxl

読み込みのチェック

以下のサンプルデータを使います。

f:id:kojirooooocks:20180227011410p:plain

php

プログラム

<?php
require("vendor/autoload.php");

use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader;

// ファイル読み込み
$reader      = new Reader();
$spreadsheet = $reader->load('./excel/example.xlsx');

// アクティブなシートから全データを取得
$data = $spreadsheet->getActiveSheet()->toArray();

// 表示
foreach ($data as $val) {
    echo $val[0] . "\t" . $val[1] . "\t" . $val[2] . "\n";
}

実行

$ php php/read.php 
4/5/2015 13:34 Apples  73
4/5/2015 3:41  Cherries    85
4/6/2015 12:46 Pears   14
4/8/2015 8:59  Oranges 52
4/10/2015 2:07 Apples  152
4/10/2015 18:10    Bananas 23
4/10/2015 2:40 Strawberries    98

python

プログラム

import openpyxl

# ファイル読み込み
workbook = openpyxl.load_workbook('./excel/example.xlsx')

# データを表示
for row in workbook.active:
    print(row[0].value, row[1].value, row[2].value, sep="\t")

実行

$ python python/read.py 
2015-04-05 13:34:02 Apples  73
2015-04-05 03:41:23 Cherries    85
2015-04-06 12:46:51 Pears   14
2015-04-08 08:59:43 Oranges 52
2015-04-10 02:07:00 Apples  152
2015-04-10 18:10:37 Bananas 23
2015-04-10 02:40:46 Strawberries    98

コード量は当然pythonのほうが短いですが、処理速度はphpの圧勝でした(体感ぜんぜん違う)

php7ではなくphp5系でやるとどうなるかもチェックしてみます。

$ phpenv install 5.6.9
...
..
.
[Success]: Built 5.6.9 successfully.

$ phpenv local 5.6.9
$ php -v
PHP 5.6.9 (cli) (built: Feb 27 2018 00:47:13) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.5.3, Copyright (c) 2002-2017, by Derick Rethans
    
$ php php/read.php 
4/5/2015 13:34 Apples  73
4/5/2015 3:41  Cherries    85
4/6/2015 12:46 Pears   14
4/8/2015 8:59  Oranges 52
4/10/2015 2:07 Apples  152
4/10/2015 18:10    Bananas 23
4/10/2015 2:40 Strawberries    98

実行してみるとやっぱり早いです。phpexcelはすごく遅い印象だったので、phpSpreadsheetの性能がいいのかな?

書き込みチェック

サンプルのエクセルと同じものを作成して、最後の行に、sumで合計値を追加したいと思います。

php

プログラム

<?php
require("vendor/autoload.php");

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;

// 新規スプレットシートとしてnew
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// サンプルのエクセル通りにデータを作る
$sheet->setCellValue('A1', '2015/4/5 13:34');
$sheet->setCellValue('A2', '2015/4/5 3:41');
$sheet->setCellValue('A3', '2015/4/6 12:46');
$sheet->setCellValue('A4', '2015/4/8 8:59');
$sheet->setCellValue('A5', '2015/4/10 2:07');
$sheet->setCellValue('A6', '2015/4/10 18:10');
$sheet->setCellValue('A7', '2015/4/10 2:40');
$sheet->setCellValue('B1', 'Apples');
$sheet->setCellValue('B2', 'Cherries');
$sheet->setCellValue('B3', 'Pears');
$sheet->setCellValue('B4', 'Oranges');
$sheet->setCellValue('B5', 'Apples');
$sheet->setCellValue('B6', 'Bananas');
$sheet->setCellValue('B7', 'Strawberries');
$sheet->setCellValue('C1', 73);
$sheet->setCellValue('C2', 85);
$sheet->setCellValue('C3', 14);
$sheet->setCellValue('C4', 52);
$sheet->setCellValue('C5', 152);
$sheet->setCellValue('C6', 23);
$sheet->setCellValue('C7', 98);

// 最後の行にC列のSUM情報を追加する
$sheet->setCellValue('C8', '=SUM(C1:C7)');

// 保存
$writer = new Writer($spreadsheet);
$writer->save('./excel/php_write.xlsx');

実行

$ php php/write.php

f:id:kojirooooocks:20180227011457p:plain

f:id:kojirooooocks:20180227011510p:plain

問題なし!

python

プログラム

import openpyxl

# 新規スプレットシートとして取得
workbook = openpyxl.Workbook()
sheet    = workbook.active

# サンプルのエクセル通りにデータを作る
sheet['A1'] = '2015/4/5 13:34'
sheet['A2'] = '2015/4/5 3:41'
sheet['A3'] = '2015/4/6 12:46'
sheet['A4'] = '2015/4/8 8:59'
sheet['A5'] = '2015/4/10 2:07'
sheet['A6'] = '2015/4/10 18:10'
sheet['A7'] = '2015/4/10 2:40'
sheet['B1'] = 'Apples'
sheet['B2'] = 'Cherries'
sheet['B3'] = 'Pears'
sheet['B4'] = 'Oranges'
sheet['B5'] = 'Apples'
sheet['B6'] = 'Bananas'
sheet['B7'] = 'Strawberries'
sheet['C1'] = 73
sheet['C2'] = 85
sheet['C3'] = 14
sheet['C4'] = 52
sheet['C5'] = 152
sheet['C6'] = 23
sheet['C7'] = 98

# 最後の行にC列のSUM情報を追加する
sheet['C8'] = '=SUM(C1:C7)'

# 保存
workbook.save('./excel/python_write.xlsx')

実行

$ python python/write.py

f:id:kojirooooocks:20180227011532p:plain

f:id:kojirooooocks:20180227011545p:plain

こっちも問題なし。

書き込みに関しては、そこまで速度の違いはなかったようでした。

終わりに

所感としては、やっぱりpythonのほうがスッキリ書けます。この辺は言語の違いですよね。

まぁこんな簡単なエクセルだと、はっきりとどっちが使いやすいっていうのは、いいにくいですよね。。。

でも、今回はopenpyxlもphpSpreadsheetもどちらも触れたので、とても勉強になりました。

しょうもないですが、これもgithubあげときますー。

yudaifujita0121/excel_tool_check_php_python_sample

ではでは。