How to open excel with python

silencement
Release: 2019-06-24 10:53:02
Original
24659 people have browsed it

How to open excel with python

Recently I have seen several people in the group ask questions about xlwt and wlrd. How should I put it? If office 2007 has just come out and everyone is not used to using xlsx files, it is understandable. It's been 10 years. Even if I haven't evolved to Office 2016, I still use Office 2003. It doesn't make sense. Can someone use xlsx to save it as xls? —— Deliberately do a few extra steps. What’s the purpose? For compatibility? Compatible with the ancient office2003? Moreover, since we use python to operate excel, we still need to save the file manually. Is this a crazy idea?

So, I still think that we should give up xls and transform to xlsx. That's why this article is written - xlwt and wlrd can only read and write xls files, but cannot operate xlsx files.

Solution: openpyxl. This is a very simple library that can be used in a few minutes. The installation is very simple. pip install openpyxl can be done in one step. I will mainly talk about the operation of excel. Of course, with all the nonsense mentioned above, students can probably guess that openpyxl can only operate xlsx files but not xls files.

1. Basic concepts

In openpyxl, three concepts are mainly used: Workbooks, Sheets, and Cells. Workbook is an excel worksheet; Sheet is a table page in the worksheet; Cell is a simple cell. openpyxl revolves around these three concepts. Regardless of reading and writing, it is "three things": open the Workbook, locate the Sheet, and operate the Cell. Below we introduce several common methods respectively for reading and writing.

2. Read xlsx

In order to do the experiment, I prepared an excel document in advance, which has three pages: Sheet1, Sheet2, and Sheet3. The following content is filled in:

First use

from openpyxl import load_workbook
Copy after login

to introduce the library

wb = load_workbook("template.xlsx")
Copy after login

to open an xlsx file

print(wb.sheetnames)    # ['Sheet1', 'Sheet2', 'Sheet3']
Copy after login

You can see which sheet pages are in the open Excel

sheet = wb.get_sheet_by_name("Sheet3")
Copy after login

After reading the specified Sheet page, the sheet becomes magical, with the desired content It's all here. For example:

print(sheet["C"])    # (<Cell Sheet3.C1>, <Cell Sheet3.C2>, <Cell Sheet3.C3>, <Cell Sheet3.C4>, <Cell Sheet3.C5>, <Cell Sheet3.C6>, <Cell Sheet3.C7>, <Cell Sheet3.C8>, <Cell Sheet3.C9>, <Cell Sheet3.C10>)      <-第C列
print(sheet["4"])    # (<Cell Sheet3.A4>, <Cell Sheet3.B4>, <Cell Sheet3.C4>, <Cell Sheet3.D4>, <Cell Sheet3.E4>)     <-第4行
print(sheet["C4"].value)    # c4     <-第C4格的值
print(sheet.max_row)    # 10     <-最大行数
print(sheet.max_column)    # 5     <-最大列数
for i in sheet["C"]:
  print(i.value, end=" ")    # c1 c2 c3 c4 c5 c6 c7 c8 c9 c10     <-C列中的所有值
Copy after login

Write to xlsx

First create a worksheet with

from openpyxl import Workbook
wb = Workbook()
Copy after login

, and then

sheet = wb.active
Copy after login

find Active sheet. The default sheet page of an empty excel sheet is called Sheet. If you want to change the name, you can directly assign a value to the title attribute.

sheet.title = "New Shit"
Copy after login

This attribute is readable and writable. Of course, this only applies to the current active page. For other pages, you can use create_sheet and remove_sheet to add and delete them.

It is relatively simple to write content into the sheet page, just like reading it above,

sheet[&#39;C3&#39;] = &#39;Hello world!&#39;
for i in range(10):
  sheet["A%d" % (i+1)].value = i + 1
Copy after login

We can also perform fancy operations, such as writing formulas:

sheet["E1"].value = "=SUM(A:A)"
Copy after login

Finally Remember to save

wb.save(&#39;保存一个新的excel.xlsx&#39;)
Copy after login

The above is the detailed content of How to open excel with python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template