📌  相关文章
📜  openpyxl - Python (1)

📅  最后修改于: 2023-12-03 15:33:18.196000             🧑  作者: Mango

openpyxl - Python

Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. It allows programmers to work with Excel files without the need for Microsoft Excel. With openpyxl, you can create, modify, and extract data from Excel files using Python.

Features
  • Reading and writing Excel files
  • Creating new Excel files from scratch
  • Modifying existing Excel files
  • Working with worksheets, charts, and pivot tables
  • Cell formatting, merging, and validation
  • Formulas and functions
  • Styles and themes
  • Protection and encryption
  • Support for Unicode and date/time values
  • Cross-platform compatibility
Installation

You can install openpyxl using pip:

pip install openpyxl

Alternatively, you can download the source code and install it manually using setup.py:

python setup.py install
Usage

Here's an example of how to use openpyxl to create a new Excel file and populate it with data:

from openpyxl import Workbook

wb = Workbook()
ws = wb.active

ws['A1'] = 'Name'
ws['B1'] = 'Age'
ws['A2'] = 'John'
ws['B2'] = 25
ws['A3'] = 'Jane'
ws['B3'] = 30

wb.save('example.xlsx')

This code creates a new Excel workbook, adds a new worksheet, and sets the values of the first three cells. The workbook is then saved as "example.xlsx".

Documentation

The official openpyxl documentation is available at https://openpyxl.readthedocs.io/. You can find detailed information on how to use the library, including tutorials, examples, and API reference.

Conclusion

Openpyxl is a powerful library for working with Excel files in Python. Whether you need to create new Excel files, modify existing ones, or extract data from them, openpyxl has you covered. With its extensive features and cross-platform compatibility, openpyxl is a must-have tool for any Python developer who deals with Excel files.