• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

python3 操作excel

开发技术 开发技术 5小时前 2次浏览

一: 安装依赖库

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 操作excel
喜欢 (0)