Python读写Excel 2007文件(xlsx)


A Python library to read/write Excel 2007 xlsx/xlsm files

http://openpyxl.readthedocs.org/en/latest/


openpyxl - A Python library to read/write Excel 2007 xlsx/xlsm files

Author: Eric Gazoni
Source code: http://bitbucket.org/openpyxl/openpyxl/src
Issues: http://bitbucket.org/openpyxl/openpyxl/issues
Generated: June 30, 2015
License: MIT/Expat
Version: 2.2.4

Introduction

Openpyxl is a Python library for reading and writing Excel 2010 xlsx/xlsm/xltx/xltm files.

It was born from lack of existing library to read/write natively from Python the Office Open XML format.

All kudos to the PHPExcel team as openpyxl was initially based on PHPExcelhttp://www.phpexcel.net/

Sample code:

from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

# Data can be assigned directly to cells
ws['A1'] = 42

# Rows can also be appended
ws.append([1, 2, 3])

# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()

# Save the file
wb.save("sample.xlsx")

User List

Official user list can be found on http://groups.google.com/group/openpyxl-users

How to Contribute Code

Any help will be greatly appreciated, just follow those steps:

1. Please start a new fork (https://bitbucket.org/openpyxl/openpyxl/fork) for each independent feature, don’t try to fix all problems at the same time, it’s easier for those who will review and merge your changes ;-)

2. Hack hack hack

3. Don’t forget to add unit tests for your changes ! (YES, even if it’s a one-liner, or there is a high probability your work will not be taken into consideration). There are plenty of examples in the /test directory if you lack know-how or inspiration.

4. If you added a whole new feature, or just improved something, you can be proud of it, so add yourself to the AUTHORS file :-)

5. Let people know about the shiny thing you just implemented, update the docs !

6. When it’s done, just issue a pull request (click on the large “pull request” button on yourrepository) and wait for your code to be reviewed, and, if you followed all theses steps, merged into the main repository.

For further information see Development Tools

This is an open-source project, maintained by volunteers on their spare time, so while we try to work on this project as often as possible, sometimes life gets in the way. Please be patient.

Other ways to help

There are several ways to contribute, even if you can’t code (or can’t code well):

  • triaging bugs on the bug tracker: closing bugs that have already been closed, are not relevant, cannot be reproduced, ...
  • updating documentation in virtually every area: many large features have been added (mainly about charts and images at the moment) but without any documentation, it’s pretty hard to do anything with it
  • proposing compatibility fixes for different versions of Python: we support 2.6 to 3.4, so if it does not work on your environment, let us know :-)

Installation

The best method to install openpyxl is using a PyPi client such as easy_install (setuptools) or pip. It is advisable to do this in a Python virtualenv without system packages:

$ pip install openpyxl

or

$ easy_install openpyxl

Note

To install from sources (there is nothing to build, openpyxl is 100% pure Python), you can download an archive from bitbucket (look in the “tags” tab).

There is support for the popular lxml library which will be used if it is installed.

After extracting the archive, you can do:

$ python setup.py install

Warning

To be able to include images (jpeg,png,bmp,...) into an openpyxl file, you will also need the ‘PIL’ library that can be installed with:

$ pip install pillow

or browse https://pypi.python.org/pypi/Pillow/, pick the latest version and head to the bottom of the page for Windows binaries.

Getting the source

Source code is hosted on bitbucket.org. You can get it using a Mercurial client and the following URLs:

  • $ hg clone https://bitbucket.org/openpyxl/openpyxl -r 2.2.4

or to get the latest development version:

  • $ hg clone https://bitbucket.org/openpyxl/openpyxl

Usage examples

Tutorial

  • Manipulating a workbook in memory
    • Create a workbook
    • Playing with data
      • Accessing one cell
      • Accessing many cells
      • Data storage
  • Saving to a file
  • Loading from a file

Cookbook

  • Simple usage
    • Write a workbook
    • Write a workbook from *.xltx as *.xlsx
    • Write a workbook from *.xltm as *.xlsm
    • Read an existing workbook
    • Using number formats
    • Using formulae
    • Merge / Unmerge cells
    • Inserting an image
    • Fold columns (outline)

Charts

  • Charts
    • Chart types
    • Creating a chart

Comments

  • Comments
    • Adding a comment to a cell
    • Loading and saving comments

Read/write large files

  • Optimized reader
  • Optimized writer

Working with styles

  • Working with styles
    • Introduction
    • Copying styles
    • Basic Font Colors
    • Applying Styles
    • Edit Page Setup
    • Edit Print Options
    • Header / Footer
    • Worksheet Additional Properties

Conditional Formatting

  • Conditional Formatting

Data Validation

  • Validating cells
    • Examples
    • Other validation examples

Information for Developers

  • Development
    • What is suppoprted
    • Supporting different Python versions
    • Coding style
    • Testing
      • Coverage
      • Organisation
      • Checking XML
      • Schema validation
      • Microsoft Tools
    • Contributing
    • Branch naming convention
    • Pull Requests
      • Documentation
    • Benchmarking
      • Memory Use
      • Pympler
  • Testing on Windows
    • Getting started
    • Setting up a development environment
      • Creating a virtual environment
    • lxml
    • tox

API Documentation

  • openpyxl package
    • Subpackages
    • Module contents

Indices and tables

  • Index
  • Module Index
  • Search Page

Release Notes

  • 2.2.5 (2015-06-29)
  • 2.2.4 (2015-06-17)
  • 2.2.3 (2015-05-26)
  • 2.2.2 (2015-04-28)
  • 2.2.1 (2015-03-31)
  • 2.2.0 (2015-03-11)
  • 2.2.0-b1 (2015-02-18)
  • 2.1.5 (2015-02-18)
  • 2.1.4 (2014-12-16)
  • 2.1.3 (2014-12-09)
  • 2.1.2 (2014-10-23)
  • 2.1.1 (2014-10-08)
  • 2.1.0 (2014-09-21)
  • 2.0.5 (2014-08-08)
  • 2.0.4 (2014-06-25)
  • 2.0.3 (2014-05-22)
  • 2.0.2 (2014-05-13)
  • 2.0.1 (2014-05-13) brown bag
  • 2.0.0 (2014-05-13) brown bag
  • 1.8.6 (2014-05-05)
  • 1.8.5 (2014-03-25)
  • 1.8.4 (2014-02-25)
  • 1.8.3 (2014-02-09)
  • 1.8.2 (2014-01-17)
  • 1.8.1 (2014-01-14)
  • 1.8.0 (2014-01-08)
  • 1.7.0 (2013-10-31)

你可能感兴趣的:(Python读写Excel 2007文件(xlsx))