openpyxl use (1)

openpyxl

openpyxl is a python library for reading and writing excel files. The purpose of this article is to introduce how to use openpyxl to process excel files.

install openpyxl

pip install openpyxl

create a workbook

  • Use Workbook to create an empty workbook (with a sheet by default)
from openpyxl import Workbook
wb = Workbook()
  • Use load_workbook to create a workbook based on an existing excel file
from openpyxl import load_workbook
wb = load_workbook(filename='fileName.xlsx')

Create and get a worksheet from a workbook

  • Check which Sheets are included in excel
sheet_list = wb.sheetnames
  • Get the default selected (activated) Sheet
ws = wb.active
  • Get the specified Sheet according to the Sheet name
ws = wb['SheetName']
  • Get each Sheet
for sheet in wb:
    print(sheet)
  • create sheet
# 在最后一个位置创建 Sheet
ws1 = wb.create_sheet('mySheet')
# 在第一个位置创建 Sheet
ws2 = wb.create_sheet('mySheet', 0)
# 在倒数第二个位置创建 Sheet
ws3 = wb.create_sheet('mySheet', -1)
# 也可以不传参数,它创建的 Sheet 名称默认由 Sheet 到 Sheetn 依次递增
ws4 = create_sheet()

Modify Sheet title

ws.title = 'customName'

Copy a Sheet

Note: The pictures and icons contained in the Sheet will not be copied. Sheets cannot be copied between workbooks, and workbooks opened in read-only or write-only mode cannot be copied.

wb.copy_worksheet(ws)

Manipulate the data in the Sheet

Get a single cell in Sheet

  • method one
cell = ws['A1']
# 也可以给单元格赋值
ws['A1'] = '哈哈'
  • Method 2: Use the cell() method
# 三个参数 row:第几行 column:第几列 value:单元格的值(不传 value 则表示获取当前单元格)
ws.cell(row=1, column=1, value='哈哈')

get multiple cells

  • Get the cells of a certain range (range)
# 表示获取三行四列的单元格
cells = ws['A1':'D3']
  • Get range of rows and columns
# 获取第一行
row_one = ws[1]
# 第二列
col_two = ws['B']
# 第二行到第三行
row_range = ws[2:3]
# 第一列到第四列
col_range = ws['A':'D']
  • The iter_rows and iter_cols methods get the row and column cells of the specified unit
# 获取两行两列的单元格范围
ws.iter_rows(min_col=1, max_col=2, min_row=1, max_row=2)
ws.iter_cols(min_col=1, max_col=2, min_row=1, max_row=2)
  • get all rows and all columns
# 所有行
rows = ws.rows
# 所有列
cols = ws.columns
  • get only cell value
vals = ws.values
print(tuple(vals))
# values_only 标识为 True 表示只获取单元格的值
vals = ws.iter_rows(values_only=True)
print(tuple(vals))
vals = ws.iter_cols(values_only=True)
print(tuple(vals))
  • Get the value of the cell
for cell in ws[1]:
    # 单元格的 value 属性
    print(cell.value)

save as a file

After a series of operations, you can use the save method to save the final modified workbook as a file

# 如果存在同名的文件则会进行覆盖且不会有任何警告
wb.save('myWb.xlsx')

Guess you like

Origin blog.csdn.net/weixin_46828094/article/details/130959861