python 读取excle ,使用openpyxl
1.安装
pip3 install openpyxl
2.读取
from openpyxl import load_workbook
path = input('excle路径')
print(path)
workbook = load_workbook(path)
worksheet = workbook.active
i = 1
for item in worksheet:
i += 1
str2 = 'B' + str(i)
# 当前cell的数据
print(worksheet[str2].value)
3.写入
from openpyxl import load_workbook
path = input('excle路径')
print(path)
workbook = load_workbook(path)
worksheet = workbook.active
i = 1
arr = [
{
"id": 2126,
"title": "标题"
}
]
for item in arr:
i += 1
str1 = 'A' + str(i)
str2 = 'B' + str(i)
# print(item)
# print(str2)
worksheet[str1] = item['title']
worksheet[str2] = 'http://www.baidu.com?vid=' + str(item['id'])
# 保存文件
workbook.save(path)
网友评论