速戰速決 Python - python 第三方庫(openpyxl): excel數據處理

速戰速決 Python https://github.com/webabcd/PythonSample
作者 webabcd

速戰速決 Python - python 第三方庫(openpyxl): excel數據處理

示例如下:

thirdLib/openpyxl/sample1.py

# openpyxl - excel 數據處理

from openpyxl import *
import sys

# sys.path[0] 用於獲取當前運行的目錄
path = sys.path[0] + "\demo1.xlsx"

# 實例化一個空的工作簿對象(可以把它理解爲 excel 文件)
wb = Workbook()
# load_workbook() - 通過指定的 excel 文件實例化工作簿對象
# wb = load_workbook(path)

# 當前 excel 文件中選中的 sheet 對象
sheet1 = wb.active
# 修改 sheet 的名稱
sheet1.title = "sheet1"
# 創建新的 sheet 並指定其名稱
sheet3 = wb.create_sheet("sheet3")
sheet4 = wb.create_sheet("sheet4")
sheet5 = wb.create_sheet("sheet5")
sheet6 = wb.create_sheet("sheet6")
# 在指定位置創建新的 sheet 並指定其名稱(index 如果是 -1 的話就代表在倒數第 2 個位置新建 sheet)
sheet2 = wb.create_sheet("sheet2", index=1)
# 刪除指定的 sheet
# wb.remove(sheet6)
# 刪除指定的 sheet
del wb["sheet6"]
# 獲取 excel 的所有 sheet 的名稱列表
print(wb.sheetnames) # ['sheet1', 'sheet2', 'sheet3', 'sheet4', 'sheet5']


# 爲單元格賦值的幾種方式
sheet1["A1"].value = "1_1" # 第一行第一列
sheet1.cell(1, 2).value = "1_2" # 第一行第二列
sheet1.append(["a", "b", "c"])

# 獲取指定位置的單元格對象
cell = sheet1.cell(3, 5)
# cell.row - 單元格的所在行的位置(數字表示)
# cell.column - 單元格的所在列的位置(數字表示)
# cell.column_letter - 單元格的所在列的位置(字母表示)
# cell.coordinate - 單元格的座標(字母和數字表示)
# cell.encoding - 單元格的編碼方式(默認是 utf-8)
# cell.data_type - 單元格的數據格式
#                  n 數值(默認值)
#                  s 字符串
#                  d 日期時間
print(cell.row, cell.column, cell.column_letter, cell.coordinate, cell.encoding, cell.data_type) # 3 5 E E3 utf-8 n


# 獲取第一列的全部單元格對象
print(sheet1["A"]) # (<Cell 'sheet1'.A1>, <Cell 'sheet1'.A2>, <Cell 'sheet1'.A3>)
# 獲取第一行的全部單元格對象
print(sheet1[1]) # (<Cell 'sheet1'.A1>, <Cell 'sheet1'.B1>, <Cell 'sheet1'.C1>, <Cell 'sheet1'.D1>, <Cell 'sheet1'.E1>)
# 獲取第一列到第二列的全部單元格對象
print(sheet1["A:B"]) # ((<Cell 'sheet1'.A1>, <Cell 'sheet1'.A2>, <Cell 'sheet1'.A3>), (<Cell 'sheet1'.B1>, <Cell 'sheet1'.B2>, <Cell 'sheet1'.B3>))
# 獲取第一行到第二行的全部單元格對象
print(sheet1[1:2]) # ((<Cell 'sheet1'.A1>, <Cell 'sheet1'.B1>, <Cell 'sheet1'.C1>, <Cell 'sheet1'.D1>, <Cell 'sheet1'.E1>), (<Cell 'sheet1'.A2>, <Cell 'sheet1'.B2>, <Cell 'sheet1'.C2>, <Cell 'sheet1'.D2>, <Cell 'sheet1'.E2>))
# 獲取第一行第一列到第二行第二列的全部單元格對象
print(sheet1["A1:B2"]) # ((<Cell 'sheet1'.A1>, <Cell 'sheet1'.B1>), (<Cell 'sheet1'.A2>, <Cell 'sheet1'.B2>))


for i in range(1, 4):
    for j in range(1, 6):
        sheet2.cell(i, j).value = f'{i}_{j}'
# 刪除第一行
sheet2.delete_rows(1)
# 刪除第一列
sheet2.delete_cols(1)
# 獲取 sheet 的行數和列數
print(sheet2.max_row, sheet2.max_column) # 2 4

# 遍歷出的數據是:每行的單元格值的元組
for row in sheet2.values:
    print(row)

# 遍歷出的數據是:每行的單元格對象的元組
for row in sheet2.rows:
    print(row)

# 遍歷出的數據是:指定範圍的每行的單元格對象的元組
for row in sheet2.iter_rows(min_col=1,max_col=2,min_row=1,max_row=2):
    print(row)

# 遍歷出的數據是:每列的單元格對象的元組
for column in sheet2.columns:
    print(column)

# 遍歷出的數據是:指定範圍的每列的單元格對象的元組
for column in sheet2.iter_cols(min_col=1,max_col=2,min_row=1,max_row=2):
    print(column)


for i in range(1, 4):
    for j in range(1, 6):
        sheet3.cell(i, j).value = f'{i}_{j}'
# 合併指定範圍的單元格,並保留左上單元格的數據
sheet3.merge_cells("A1:B1")
sheet3.merge_cells(start_column=3,end_column=4,start_row=1,end_row=3)


for i in range(1, 10):
    for j in range(1, 10):
        sheet4.cell(i, j).value = i * j
# 爲指定的單元格設置公式
sheet4.cell(1, 10).value = '=SUM(H1:I1)'


from openpyxl.utils import get_column_letter, column_index_from_string
# 根據列的數字位置返回字母位置
print(get_column_letter(4))  # D
# 根據列的字母位置返回數字位置
print(column_index_from_string('D'))  # 4


# 保存 excel
wb.save(path)

速戰速決 Python https://github.com/webabcd/PythonSample
作者 webabcd

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章