聊聊 Python 辦公自動化之一 Excel
本文轉(zhuǎn)載自微信公眾號「AirPython」,作者星安果。轉(zhuǎn)載本文請聯(lián)系A(chǔ)irPython公眾號。
1. 前言
在我們?nèi)粘9ぷ髦?,?jīng)常會使用 Word、Excel、PPT、PDF 等辦公軟件
但是,經(jīng)常會遇到一些重復(fù)繁瑣的事情,這時候手工操作顯得效率極其低下;通過 Python 實現(xiàn)辦公自動化變的很有必要
接下來的 一系列 文章,我將帶大家對 Python 辦公自動化做一個全面的總結(jié),絕對的干貨!
2. 準(zhǔn)備
使用 Python 操作 Excel 文件,常見的方式如下:
- xlrd / xlwt
- openpyxl
- Pandas
- xlsxwriter
- xlwings
- pywin32
xlrd 和 xlwt 是操作 Excel 文件最多的兩個依賴庫
其中,
xlrd 負責(zé)讀取 Excel 文件,xlwt 可以寫入數(shù)據(jù)到 Excel 文件
我們安裝這兩個依賴庫
- # 安裝依賴庫
- pip3 install xlrd
- pip3 install xlwt
3. xlrd 讀取 Excel
使用 xlrd 中的 open_workbook(filepath) 打開本地一個 Excel 文件
- import xlrd
- # 打開文件,返回一個工作簿對象
- wb = xlrd.open_workbook(file_path)
工作簿對象的 nsheets 屬性獲取 Sheet 數(shù)目,sheet_names() 方法返回所有 Sheet 名稱的列表
- # 統(tǒng)計sheet數(shù)量
- sheets_num, sheets_names = wb.nsheets, wb.sheet_names()
- print('sheet數(shù)量一共有:', sheets_num)
- print('sheet名稱分別為:', sheets_names)
篩選出工作簿中的某一個 Sheet 有 2 種方式,分別是:
- 通過 Sheet 名稱
- 使用位置索引,從 0 開始
- # 獲取某一個sheet
- # 通過名稱或者索引獲取
- sheet = wb.sheet_by_index(0)
- # sheet = wb.sheet_by_name('第一個Sheet')
- print(sheet)
每一個 sheet 對象都可以利用 name、nrows、ncols 獲取 Sheet 名稱、行數(shù)量、列數(shù)量
另外
row_values(index)、col_values(index) 分別用于獲取某一行或某一列的數(shù)據(jù)列表
- # 獲取某一個sheet中,包含的行數(shù)量、列數(shù)量
- sheet_name, sheet_row_count, sheet_column_count = sheet.name, sheet.nrows, sheet.ncols
- print('當(dāng)前sheet名稱為:', sheet_name, ",一共有:", sheet_row_count, "行;有:", sheet_column_count, "列")
- # 單獨獲取某一行數(shù)據(jù),索引從0開始
- # 比如:獲取第2行數(shù)據(jù)
- row_datas = sheet.row_values(1)
- print('第2行數(shù)據(jù)為:', row_datas)
- # 單獨獲取某一列數(shù)據(jù),索引從0開始
- # 比如:獲取第二列數(shù)據(jù)
- column_datas = sheet.col_values(1)
- print('第2列數(shù)據(jù)為:', column_datas)
單元格可以通過行索引、列索引,調(diào)用 cell(row_index,column_index) 函數(shù)獲取
需要注意的是,行索引和列索引都是從 0 開始,即:0 代表第一行
在 xlrd 中,單元格的數(shù)據(jù)類型包含 6 種,用 ctype 屬性對應(yīng)關(guān)系如下:
- 0 -- 空(empty)
- 1 -- 字符串(string)
- 2 -- 數(shù)字(number)
- 3 -- date(日期)
- 4 -- boolean(布爾值)
- 5 -- error(錯誤)
- # 獲取某一個單元格的數(shù)據(jù)
- # 比如:獲取第2行第1列的單元格的數(shù)據(jù)
- one_cell = sheet.cell(1, 0)
- # 單元格的值
- cell_value = one_cell.value
- print("單元格的值為:", cell_value)
- # 單元格數(shù)據(jù)類型
- cell_type = one_cell.
- print("單元格數(shù)據(jù)類型為:", cell_type)
最后,如果要獲取當(dāng)前 Sheet 所有單元格中的數(shù)據(jù),可以通過遍歷所有行、列來操作
- # 獲取所有單元格的值
- print('表格中所有數(shù)據(jù)如下:')
- for r in range(sheet.nrows):
- for i in range(sheet.ncols):
- print(sheet.cell(r, i).value)
4. xlwt 寫入 Excel
如果想實現(xiàn)將數(shù)據(jù)寫入到 Excel 中,xlwt 就很方便了
首先,使用 xlwt 的 Workbook() 方法創(chuàng)建一個工作簿對象
然后,使用工作簿對象的 add_sheet(sheetname) 方法新增 Sheet
- import xlwt
- sheetname = '第一個Sheet'
- # 創(chuàng)建一個工作簿對象
- wb = xlwt.Workbook()
- # 添加Sheet,通過sheet名稱
- sheet = wb.add_sheet(sheetname)
接著,通過 sheet 對象的 write() 方法,按照行索引和列索引,將數(shù)據(jù)寫入到對應(yīng)單元格中去
- # 將數(shù)據(jù)寫入到Sheet中
- # 3個參數(shù)分別是:行索引(從0開始)、列索引(從0開始)、單元格的值
- # 第一行第一列,寫入一個數(shù)據(jù)
- # 寫入標(biāo)題
- for index, title in enumerate(self.titles):
- sheet.write(0, index, title)
- # 寫入值
- for index_row, row_values in enumerate(self.values):
- for index_column, column_value in enumerate(row_values):
- sheet.write(index_row + 1, index_column, column_value)
需要注意的是,最后必須調(diào)用工作簿的 save(filepath),才能在本地生成 Excel 文件
- # 保存文件
- # 最后保存文件即可
- wb.save(filepath)
5. 進階用法
接下來,聊聊幾個常用的進階用法
1、獲取所有可見的 Sheet
在讀取 Sheet 數(shù)據(jù)時,經(jīng)常需要過濾隱藏的 Sheet
當(dāng) sheet 對象的 visibility 屬性值為 0 時,代表此 Sheet 在工作簿中是顯示的;否則被隱藏了
- def get_all_visiable_sheets(self, wb):
- """
- 獲取所有可見的sheet
- :param wb:
- :return:
- """
- return list(filter(lambda item: item.visibility == 0, wb.sheets()))
- # 1、獲取所有可看見的sheet
- sheet_visiable = self.get_all_visiable_sheets(wb)
- print('所有可見的sheet包含:', sheet_visiable)
2、獲取 Sheet 可見行或列
某一個 Sheet 中,可能存在部分行、列被隱藏了
- def get_all_visiable_rows(self, sheet):
- """
- 獲取某一個sheet中,可見的行
- :param sheet:
- :return:
- """
- result = [index for index in range(sheet.nrows) if sheet.rowinfo_map[index].hidden == 0]
- return result
- def get_all_visiable_columns(self, sheet):
- """
- 獲取某一個sheet中,可見的列
- :param sheet:
- :return:
- """
- result = [index for index in range(sheet.ncols) if sheet.colinfo_map[index].hidden == 0]
- return result
3、獲取單元格的樣式
以獲取單元格字體顏色和背景為例
- def get_cell_bg_color(self, wb, sheet, row_index, col_index):
- """
- 獲取某一個單元格的背景顏色
- :param wb:
- :param sheet:
- :param row_index:
- :param col_index:
- :return:
- """
- xfx = sheet.cell_xf_index(row_index, col_index)
- xf = wb.xf_list[xfx]
- # 字體顏色
- font_color = wb.font_list[xf.font_index].colour_index
- # 背景顏色
- bg_color = xf.background.pattern_colour_index
- return font_color, bg_color
需要注意的是,使用 xlrd 讀取單元格的樣式,打開工作簿的時候需要顯式定義 formatting_info = True,否則會拋出異常
- # 注意:必須設(shè)置formatting_info=True,才能正常獲取屬性
- wb = xlrd.open_workbook(file_path, formatting_info=True)
- sheet = wb.sheet_by_index(0
6. 最后搭配使用 xlrd、xlwt,基本上能完成大部分的工作,對于一些復(fù)雜的功能,比如:復(fù)制、分割、篩選等功能,可以用上 xlutils 這個依賴庫
需要指出的是,這個組合對 xlsx 的兼容性不太好;如果需要操作 xlsx 文件,需要先轉(zhuǎn)為 xls,然后再進行