前面介紹了利用Python中xlrd與xlwt讀寫Excel的基本操作( 《使用Python xlrd與xlwt模塊讀寫Excel》)衅金,這兩個模塊可以很方便地對Excel進行處理,非常實用禁悠。但由于xlwt只支持Excel2007之前的版本耘沼,所以無法生成.xlsx后綴的文件魁衙,而且其單個sheet支持的最大行數為65535羡忘,數據量一旦超過限制行數么鹤,會遇到如下錯誤
ValueError: row index was 65536, not allowed by .xls format
這是xlwt很大的短板,遇到這種情況只能用其他包代替棋返,openpyxl 是一個不錯的選擇延都,它可用于讀寫Excel2010 xlsx/xlsm/xltx/xltm 文件,同時支持 Pandas 和 NumPy 等包懊昨,能夠繪制圖表窄潭,并且同樣支持格式控制等春宣,詳細文檔可以參考:
https://openpyxl.readthedocs.io/en/default/index.html#
下面開始介紹它的一些基本操作:
首先通過pip安裝
pip install openpyxl
或者手動安裝:https://pypi.python.org/pypi/openpyxl
Excel的讀取
#導入openpyxl package
import openpyxl
#打開一個已有工作簿
f = openpyxl.load_workbook('test.xlsx')
sheet相關操作
#獲取sheet名稱列表
name_list = f.sheetnames
name_list = f.get_sheet_names()
#通過名稱調用sheet
table = f['name']
table = f.get_sheet_by_name('name')
#通過檢索調用sheet
table = f.get_sheet_by_name(sheet_names[index])
#調用正在運行的sheet酵颁,一般為最后一個
table = f.active
#改變sheet的名字
table.title = 'newname'
單元格相關操作
#讀取單元格或范圍切片
c = table['A1'] #獲取'A1',返回class
c = table.cell(row = 1,column = 1) #按行列數獲讀取
row5 = table[5] #獲取第5行月帝,返回元組
colA = table['C'] #獲取C列
cell_range = table['A1':'B4'] #獲取范圍切片元組
row_range = table[5:8] #5到8行
col_range['A:D'] #A到D列
#按行列數讀取范圍切片
for row in table.iter_rows(min_row=1, max_col=3, max_row=2): #按行讀取
for col in table.iter_cols(min_row=1, max_col=3, max_row=2): #按列讀取
table.max_row #最大行數
table.max_column #最大列數
table.rows #按行遍歷
table.columns #按列遍歷
#讀取單元格的值
c = table['A2'].value #按位置讀取
c = table.cell(row = 1,column = 1).value #按行列數讀取
需要注意的是openpyxl中按行列數檢索時參數名‘row =’及‘column =’ 不能省略躏惋,而且均從1開始計數,這與xlrd有所不同嚷辅。
一個例子
利用openpyxl讀出圖1所示表中的一些信息簿姨,代碼及結果如下:
[圖片上傳失敗...(image-ad4b83-1569340188843)] 圖1.成績單
import openpyxl #導入openpyxl
f = openpyxl.load_workbook('demo.xlsx') #打開工作簿
print(f.sheetnames) #打印sheet名稱列表
table = f['成績單'] #調用成績單sheet
print(table.max_column) #打印最大列數
print(table.cell(row = 3,column = 3).value) #打印C3的值
for i in table['A']: #依次打印A列的值
print(i.value)
運行結果如下:
[‘名單’, ‘成績單’]
6
80
姓名
None
小明
小紅
李華
openpyxl在讀取合并格時也將其值當做左上角單元格的值,剩余單元格則返回None,而在xlrd中其他單元格則為empty: ‘’,有所區(qū)別扁位。
Excel的創(chuàng)建及更改
#導入openpyxl package
import openpyxl
#創(chuàng)建一個工作簿
f = openpyxl.Workbook()
每次新建一個工作簿會默認生成一個名稱為“Sheet1”的工作表准潭,可以先將其利用起來,操作與前面相同域仇,比如
table = f.active
#or
table = f['Sheet1']
當需要更多的工作表時刑然,可以創(chuàng)建新的sheet
#創(chuàng)建sheet
table = f.create_sheet('AD') #創(chuàng)建并插入末尾
table = f.create_sheet('AD',0) #創(chuàng)建并插入首位
#可以通過RGB色值設置sheet標簽顏色
table.sheet_properties.tabColor = 'RRGGBB'
#復制一個工作表
target = f.copy_worksheet(table)
寫入數據
table['A2'] = 4 #單元格寫入
table['A1'] = '=SUM(1,1)' #按公式寫入
#按行列數寫入
table.cell(row = 3,column = 1,value = 10)
table.cell(row = 3,column = 1).value = 10
#單元格合并與分解
table.merge_cells('A1:B1') #按位置
table.unmerge_cells('A1:B1')
table.merge_cells(star_row = 2,star_column = 1,end_row = 2,end_column = 4) #按行列號
table.unmerge_cells(start_row = 2,star_column = 1,end_row = 2,end_column = 4)
#插入圖片
img = openpyxl.drawing.image.Image('demo.png')
table.add_image(img,'A1')
#保存文件
f.template = True #存為模板
f.save('demo.xlsx', as_template = False)
openpyxl的寫入語法與xlwt差別不大,主要區(qū)別在于它對單元格的定位主要根據坐標進行暇务,如 ‘A5’,‘C3’泼掠,而xlrd則完全依靠單元格的行列位置,如cell(0,4),cell(2,2)垦细。除了常規(guī)的讀寫操作择镇,openpyxl也提供了完善的格式控制的方法,下面進行簡單介紹括改。
單元格的格式控制
openpyxl中提供的Style方法用于調整表格的外觀腻豌,如字體格式、邊框嘱能、對齊方式饲梭、單元格背景、保護等焰檩。
字體格式:
# 導入相關模塊
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
#字體格式
font0 = Font(name='Calibri',
size=11,
bold=False,
italic=False,
vertAlign=None, #Maybe:'baseline', 'superscript', 'subscript'
underline='none', #Maybe:'single','double','singleAccounting','doubleAccounting'
strike=False,
color='FF00FF00')
#單元格填充
fill0 = PatternFill(fill_type=None,
#Maybe:'lightUp', 'darkVertical', 'darkGrid', 'solid', 'darkHorizontal', 'darkUp', 'lightVertical', 'lightGray', 'darkTrellis', 'lightDown', 'gray125', 'gray0625', 'mediumGray', 'lightTrellis', 'darkGray', 'darkDown', 'lightHorizontal', 'lightGrid'
start_color='FFFFFFFF',
end_color='FF000000')
#邊框
border0 = Border(left=Side(border_style=None,color='FF000000'),
#style Maybe:'mediumDashDotDot', 'dotted', 'thick', 'medium', 'dashDotDot', 'double', 'dashed', 'mediumDashed', 'dashDot', 'mediumDashDot', 'hair', 'slantDashDot', 'thin'
right=Side(border_style=None,color='FF000000'),
top=Side(border_style=None,color='FF000000'),
bottom=Side(border_style=None,color='FF000000'),
diagonal=Side(border_style=None,color='FF000000'),
diagonal_direction=0,
outline=Side(border_style=None,color='FF000000'),
vertical=Side(border_style=None,color='FF000000'),
horizontal=Side(border_style=None,color='FF000000')
)
#對齊方式
alignment0 = Alignment(horizontal='general', #Maybe:'centerContinuous', 'fill', 'right', 'distributed', 'justify', 'general', 'center', 'left'
vertical='bottom',
text_rotation=0,
wrap_text=False,
shrink_to_fit=False,
indent=0)
#表格保護
protection0 = Protection(locked=True,
hidden=False)
格式應用:
# 導入相關模塊
from openpyxl.styles import NamedStyle
# 創(chuàng)建格式
style0 = NamedStyle(name = 'style_example')
#格式賦值
style0.font = font0
style0.alignment = alignment0
style0.border = border0
style0.fill = fill0
style0.Protection = protection0
#格式調用
#單屬性調用
table['A1'].font = font0
table['A1'].alignment = alignment0
table.cell(row = 1,column = 1).border = border0
#按名稱調用
table['A1'].style = style0
table['A1'].style = 'style_example'
table.cell(row = 1,column = 1).style = style0
由上可知憔涉,openpyxl提供的格式控制方法可以實現對單元格屬性所有基本操作。下面還是以實現圖 1 所示表格為例進行演示析苫。
第二個例子
import openpyxl
from openpyxl.styles import NamedStyle, Border, Side, Alignment
# 生成工作簿及sheet
f = openpyxl.Workbook()
table = f.active
table.title = '成績單'
# 信息列表
subject_list = ['語文','思想品德','數學','科學']
info_list = [
['小明',22,80,85,90,77],
['小紅',23,91,88,95,90],
['李華',24,75,70,98,100]
]
# 創(chuàng)建表頭
table.merge_cells('A1:A2')
table.cell(row = 1,column = 1,value = '姓名')
table.merge_cells('B1:B2')
table.cell(row = 1,column = 2,value = '學號')
table.merge_cells('C1:D1')
table.cell(row = 1,column = 3,value = '文科')
table.merge_cells('E1:F1')
table.cell(row = 1,column = 5,value = '理科')
for i in range(4):
table.cell(row = 2,column = i+3,value = subject_list[i])
# 寫入信息
for obs in range(3):
for info in range(6):
table.cell(row = obs+3,column = info+1,value = info_list[obs][info])
# 設置單元格格式兜叨,其中字體及背景為默認
style0 = NamedStyle('style0')
style0.border = Border(left=Side(border_style='thin'),
right=Side(border_style='thin'),
top=Side(border_style='thin'),
bottom=Side(border_style='thin'))
style0.alignment = Alignment(horizontal='center',vertical='center',)
# 格式應用
for row in table.rows:
for cell in row:
cell.style = style0
# 保存文件
f.save("score.xlsx")
生成結果如下:
圖2.使用openpyxl生成的成績單
??對比后發(fā)現,使用openpyxl做出表格的效果與office界面制作的完全相同衩侥。當然国旷,若只是制作類似簡單的表格,寫代碼的效率顯然不如office界面操作高茫死,編程處理表格的優(yōu)勢在于處理人工難以完成的工作跪但,下期將分享兩個用Python處理Excel的實例。