美文网首页
python读取excle

python读取excle

作者: ONE2 | 来源:发表于2021-03-23 14:45 被阅读0次

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)

相关文章

网友评论

      本文标题:python读取excle

      本文链接:https://www.haomeiwen.com/subject/kkdbcltx.html