Python獲取excel內(nèi)容及相關(guān)操作代碼實(shí)例
Python沒有自帶openyxl,需要安裝: pip install openyxl
打開excel文檔: openyxl.load_workbook(excel地址) - 打開現(xiàn)有excel文件
openyxl.Workbook() - 新建一個excel文件
返回一個工作博對象
import openpyxl
wb = openpyxl.load_workbook("test.xlsx")
print(type(wb)) # <class 'openpyxl.workbook.workbook.Workbook'>
openpyxl.load_workbook()函數(shù),傳入一個存在的excel文件名稱/excel文件名稱+文件路徑,返回一個workbook對象。
從workbook對象中獲取工作表
import openpyxl wb = openpyxl.load_workbook("test.xlsx") # print(type(wb)) # <class 'openpyxl.workbook.workbook.Workbook'> # 工作簿對象.sheetnames - 獲取當(dāng)前工作簿中所有表的名字 # print(wb.sheetnames) ['Sheet1', 'Sheet2', 'Sheet3'] # 工作簿對象.active - 獲取當(dāng)前活動表對應(yīng)的Worksheet對象 # print(wb.active) <Worksheet "Sheet1"> # 工作簿對象[表名] - 根據(jù)表名獲取指定表對象 # print(wb["Sheet2"]) <Worksheet "Sheet2"> # 表對象.title - 獲取表對象的表名 ws = wb["Sheet1"] # print(ws.title) Sheet1 # 表對象.max_row - 獲取表中最多有多少行 # print(ws.max_row) 15 # 表對象.max_column - 獲取表有多少列 print(ws.max_column) # 3
從表中取得單元格
import openpyxl wb = openpyxl.load_workbook("test.xlsx") ws = wb["Sheet1"] # 表對象['列號行號'] - 獲取指定列的指定行對應(yīng)的單元格對象(單元格對象是 Cell 類的對象,列號是從A開始,行號是從1開始) a = ws["A1"] # print(a) # <Cell 'Sheet1'.A1> # 單元格對象.value - 獲取單元格中的內(nèi)容 print(a.value) # 單元格對象.row - 獲取行號(數(shù)字1開始) print(a.row) # 單元格對象.column - 獲取列號(數(shù)字1開始) print(a.column) # 單元格對象.coordinate - 獲取位置(包括行號和列號) print(a.coordinate) # 表對象.iter_rows() - 一行一行的取 row_s = ws.iter_rows() for a in row_s: for i in a: print(i.value) # 表對象.iter_cols() - 列表一列的取 col_s = ws.iter_cols() for c in col_s: for j in c: print(j.value)
用字母來指定列時會出現(xiàn)列Z之后用兩個字母代替,可以調(diào)用表的cell()方法,傳入整數(shù)作為行數(shù)和列數(shù),第一行或者第一列的整數(shù)是1,而不是0
表對象.cell(行號,列號)
import openpyxl wb = openpyxl.load_workbook("test.xlsx") ws = wb["Sheet1"] # 獲取第二列的所有內(nèi)容 max_row = ws.max_row for row in range(1, max_row + 1): cell = ws.cell(row, 2) print(cell.value)
從表中取得列和行
取電子表格中一行、一列或一個矩形區(qū)域中的所有 Cell 對象
表對象[位置1:位置2] 獲取指定范圍內(nèi)的所有單元格
import openpyxl from openpyxl.utils import get_column_letter, column_index_from_string wb = openpyxl.load_workbook("test.xlsx") ws = wb["Sheet1"] max_row = ws.max_row column = get_column_letter(max_row) # 獲取第一列所有單元格對象 row2 = ws["A1":f"{column}1"] ss = [(cell.coordinate, cell.value) for cells in row2 for cell in cells] print(ss)
import openpyxl from openpyxl.utils import get_column_letter, column_index_from_string wb = openpyxl.load_workbook("test.xlsx") ws = wb["Sheet1"] max_cols = ws.max_column column = get_column_letter(max_cols) # 獲取第一片區(qū)域所有單元格對象 row2 = ws["A1":f"{column}3"] ss = [(cell.coordinate, cell.value) for cells in row2 for cell in cells] print(ss)
創(chuàng)建并保存Excel文檔
openpyxl.Workbook() - 創(chuàng)建空的Excel文件對應(yīng)的工作薄對象
工作薄對象.save(文件路徑) - 保存文件
import openpyxl wb = openpyxl.load_workbook("test.xlsx") ws = wb["Sheet1"] # 修改sheet的名稱 ws.title = "hello_world" wb.save("test.xlsx")
創(chuàng)建和刪除sheet
工作薄對象.create_sheet(title, index) - 在指定工作薄中的指定位置(默認(rèn)是最后)創(chuàng)建指定名字的表,并返回表對象
工作薄對象.remove(表對象) - 刪除工作薄中的指定表
import openpyxl wb = openpyxl.load_workbook("test.xlsx") wb.create_sheet() print(wb.sheetnames) wb.create_sheet("test1") print(wb.sheetnames) wb.create_sheet("test2", index=0) print(wb.sheetnames) wb.remove(wb["test2"]) print(wb.sheetnames) wb.save("test.xlsx")
將數(shù)據(jù)寫入表格中
import openpyxl wb = openpyxl.load_workbook("test.xlsx") ws = wb["hello_world"] # 方式一 ws["A4"] = "hello_world" # 方式二 ws.cell(4, 5).value = "hello_test" wb.save("test.xlsx")
設(shè)置單元格樣式
用表格展示數(shù)據(jù)的時候,有的時候需要對不同的數(shù)據(jù)以不同的風(fēng)格進(jìn)行展示從而達(dá)到分區(qū)或者強(qiáng)調(diào)的作用。
import openpyxl from openpyxl.styles import Font, PatternFill, Border, Side, Alignment # 1.打開工作薄 wb = openpyxl.load_workbook("test.xlsx") ws = wb["hello_world"] # 2.設(shè)置單元格字體樣式 """ Font( name=None, # 字體名,可以用字體名字的字符串 strike=None, # 刪除線,True/False color=None, # 文字顏色 size=None, # 字號 bold=None, # 加粗, True/False italic=None, # 傾斜,Tue/False underline=None # 下劃線, 'singleAccounting', 'double', 'single', 'doubleAccounting' ) """ # 1:創(chuàng)建字體對象 font1 = Font( size=20, italic=True, color="ff0000", bold=True, strike=True ) # 2:設(shè)置指定單元格的字體 # 單元格對象.font = 字體對象 ws["B2"].font = font1 # 3:設(shè)置單元格填充樣式 """ PatternFill( fill_type=None, # 設(shè)置填充樣式: 'darkGrid', 'darkTrellis', 'darkHorizontal', 'darkGray', 'lightDown', 'lightGray', 'solid', 'lightGrid', 'gray125', 'lightHorizontal', 'lightTrellis', 'darkDown', 'mediumGray', 'gray0625', 'darkUp', 'darkVertical', 'lightVertical', 'lightUp' start_color=None # 設(shè)置填充顏色 ) """ fill = PatternFill( fill_type="solid", start_color="ffff00" ) ws["B2"].fill = fill # 設(shè)置單元格對齊樣式 al = Alignment( horizontal="right", # 水平向方: center 靠左:left 靠右: right vertical="top" # 垂直方向: center, top, bottom ) ws["B2"].alignment = al # 設(shè)置邊框樣式 # 設(shè)置邊對象(四個邊可以是一樣的也可以不同,如果不同就創(chuàng)建多個Side對象) side = Side(border_style="thin", color="0000ff") # 設(shè)置邊框?qū)ο?left、right、top、bottom表示的是邊框的四個邊,這兒四個邊使用的是一個邊對象) db = Border(left=side, right=side, top=side, bottom=side) ws["B2"].border = db # 設(shè)置單元格的寬度和高度 # 設(shè)置指定列的寬度 ws.column_dimensions["A"].width = 20 # 設(shè)置指定行的高度 ws.row_dimensions[1].height = 45 wb.save("test.xlsx")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django admin 實(shí)現(xiàn)search_fields精確查詢實(shí)例
這篇文章主要介紹了Django admin 實(shí)現(xiàn)search_fields精確查詢實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03基于python實(shí)現(xiàn)把圖片轉(zhuǎn)換成素描
這篇文章主要介紹了基于python實(shí)現(xiàn)把圖片轉(zhuǎn)換成素描,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11python3 cvs將數(shù)據(jù)讀取為字典的方法
今天小編就為大家分享一篇python3 cvs將數(shù)據(jù)讀取為字典的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python中schedule模塊關(guān)于定時任務(wù)使用方法
這篇文章主要介紹了Python中schedule模塊關(guān)于定時任務(wù)使用方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05