Python处理Excel文件:openpyxl
OpenPyxl是一个Python库,用于读取/写入Excel 2010 XLSX/XLSM/XLTX/XLTM文件。 要处理xls,参考使用 xlrd/xlwd
安装组件openpyxl
- 官方地址
$ pip install openpyxl
- 要处理图片,还需要进下安装
$ pip install pillow
读取Excel
>>> from openpyxl import load_workbook
>>> wb = load_workbook(filename = 'empty_book.xlsx')
>>> sheet_ranges = wb['range names']
>>> print(sheet_ranges['D18'].value)
>>> wb.close()
3
创建Excel
>>> from openpyxl import Workbook
>>> wb = Workbook()
>>> ws = wb.active
>>>
>>> ws["A1"] = "=SUM(1, 1)"
>>> wb.save("formula.xlsx")
>>> wb.close()
编辑现有Excel
>>> from openpyxl import Workbook
>>> wb = load_workbook(filename = 'formula.xlsx')
>>> sheet_ranges = wb['range names']
>>>
>>> ws["A1"] = "25"
>>> wb.save("formula.xlsx")
>>> wb.close()
|