python operates Excel

This document only supports excel version 2007 and above (and files with the suffix: .xlsx)

create table

OpenPyXl documentation says: https://openpyxl.readthedocs.io/en/latest/index.html

One: Install OpenPyXl

pip install openpyxl

Two: create a workbook instance

from openpyxl import Workbook         # 引入模块
wb = Workbook()                       # 创建一个工作簿实例,默认里面有一个表

Three: View all the tables in the book

print(wb.sheetnames)        # 打印薄中所有表的名称,输入的值为列表

openpyxl.workbook.Workbook.sheetnames()where the property is located

Four: create a table

ws1 = wb.create_sheet("Mysheet")                    # 在末尾插入"Mysheet"表 (默认)
ws2 = wb.create_sheet("Mysheet", 0)                 # 插入到指定的位置,0代表第一个位置

openpyxl.workbook.Workbook.create_sheet()where the property is located

Five: Get the table in the thin

ws = wb.active          # 获取薄中的第一个表
ws = wb['Sheet']        # 获取名称为‘Sheet’的表

Six: Modify the specified table

ws.title = "New Title"                        # 更改表的名字
ws.sheet_properties.tabColor = "1072BA"       # 设置表的标签颜色,默认为白色,颜色为RGB代码

Seven: Create a copy

source = wb.active                                               # 创建一个实例
target = wb.copy_worksheet(source)                  # 创建副本(复制)

openpyxl.workbook.Workbook.copy_worksheet()where the property is located

Eight: Preservation thin

wb.save('test.xlsx')            # 保存到文件,path为路径

When saving, if the file exists, replace the content inside, if not, create a new file

Action sheet

One: Open a thin and get a specific sheet

wb = openpyxl.load_workbook('test.xslx',guess_types = True)  # 打开已存在的薄
ws = wb.active

guess_typesWhether to enable data type, not enabled by default

data_onlyWhether it has a formula (do not understand what it means)

keep_vbaWhether to retain the basic visual elements, the default is retained

Two: manipulate the value of the cell

1.# 直接修改单元格的值
  ws['A1'] = 123
3.# 通过value属性来修改值
  a = ws['A1']
  a.value = 456
3.# 通过函数来修改值
  ws.cell(row = 1,column = 1 ,value = '123')    # 修改第一行第一列单元格的值
  ws.cell(1, 1, '123')     # 修改第一行第一列单元格的值(简写)
4.# 获取单元格的值
    a = ws['A1']
  print(a.value)
  print(ws['A1'].value)

row: row number, column: column number (number format), value: value to modify

Three: Access the range of cells by slicing

print(ws['a1:c4'])     # 打印出范围内的所有单元格
print(ws['a'])         # 打印A列所有活动的单元格
print(ws['a''c'])    # 打印A列到C列所有活动的单元格
print(ws[10])          # 打印所有活动列的第10行
print(ws[1:10])        # 打印所有活动列的1-10行,按行排序(A1,B1,C1....A2,B2,C2....)

Four: slice through built-in functions

1.# 第一种方法:按照行排序
  for row in ws.iter_rows(min_row=1, max_row=2, min_col = 2 ,max_col=3):
     for cell in row:
         print(cell)
输入结果:
<Cell '2007测试表'.B1>
<Cell '2007测试表'.C1>
<Cell '2007测试表'.B2>
<Cell '2007测试表'.C2>

openpyxl.worksheet.Worksheet.iter_rows()where the property is located

2.# 第二种方法:按照列排序
  for col in ws.iter_cols(min_row=1, max_row=2, min_col = 2 ,max_col=3):
      for cell in col:
          print(cell)
输入结果:
<Cell '2007测试表'.B1>
<Cell '2007测试表'.B2>
<Cell '2007测试表'.C1>
<Cell '2007测试表'.C2>

openpyxl.worksheet.Worksheet.iter_cols()where the property is located

Five: Traverse all active cells

print(list(ws.columns))     # 以 列 输出所有活动的单元格
print(list(ws.rows))        # 以 行 输出所有活动的单元格

Since the returned object is an object, it needs to be displayed by converting the list to a list

openpyxl.worksheet.Worksheet.rows()where the property is located

openpyxl.worksheet.Worksheet.columns()where the property is located

Six: Merge and split cells

ws.merge_cells('A2:D2')             # 合并单元格
ws.unmerge_cells('A2:D2')           # 拆分单元格

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325978880&siteId=291194637