The perfect combination of python and Excel

Excel is a popular and powerful spreadsheet application in the Windows environment. The openpyxl module allows Python programs to read and modify Excel spreadsheet files. For example, there may be a boring task that needs to copy some data from one spreadsheet and paste it into another spreadsheet. Or it may be necessary to select a few rows from thousands of rows and slightly modify them according to certain conditions. Or you need to look at hundreds of departmental budget spreadsheets and look for deficits.

It is this kind of boring and brainless spreadsheet task that can be done through Python. LibreOffice Calc, WPS and OpenOffice Calc can all handle the spreadsheet file format of Excel, which means that the openpyxl module can also handle spreadsheets from these applications.

You can download these software from https://www.libreoffice.org/ and http://www.openoffice.org/. Even if you have Excel installed on your computer, you may find these programs easier to use.

1. Basic definition of Excel document

  • Workbook (workbook): an Excel spreadsheet document;
  • Sheet: Each workbook can contain multiple sheets, such as: sheet1, sheet2, etc.;
  • Active sheet: the sheet currently viewed by the user;
  • Column: The column address starts from A;
  • Row: The row address starts from 1;
  • Cell: A grid in a specific row and column;

2. Install the openpyxl module

Python does not come with openpyxl, so it must be installed.

pip3 install openpyxl

3. Read Excel documents

A spreadsheet example.xlsx will be used. You can create this electronic document yourself with the following content:
Insert picture description here

After importing the openpyxl module, you can use the openpyxl.load_workbook() function. Open Excel document

# 导入工作薄
wb = openpyxl.load_workbook('excelDemo/example.xlsx') # 加载工作薄
wb.sheetnames           # 获取当前所有工作表的名称, 返回一个列表 
wb.active               # 获取当前活跃的工作表 

Get worksheet from workbook

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
sheet = wb['Sheet1']                # 工作表
sheet.title                         # 获取当前活动表的名称
sheet.cell(row=1, column=2)         #  获取单元格指定行和指定列的内容

Get cells in the table

The Cell object has a value property, not surprisingly, it contains the value stored in this cell. The Cell object also has row, column and coordinate properties, which provide the location information of the cell.

The row attribute gives the integer 1, the column attribute gives'B', and the coordinate attribute gives'B1'.

cell = sheet['A1']
cell_value = sheet['A1'].value
cell.row, cell.column cell.coordinate

4. Workbooks, worksheets, cells

The following are all functions, methods, and data types involved in reading cells from a spreadsheet file. A complete operation process is as follows:

  • Import the openpyxl module.
  • Call openpyxl.load_workbook()functions.
  • Get the Workbook object.
  • Call wb.sheetnames and wb.active to get the workbook details.
  • Get the Worksheet object.
  • Use the cell() method of the index or worksheet with the row and column keyword parameters.
  • Get the Cell object.
  • Read the value property of the Cell object

A complete case code is as follows:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import openpyxl

# 1. 读取excel文档
wb = openpyxl.load_workbook('excelDemo/example.xlsx')
# # 返回一个workbook对象, 有点类似于文件对象;
# print(wb, type(wb))

# 2. 在工作薄中取得工作表
# print(wb.get_sheet_names())
# 返回一个列表, 存储excel表中所有的sheet工作表;
print(wb.sheetnames)

# 返回一个worksheet对象, 返回当前的活动表;
# print(wb.get_active_sheet())
# print(wb.active)

# 3. 获取工作表中, 单元格的信息
# wb.get_sheet_by_name('Sheet1')
sheet = wb['example']
print(sheet['A1'])
print(sheet['B1'].value)

cell = sheet['B1']
print(cell.row, cell.column)

print(sheet.cell(row=3, column=2))
print(sheet.cell(row=3, column=2).value)
print(sheet.cell(row=3, column=2, value='www'))

# sheet的属性

print(sheet.max_column)
print(sheet.max_row)
print(sheet.title)
sheet.title = 'example'
print(sheet.title)

for row in sheet.rows:
    for cell in row:
        print(cell.value, end='\t')
    print('\n')

wb.save(filename="excelDemo/example.xlsx")

Guess you like

Origin blog.csdn.net/qdPython/article/details/112708878