国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python辦公自動化之excel的操作

瀏覽:3日期:2022-06-18 16:20:43
目錄準(zhǔn)備xlrd 讀取 Excelxlwt 寫入 Excel進(jìn)階用法最后準(zhǔn)備

使用 Python 操作 Excel 文件,常見的方式如下:

xlrd / xlwt openpyxl Pandas xlsxwriter xlwings pywin32

xlrd 和 xlwt 是操作 Excel 文件最多的兩個(gè)依賴庫

其中,

xlrd 負(fù)責(zé)讀取 Excel 文件,xlwt 可以寫入數(shù)據(jù)到 Excel 文件

我們安裝這兩個(gè)依賴庫

# 安裝依賴庫pip3 install xlrd pip3 install xlwt xlrd 讀取 Excel

使用 xlrd 中的 open_workbook(filepath) 打開本地一個(gè) Excel 文件

import xlrd# 打開文件,返回一個(gè)工作簿對象wb = xlrd.open_workbook(file_path)

工作簿對象的 nsheets 屬性獲取 Sheet 數(shù)目,sheet_names() 方法返回所有 Sheet 名稱的列表

​# 統(tǒng)計(jì)sheet數(shù)量sheets_num, sheets_names = wb.nsheets, wb.sheet_names()print(’sheet數(shù)量一共有:’, sheets_num)print(’sheet名稱分別為:’, sheets_names)

篩選出工作簿中的某一個(gè) Sheet 有 2 種方式,分別是:

通過 Sheet 名稱 使用位置索引,從 0 開始

# 獲取某一個(gè)sheet# 通過名稱或者索引獲取sheet = wb.sheet_by_index(0)# sheet = wb.sheet_by_name(’第一個(gè)Sheet’)print(sheet)

每一個(gè) sheet 對象都可以利用 name、nrows、ncols 獲取 Sheet 名稱、行數(shù)量、列數(shù)量

另外

row_values(index)、col_values(index) 分別用于獲取某一行或某一列的數(shù)據(jù)列表

# 獲取某一個(gè)sheet中,包含的行數(shù)量、列數(shù)量sheet_name, sheet_row_count, sheet_column_count = sheet.name, sheet.nrows, sheet.ncolsprint(’當(dāng)前sheet名稱為:’, sheet_name, ',一共有:', sheet_row_count, '行;有:', sheet_column_count, '列')# 單獨(dú)獲取某一行數(shù)據(jù),索引從0開始# 比如:獲取第2行數(shù)據(jù)row_datas = sheet.row_values(1)print(’第2行數(shù)據(jù)為:’, row_datas)# 單獨(dú)獲取某一列數(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(錯(cuò)誤)

# 獲取某一個(gè)單元格的數(shù)據(jù)# 比如:獲取第2行第1列的單元格的數(shù)據(jù)one_cell = sheet.cell(1, 0)# 單元格的值cell_value = one_cell.valueprint('單元格的值為:', 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)xlwt 寫入 Excel

如果想實(shí)現(xiàn)將數(shù)據(jù)寫入到 Excel 中,xlwt 就很方便了

首先,使用 xlwt 的 Workbook() 方法創(chuàng)建一個(gè)工作簿對象

然后,使用工作簿對象的 add_sheet(sheetname) 方法新增 Sheet

import xlwt​sheetname = ’第一個(gè)Sheet’# 創(chuàng)建一個(gè)工作簿對象wb = xlwt.Workbook()# 添加Sheet,通過sheet名稱sheet = wb.add_sheet(sheetname)

接著,通過 sheet 對象的 write() 方法,按照行索引和列索引,將數(shù)據(jù)寫入到對應(yīng)單元格中去

# 將數(shù)據(jù)寫入到Sheet中# 3個(gè)參數(shù)分別是:行索引(從0開始)、列索引(從0開始)、單元格的值# 第一行第一列,寫入一個(gè)數(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)進(jìn)階用法

接下來,聊聊幾個(gè)常用的進(jìn)階用法

1、獲取所有可見的 Sheet

在讀取 Sheet 數(shù)據(jù)時(shí),經(jīng)常需要過濾隱藏的 Sheet

當(dāng) sheet 對象的 visibility 屬性值為 0 時(shí),代表此 Sheet 在工作簿中是顯示的;否則被隱藏了

def get_all_visiable_sheets(self, wb): ''' 獲取所有可見的sheet :param wb: :return: ''' return list(filter(lambda item: item.visibility == 0, wb.sheets()))# 1、獲取所有可看見的sheetsheet_visiable = self.get_all_visiable_sheets(wb)print(’所有可見的sheet包含:’, sheet_visiable)

2、獲取 Sheet 可見行或列

某一個(gè) Sheet 中,可能存在部分行、列被隱藏了

def get_all_visiable_rows(self, sheet): ''' 獲取某一個(gè)sheet中,可見的行 :param sheet: :return: ''' result = [index for index in range(sheet.nrows) if sheet.rowinfo_map[index].hidden == 0] return resultdef get_all_visiable_columns(self, sheet): ''' 獲取某一個(gè)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): ''' 獲取某一個(gè)單元格的背景顏色 :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 讀取單元格的樣式,打開工作簿的時(shí)候需要顯式定義 formatting_info = True,否則會拋出異常

# 注意:必須設(shè)置formatting_info=True,才能正常獲取屬性wb = xlrd.open_workbook(file_path, formatting_info=True)sheet = wb.sheet_by_index(0)最后

搭配使用 xlrd、xlwt,基本上能完成大部分的工作,對于一些復(fù)雜的功能,比如:復(fù)制、分割、篩選等功能,可以用上 xlutils 這個(gè)依賴庫

需要指出的是,這個(gè)組合對 xlsx 的兼容性不太好;如果需要操作 xlsx 文件,需要先轉(zhuǎn)為 xls,然后再進(jìn)行

代碼地址:https://github.com/xingag/test_auto/tree/master/office_auto/Excel

以上就是python辦公自動化之excel的操作的詳細(xì)內(nèi)容,更多關(guān)于python excel自動化的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: python
相關(guān)文章:
主站蜘蛛池模板: 榆社县| 上饶市| 舞钢市| 乐昌市| 墨江| 葫芦岛市| 渝北区| 富宁县| 分宜县| 顺义区| 拉孜县| 吕梁市| 韩城市| 垣曲县| 上栗县| 山丹县| 昌乐县| 广昌县| 郴州市| 托里县| 乌兰浩特市| 恩施市| 辉县市| 靖州| 连南| 普陀区| 内乡县| 台湾省| 宝山区| 郎溪县| 平原县| 广宗县| 南通市| 榆中县| 留坝县| 建阳市| 丰顺县| 烟台市| 冕宁县| 胶州市| 双柏县|