Python openpyxl открыть файл

Use openpyxl — open, save Excel files in Python

In this article I show how to work Excel with openpyxl.

Environment

Runtime environment is as below.

Install

openpyxl is a Python library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.

However, openpyxl can also work with Mac Excel 2016 on my Macbook Pro.

Let’s install with pip command.

Create new Excel file

Import openpyxl

At first, import Workbook class from openpyxl

1from openpyxl import Workbook

Create Workbook

The Workbook is a Class for Excel file in openpyxl.

Creating Workbook instance works creating a new empty workbook with at least one worksheet.

1# Create a Workbook 2wb = Workbook()

Change the name of Worksheet

Now change the name of Worksheet to “Changed Sheet” .

1ws = wb.active 2ws.title = "Changed Sheet"

The active property in Workbook instance returns the reference to the active worksheet.

Save file

Save a file as sample_book.xlsx with save function.

1wb.save(filename = 'sample_book.xlsx')

The saved xlsx file exists in the same folder as the program.

download_file

Now open the file and check that the file name has been changed correctly.

rename_sheet

Open a Excel file that already exists

It is easy to get Workbook instance using load_workbook function.

1from openpyxl import load_workbook 2wb = load_workbook('sample_book.xlsx') 3print(wb.sheetnames)

Conclusion

  • Create Excel file creating Workbook instance
  • Save a Excel file with save function
  • Change name of default Worksheet with active property
  • Open Excel file with load_workbook function

Источник

openpyxl — A Python library to read/write Excel 2010 xlsx/xlsm files¶

openpyxl is a Python library to read/write 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 PHPExcel.

Security¶

By default openpyxl does not guard against quadratic blowup or billion laughs xml attacks. To guard against these attacks install defusedxml.

Mailing List¶

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") 

Documentation¶

Support¶

This is an open source project, maintained by volunteers in their spare time. This may well mean that particular features or functions that you would like are missing. But things don’t have to stay that way. You can contribute the project Development yourself or contract a developer for particular features.

Professional support for openpyxl is available from Clark Consulting & Research and Adimian. Donations to the project to support further development and maintenance are welcome.

Bug reports and feature requests should be submitted using the issue tracker. Please provide a full traceback of any error you see and if possible a sample file. If for reasons of confidentiality you are unable to make a file publicly available then contact of one the developers.

The repository is being provided by Octobus and Clever Cloud.

How to Contribute¶

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

1. Please join the group and create a branch (https://foss.heptapod.net/openpyxl/openpyxl/) and follow the Merge Request Start Guide. 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, changes without tests will not be accepted.) There are plenty of examples in the source 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 your repository) 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

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 3.6, 3.7, 3.8 and 3.9.

Источник

Читайте также:  Подключить api javascript api
Оцените статью