1. 程式人生 > >python openpyxl基礎教程2.5.3

python openpyxl基礎教程2.5.3

   2.5.3的更新有很大的變化,很多api 已經過時了

 import openpyxl
wb=openpyxl.load_workbook(r"E:\pythion\電影250.xlsx")

type(wb)

wb.properties  表的資訊 建立日期 修改日期等  可以調出來得

wb.sheetnames  #返回所有的工作表名稱

['Sheet']

ws=wb.get_sheet_names() # 不能用了

ws=wb['Sheet'] #直接寫表名  簡潔了很多吧

news=wb.create_chartsheet(index=0,title="DBB_Demo")#新增表  可以簡化的

wb.remove_sheet(ws) # 這有事一個過時的方法

del wb['DBB_Demo2']  按名稱刪除

wb.remove(ws)           刪除表

刪除完要儲存一下表 才生效哦

c=ws['A2']           位置

c.row
2
>>> c.column
'A'
>>> c.coordinate     #座標
'A2'
>>> ws['A2'].value
'追風箏的人'
>>> ws['a2'].value
'追風箏的人'
>>> c.value

'追風箏的人'

d=c.offset(2,0) #偏移先行後 列 (左括號給提示

'AA1'是多少 26進位制   就是27

openpyxl.cell.cell.get_column_letter (27)
'AA'
>>> openpyxl.cell.cell.column_index_from_string('AA')

27

for each_book in ws['A2':'B10']:            #範圍訪問  each_book 代表一行的資訊  
for each_cell in each_book:

print(each_cell.value,end='  ')

print('\n')

___________________________

for each_row in ws.rows:         # each_row 表示所有行的資訊   

print(each_row[0].value)   #第一列的所有資訊

for each_row in ws.iter_rows(min_row=2,min_col=1,max_row=4,max_col=2):   #each_row指定範圍的所有資訊

print(each_row[0].value)                            #

new=wb.copy_worksheet(ws)                         #複製表
>>> wb.save(r"E:\pythion\電影250.xlsx")        #儲存表

——————————————————

表名顏色 行高 列寬

import openpyxl
>>> wb=openpyxl.Workbook()
>>> ws1=wb.create_chartsheet("鳳姐")

>>> ws2=wb.create_chartsheet("大B")

ws=wb.active            #想修改表 必須先啟用表 否則無法修改
>>> ws.sheet_properties.tabColor='FF0000'

>>> ws.row_dimensions [2].height=100

ws.column_dimensions['c'].width =50

>>> wb.save ("E:\pythion\excl02.xlsx")

ws.column_dimensions['c'].width =50

ws.merge_cells ("A1:C3")                      #合併單元格
>>> ws['A1']="大B老師gogogog"       
>>> ws.unmerge_cells ("A1:C3")            #取消合併
>>> ws.freeze_panes='B8'                    #凍結   B8為分界
>>> ws.freeze_panes=None                 #取消凍結

from openpyxl.styles import Font
>>> bold_red_font=Font(bold=True,color="00ff00")  #定義 字型型別

>>> b2.font=bold_red_font

b3=ws['B3']

>>> b3.value="大B老師你最棒"

italic_strike_blue_16font=Font(size=16,italic=True,strike=True,color="0000FF")
>>> b3.font=italic_strike_blue_16font

from openpyxl.styles import PatternFill            #單元格背景

yellow_fill = PatternFill(fill_type="solid",fgColor="FFFF00")    #單色

b2.fill=yellow_fill

from openpyxl.styles import GradientFill           #漸變背景色

red2green_fill=GradientFill(type="linear",stop=("FF0000","00FF00"))

b1.fill =red2green_fill

 from openpyxl.styles import Border,Side      #邊框設定

thin_side=Side(border_style="thin",color="000000")

double_side=Side(border_style="double",color="FF0000")    #雙紅線
>>> b1.border=Border(diagonal=thin_side,diagonalUp=True,diagonalDown=True)
>>> b2.border =Border(left=double_side,top=double_side,right=double_side,bottom=double_side)

from openpyxl.styles import Alignment      #對齊

center_aligment=Alignment(horizontal="center",vertical="center")
>>> ws['A1'].value="I Iove FishC.com"
>>> ws.merge_cells('A1:C2')
>>> wb.save ("E:\pythion\exc03.xlsx")

>>> ws['A1'].alignment=center_aligment

from openpyxl.styles import NamedStyle     #單元格樣式設定,為了方便使用
>>> highlight = NamedStyle(name="highlight")

>>> highlight=NamedStyle(name="highlight")

from openpyxl.styles import Font

highlight.font=Font(bold=True,size=20)
>>> highlight.alignment = Alignment(horizontal="center",vertical="center")

>>> wb.add_named_style(highlight)

ws['A1'].style =highlight
>>> ws["B5"].value ="Love"
>>> ws["B5"].style =highlight

>>> wb.save ("E:\pythion\exc03.xlsx")

ws["A1"]=88.8                                #單元格數字格式設定  自動加人民幣

>>> ws["A1"].number_format ="#,###.00人民幣"

import datetime
ws["A2"]=datetime.datetime.today()

>>> ws["A2"].number_format ='yyyy-mm-dd'    #日期格式

from openpyxl.styles .colors import RED,GREEN,BLUE,YELLOW        #匯入4種顏色關鍵字

ws['A1'].number_format ="[RED]+#,###.00;[GREEN]-#,###.00;[BLUE];[YELLOW]"   正;負;零;文字自動判斷對應4種格式

ws['A7'].number_format ="[=1]男;[=0]女"                                       判斷等於那種情況

ws['A8'].number_format ="[<60][RED]不及格;[>=60][GREEN]及格"     條件判斷