首页 > 编程语言 > 详细

python处理excel

时间:2021-02-15 23:41:22      阅读:21      评论:0      收藏:0      [点我收藏+]

python操作excel

先安装 openpyxl 模块

文件的创建

from openpyxl import Workbook	#导入模块

#创建
wb = Workbook()     #creat an excel file in RAM 
sheet = wb.active
print(sheet.title)
sheet.title = "Hello"	#表单名叫做Hello
wb.save("excel_test.xlsx")	#保存

向文件中写数据

from openpyxl import Workbook	

wb = Workbook()     #creat an excel file in RAM 
sheet = wb.active
print(sheet.title)
sheet.title = "Hello"	#表单名叫做Hello

sheet["B5"] = "ZLP"
sheet["B7"] = "FYF"
sheet.append(["fff","MLT","CZP","CHN"])

wb.save("excel_test.xlsx")	#保存

遍历单元格

from openpyxl import load_workbook		#导入模块
wb = load_workbook("name.xlsx")		#打开表格

print(wb.sheetnames)	#打印出所有的sheet
print(wb.get_sheet_names())
sheet = wb.get_sheet_by_name("Sheet1")		#获取指定的Sheet

print(sheet["B5"].value)		#打印B5单元格的内容


# 获取指定单元格的长度
# for cell in sheet["D2:D10"]:    
#     print(cell[0].value)

#获取所有单元格
# for row in sheet:
#     for cell in row:
#         print(cell.value,end=",")
#     print()

#获取指定行列的单元格内容
# for row in sheet.iter_rows(min_row=4,max_row=20,max_col=4):
#     for cell in row:
#         print(cell.value,end=",")
#     print()

#按列循环
# for column in sheet.columns:
#     for cell in column:
#         print(cell.value,end=",")
#     print()

#指定列循环
# for column in sheet.iter_cols(min_col=1,max_col=1,min_row=2,max_row=20):
#     for cell in column:
#         print(cell.value,end=",")
#     print()

给excel表数据设置各种样式

个人觉得暂时没有什么用处

python处理excel

原文:https://www.cnblogs.com/zhailipu/p/14405602.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!