node与python的excel库使用分享
因为工作原因需要读写excel文件,试着用了下python和node的对应库都不错,记录一下基础使用。
Node
xlsx: https://github.com/SheetJS/js-xlsx
Install
yarn add xlsx
Example
js
import * as XLSX from 'xlsx';// read xlsx file to workbookconst workbook = XLSX.readFile(`${__dirname}/some.xlsx`);// get first sheetconst sheetName = workbook.SheetNames[0];// convert sheet to jsonconst sheet = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);// ...// do things with the sheet// ...
python
Install
pip install openpyxl
Example
py
# read xlsx file to workbookworkbook = load_workbook('some.xlsx')sheet_name = 'some_sheetname'# get first sheet by namesheet = workbook[sheet_name]# iterate over rowsfor row in sheet.rows:for cell in row:print(cell.value)