Node.jsでPythonの実行 | python3Xのブログ

python3Xのブログ

ここでは40代、50代の方が日々の生活で役に立つ情報や私の趣味であるプログラム、Excelや科学に関する内容で投稿する予定です。

sample.py

from openpyxl import load_workbook
filename = 'test_worksheets.xlsx'
wb = load_workbook(filename, read_only=True)
print(f'{filename} のワークシート情報を読み込む')
ws0 = wb.worksheets[0]        #←最初のワークシートを取得
print(f'{ws0.title} のセルを1行ずつ表示')
for row in ws0:        #←行を読み込む
    values = [str(column.value) for column in row]        #←列を読み込む
    print(values)
 
test.js
var {PythonShell} = require('python-shell');
PythonShell.run('sample.py', null, function (err, result) {
  if (err) throw err;
 
  console.log(result);
});
 
[
  'test_worksheets.xlsx のワークシート情報を読み込む',
  'ワークシート0 のセルを1行ずつ表示',
  "['名前', ' 数学', ' 英語', '  国語', ' 理科', ' 社会']",
  "['高橋', '86', '65', '70', '40', '45']",
  "['鈴木', '58', '51', '56', '70', '81']",
  "['野村', '67', '86', '68', '49', '67']",
  "['小林', '60', '40', '64', '80', '72']",
  "['岡部', '54', '83', '67', '81', '42']"

]