当前位置 : 主页 > 编程语言 > python >

python3 操作excel

来源:互联网 收集:自由互联 发布时间:2022-09-02
一: 安装依赖库 pip3 install openpyxl 二:创建新文件 from openpyxl import Workbook import time book = Workbook() sheet = book.active sheet['A1'] = 11 sheet['A2'] = 22 now = time.strftime("%x") sheet['A3'] = now book.save("demo.xls

一: 安装依赖库

pip3 install openpyxl

二:创建新文件

from openpyxl import Workbook
import time

book = Workbook()
sheet = book.active

sheet['A1'] = 11
sheet['A2'] = 22

now = time.strftime("%x")
sheet['A3'] = now

book.save("demo.xlsx")

三:通过​​cell()​​多行写入

from openpyxl import Workbook

book = Workbook()
sheet = book.active

sheet.cell(row=2, column=2).value = 2

book.save('demo.xlsx')

四:读取单元格

import openpyxl

book = openpyxl.load_workbook('demo.xlsx')

sheet = book.active

a1 = sheet['A1']
a2 = sheet['A2']
a3 = sheet.cell(row=3, column=1)

print(a1.value)
print(a2.value)
print(a3.value)

 



上一篇:python3 生成MD5 方法
下一篇:没有了
网友评论