openpyxl 3.0.9
工作簿-
创建工作薄:
from openpyxl import workbook
wb = workbook.Workbook() -
获取工作簿:
wb =openpyxl.load_workbook(“”); -
保存工作簿:
wb.save("./第一个工作簿.xlsx ")
-
工作表(sheet,ws)
-
创建工作表:
wb.create_sheet(title=‘str’,index=int) #索引从零开始 -
获取工作表:
sheet=wb.get_sheet_by_name(“”);
sheet=wb[‘str’] -
修改表名:
wb.active.title=“filename”
wb[‘str’].title=“filename” -
删除工作表:
del wb[‘str’] -
所有工作表以列表形式返回,输出到屏幕上
print(wb.sheetnames)
-
单元格:(cell) : value,row,column,coordinate(坐标)
-
单元格 *** 作:
wb[‘str’][‘单元格位置’]=‘str’
sheet=wb[‘str’]
sheet=wb.active
sheet[‘A1’]=‘hello’ (单元格内容进行赋值)
sheet.cell(row=,column=).value -
取值:
.value -
单元格的值: row,column从一开始
sheet.cell(row=rowNum,column=columnNum).value -
字典:
Dict_name = {3: 500, 2: 400, 1: 200} 键值对(int:int,str:str,int:str,str:int)
Talent=sheet.cell(row=rowNum,column=10).value
if Talent in Dict_name:
sheet.cell(row=rowNum,column=12).value=Dict_name[Talent] -
取键:dict[键]
- 遍历:(变量也可以是:row column)
- 行遍历取元素:
wb=openpyxl.load_workbook(‘大学生排名2016.xlsx’)
sheet=wb.get_sheet_by_name(‘Sheet1’) - sheet
for rowNum in range(2,sheet.max_row+1): //加一才能访问到最后一个元素。
for columnNum in range(1,sheet.max_column+1):
-
单元格风格样式:
from openpyxl.styles import Font
sheet.cell(row=rowNum, column=4).font = Font(name=‘楷体’, size=18,color=‘8470FF’) -
单元格背景填充色:
from openpyxl.styles import PatternFill
sheet.cell(row=rowNum, column=7).fill=PatternFill(patternType=‘solid’,fgColor=‘8470FF’) -
单元格对齐方式:
from openpyxl.styles import Alignment
sheet.cell(row=rowNum, column=5).alignment=Alignment(horizontal=‘center’) -
单元格边框样式:
from openpyxl.styles import Side,Border
s1=Side(style=‘double’,color=‘ff0000’) #style dotted(点状) solid(实线) double(双线) dashed(虚线)
sheet.cell(row=rowNum, column=6).border=Border(top=s1,right=s1,left=s1,bottom=s1)
-
添加筛选器:
sheet.auto_filter.ref=‘A1:M311’
sheet.auto_filter.add_filter_column(2,[‘北京市’,‘天津市’]) -
添加筛选条件:
sheet.auto_filter.add_sort_condition(ref=‘I1:I311’,descending=True) //descending默认降序 -
设置行高:
sheet.row_dimensions[3].heigth = 50 0~409 1=1/71英寸 默认12.75 -
设置列宽:
sheet.column_dimensions[‘列号’].width=10 0~255 默认8.43个字符 -
批量更改宽高:
sheet.column_dimensions[openpyxl.utils.get_column_letter(rowNum)].width =16
sheet.row_dimensions[i].heigth=height -
合并单元格:
sheet.merge_cells(“A1:D7”)
sheet[‘A1’]=“Hello” -
拆分单元格:
unmerge_cells(“A1:D7”)
import openpyxl
from openpyxl.styles import Font, Alignment,Side,Border,PatternFill
wb=openpyxl.load_workbook("E:\Pycharm\PycharmProjects\OnePython\第一个工作簿.xlsx")
# sheet=wb.get_sheet_by_name("进度表")
# sheet.title="学校排名表"
sheet=wb.get_sheet_by_name('进度表')
s1=Side(style='dashed',color='ff0000')
Dict_name = {3: 500, 2: 400, 1: 200}
for rowNum in range(2,sheet.max_row+1):
for columnNum in range(1, sheet.max_column + 1):
sheet.column_dimensions[openpyxl.utils.get_column_letter(rowNum)].width =16
sheet.row_dimensions[rowNum].heigth = 20
Talent=sheet.cell(row=rowNum,column=10).value
if Talent in Dict_name:
sheet.cell(row=rowNum,column=12).value=Dict_name[Talent]
School=sheet.cell(row=rowNum,column=2).value
sheet.cell(row=rowNum, column=columnNum).font = Font(name='楷体', size=10,color='8470FF')
sheet.cell(row=rowNum, column=columnNum).border = Border(top=s1, right=s1, left=s1, bottom=s1)
sheet.cell(row=rowNum, column=columnNum).alignment = Alignment(horizontal='center')
for columnNum in range(1,sheet.max_column+1):
sheet.cell(row=1, column=columnNum).fill = PatternFill(patternType='solid', fgColor='8470FF')
sheet.auto_filter.ref='A1:M311'
sheet.auto_filter.add_filter_column(2,['北京市','天津市'])
sheet.auto_filter.add_sort_condition(ref='I1:I311',descending=False)
wb.save("E:\Pycharm\PycharmProjects\OnePython\第一个工作簿.xlsx ")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)