6

Python解析xlsx文件

 1 year ago
source link: https://viencoding.com/article/298
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python解析xlsx文件

2021-05-13 03:41:04   Python

xlrd可以解析普通Excel文件比如xls、csv等,但是不支持xlsx解析,因此我们需要借助另外一个库openpyxl,openpyxl可以支持xlsx解析

pip install openpyxl
from openpyxl import load_workbook

# 打开一个workbook
wb = load_workbook(filename="data.xlsx")

# 获取所有表格(worksheet)的名字
sheets = wb.sheetnames

# 遍历素有表格
for sheet in sheets:
    print(sheet)
    # 根据名字获取表格
    ws = wb[sheet]

    # 获取表格所有行和列,两者都是可迭代的
    rows = ws.rows
    columns = ws.columns

    # 迭代获取每一行
    for idx, row in enumerate(rows):
        line = [col.value for col in row]
        print(line)

viencoding.com版权所有,允许转载,但转载请注明出处和原文链接: https://viencoding.com/article/298 欢迎小伙伴们在下方评论区留言 ~ O(∩_∩)O
文章对我有帮助, 点此请博主吃包辣条 ~ O(∩_∩)O


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK