先安装 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()
个人觉得暂时没有什么用处
原文:https://www.cnblogs.com/zhailipu/p/14405602.html