操作excel

一、写一个excel

# -*- coding: utf-8 -*-

import locale

from openpyxl import Workbook

wb = Workbook()    #创建文件对象

# grab the active worksheet

ws = wb.active    #获取第一个sheet

# Data can be assigned directly to cells

ws['A1'] = 42      #写入数字

ws['B1'] = "光荣之路"+"automation test" #写入中文

# Rows can also be appended

ws.append([1, 2, 3])    #写入多个单元格

#保存到本地

wb.save("e:\\a.xlsx")


二、写入时间

import locale

from openpyxl import Workbook

import datetime

import time

import locale

wb = Workbook()

ws = wb.active 

ws['A2'] = datetime.datetime.now()    #写入一个当前时间

#写入一个自定义的时间格式

locale.setlocale(locale.LC_CTYPE, 'chinese')

ws['A3'] =time.strftime("%Y年%m月%d日 %H时%M分%S秒",

time.localtime())

# Save the file

wb.save("e:\\sample.xlsx")


三、设置单元格格式

实例一:新增两个sheet页面

from openpyxl import Workbook

wb = Workbook()

ws = wb.create_sheet("Mysheet1")

ws1 = wb.create_sheet("Mysheet")

# Save the file

wb.save("e:\\sample.xlsx")

实例二:

from openpyxl import Workbook

wb = Workbook()

ws = wb.create_sheet("Mysheet1")

ws1 = wb.create_sheet("Mysheet")

#改名

ws1.title = "New Title"

ws2 = wb.create_sheet("Mysheet", 0)        #设定sheet的插入位置

ws2.title = u"光荣之路自动化测试培训"

ws1.sheet_properties.tabColor = "1072BA"

#获取某个sheet对象

print (wb["光荣之路自动化测试培训"])

print (wb["New Title" ])

print (wb.sheetnames)

for sheet_name in wb.sheetnames:

    print (sheet_name)

    print(wb[sheet_name])

print("*"*50)

#遍历所有的对象

for sheet in wb:

print (sheet)

#遍历所有对象的名字

for sheet in wb:

    print (sheet.title)

#复制一个sheet

wb["New Title" ]["A1"]="gloryroad"

source = wb["New Title" ]

target = wb.copy_worksheet(source)

target.title="New copy Title"

#删除sheet

del wb["New Title" ]

# Save the file

wb.save("e:\\sample.xlsx")

备注:对象和名字区别:

对象:整个sheet里面的所有单元格数据

名字:sheet的名字


四、操作单元格

# -*- coding: utf-8 -*-

#读取单元格数据

from openpyxl import Workbook

wb = Workbook()

ws1 = wb.create_sheet("Mysheet")           #创建一个sheet

ws1["A1"]=123.11

ws1["B2"]="光荣之路"

d = ws1.cell(row=4, column=2, value=10)

print (ws1["A1"].value)

print (ws1["B2"].value)

print (d.value)

print (ws1.cell(row=4,column=2).value)

# Save the file

wb.save("e:\\sample.xlsx")

五、批量操作单元格

# -*- coding: utf-8 -*-

from openpyxl import Workbook

wb = Workbook()

ws1 = wb.create_sheet("Mysheet")          #创建一个sheet

ws1["A1"]=1

ws1["A2"]=2

ws1["A3"]=3

ws1["B1"]=4

ws1["B2"]=5

ws1["B3"]=6

ws1["C1"]=7

ws1["C2"]=8

ws1["C3"]=9

#操作单列

print (ws1["A"])

for cell in ws1["A"]:

    print (cell.value)

#操作多列,获取每一个值

print (ws1["A:C"])

for column in ws1["A:C"]:

    for cell in column:

        print (cell.value)

#最大行号和列号

print(ws1.max_row,ws1.max_column)

#最小行号和列号

print(ws1.min_row,ws1.min_column)

#操作多行

print ("*"*50)

for row in ws1.iter_rows(min_row=1, min_col=1,

max_col=3, max_row=3):

    for cell in row:

        print (cell.value)

# Save the file

wb.save("e:\\sample.xlsx")


六、获取所有行和所有列

# -*- coding: utf-8 -*-

from openpyxl import Workbook

wb = Workbook()

ws1 = wb.create_sheet("Mysheet")          #创建一个sheet

ws1["A1"]=1

ws1["A2"]=2

ws1["A3"]=3

ws1["B1"]=4

ws1["B2"]=5

ws1["B3"]=6

ws1["C1"]=7

ws1["C2"]=8

ws1["C3"]=9

#获取所有行

print (ws1.rows)

for row in ws1.rows:

    print (row)

print ("*"*50)

#获取所有列

print (ws1.columns)

for col in ws1.columns:

    print (col)

# Save the file

wb.save("e:\\sample.xlsx")


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用openpyxl操作excel表格 openpyxl 是一个用来操作excel 2010 的xlsx/xlsm的...
    stone46阅读 12,168评论 0 17
  • 最近在网上爬取奥运项目资料,并写入Excel中。在写到Excel中是用到了OpenPyXL,翻译了一部分自己用到的...
    LeeLom阅读 201,222评论 7 78
  • 翠笋洗净备用,肉煮熟备用 热油锅入蒜末、红泡椒、翻炒,加容易熟肉、翠笋芹菜或蒜叶、盐和鸡精翻炒片刻出锅
    渡把阅读 1,459评论 0 0
  • 你有没有这样的经历,读一本书时轻松惬意,仿佛感触良多受益匪浅。但是时过境迁,某一天突然又捧起这本书回味一下,却发现...
    云淡风清漫步游阅读 3,865评论 0 1
  • 你有多久没玩游戏了? (一) “诶,小轩啊,你发我的那个报告我看了下,写得不错,就是还得再润色一下,嗯,你明白我意...
    MInho24阅读 2,885评论 0 3

友情链接更多精彩内容