日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 操作 excel

發布時間:2025/5/22 python 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 操作 excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 安裝openpyxl

python 中與excel操作相關的模塊:

  • xlrd庫:從excel中讀取數據,支持xls、xlsx
  • xlwt庫:對excel進行修改操作,不支持xlsx格式的修改
  • xlutils庫:在xlw和xlrd中,對一個已存在的文件進行修改
  • openpyxl庫:主要針對xlsx格式的excel進行讀取和編輯

2. Excel 中的三大對象

  • WorkBook:工作對象
  • Sheet:表單對象
  • Cell:表格對象

3. openpyxl 對 Excel的操作

  • 創建一個工作簿: wb = openpyxl.Workbook()
  • 新增一個sheet表單:wb.create_sheet(‘sheet2’)
  • 保存 demo.xlsx文件:wb.save(‘demo.xlsx’)
  • 打開工作簿:wb = openpyxl.load_workbook(‘demo.xlsx’)
  • 選取表單:sh = wb[‘sheet2’]
  • 讀取第一行、第一列的數據:ce = sh.cell(‘row=1, column=1’)
  • 按行讀取數據:row_data = list(sh.rows)
  • 按列讀取數據:columns_data = list(sh.clumns)
  • 關閉工作簿:wb.close()
寫入數據

寫入數據之前,該文件一定要處于關閉狀態

  • 寫入第一行、第二列數據:
value = 'reslut':sh.cell(row=1,column=2,value='result')
  • 獲取做大行總數、最大列總數:sh.max_row、sh.max_column
  • del 刪除表單的用法:del wb[‘sheet_name’]
  • remove 刪除表單的用法:
sh = wb['sheet_name'] wb.remove(sh)

4. excel數據

5. 操作案例

5.1 創建excel

import openpyxl# 創建一個工作簿 實例化 wb = openpyxl.Workbook()# 創建一個test_case的sheet表單 wb.create_sheet('StudentsInfo')# 刪除默認表 sh = wb['Sheet'] wb.remove(sh)# 保存為一個xlsx格式的文件 wb.save('批量導入用戶模板.xlsx')

5.2 讀取數據

import openpyxl # 第一步:打開工作簿 wb = openpyxl.load_workbook('批量導入用戶模板.xlsx')# 第二步:選取表單 sh = wb['StudentsInfo']
5.2.1 讀取指定單元格數據
# 第三步:讀取指定單元格 # 參數 row:行 column:列 ce = sh.cell(row = 1,column = 2) # 讀取第一行,第一列的數據 print(ce.value) 輸出:姓名
5.2.2按行讀取數據
# 按行讀取數據 list(sh.rows) print(list(sh.rows)[1:]) # 按行讀取數據,去掉第一行的表頭信息數據for cases in list(sh.rows)[1:]:stu_id = cases[0].valuestu_name = cases[1].valuestu_sex = cases[2].valuestu_age = cases[3].valueprint(stu_id, stu_name, stu_sex, stu_age) 輸出: [ (<Cell 'StudentsInfo'.A2>, <Cell 'StudentsInfo'.B2>, <Cell 'StudentsInfo'.C2>, <Cell 'StudentsInfo'.D2>), (<Cell 'StudentsInfo'.A3>, <Cell 'StudentsInfo'.B3>, <Cell 'StudentsInfo'.C3>, <Cell 'StudentsInfo'.D3>), (<Cell 'StudentsInfo'.A4>, <Cell 'StudentsInfo'.B4>, <Cell 'StudentsInfo'.C4>, <Cell 'StudentsInfo'.D4>), (<Cell 'StudentsInfo'.A5>, <Cell 'StudentsInfo'.B5>, <Cell 'StudentsInfo'.C5>, <Cell 'StudentsInfo'.D5>) ] 1 大佬 男 18 2 高手 女 18 3 大神 男 19 None 6 None None
5.2.2按行讀取數據 結果轉為json
all_row = [] for cases in list(sh.rows)[1:]:row = []stu_id = cases[0].valuestu_name = cases[1].valuestu_sex = cases[2].valuestu_age = cases[3].value# print(stu_id, stu_name, stu_sex, stu_age)data = {"stu_id": stu_id,"stu_name": stu_name,"stu_sex": stu_sex,"stu_age": stu_age}all_row.append(data)print(all_row) 輸出: [ {'stu_id': 1, 'stu_name': '大佬', 'stu_sex': '男', 'stu_age': 18}, {'stu_id': 2, 'stu_name': '高手', 'stu_sex': '女', 'stu_age': 18}, {'stu_id': 3, 'stu_name': '大神', 'stu_sex': '男', 'stu_age': 19}, {'stu_id': None, 'stu_name': 6, 'stu_sex': None, 'stu_age': None} ]
5.3 關閉工作薄
# 關閉工作薄 wb.close()

5.3 寫數據

5.3.1 寫入一行
# 第二步:選取表單 sh = wb['StudentsInfo']# 第一行輸入 sh.append(['1', '2', '3', '6'])

6. 樣式

from openpyxl.styles import Font, colors, Alignment
6.1 改變 sheet 標簽按鈕顏色

二進制顏色代碼大全(含圖)

sh.sheet_properties.tabColor = "FFAA33"
6.2 設置單元格風格

下面的代碼指定了等線24號,加粗斜體,字體顏色藍色。直接使用cell的font屬性,將Font對象賦值給它。

sh = wb['StudentsInfo']bold_itatic_24_font = Font(name='等線', size=24, italic=True, color=colors.BLUE, bold=True) sh['A1'].font = bold_itatic_24_font
6.3 對齊方式

也是直接使用cell的屬性aligment,這里指定垂直居中和水平居中。除了center,還可以使用right、left等等參數。

sh['B1'].alignment = Alignment(horizontal='center', vertical='center')
6.4 設置行高和列寬
# 第2行行高 sh.row_dimensions[2].height = 40 # C列列寬 sh.column_dimensions['C'].width = 30
6.5 合并和拆分單元格
sh.merge_cells('B1:G1') # 合并一行中的幾個單元格 sh.merge_cells('A3:D6') # 合并一個矩形區域中的單元格

參考:
https://www.cnblogs.com/wanglle/p/11455758.html
http://www.52codes.net/develop/shell/58896.html

總結

以上是生活随笔為你收集整理的python 操作 excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。