SoFunction
Updated on 2024-11-15

How to process data in excel sheet with Python

I. Foundations, common methods

1. Reading excel

1. Import module:

import xlrd

2. Open the file:

x1 = xlrd.open_workbook("")

3. Get the sheet:

sheet is the name of the worksheet, because an excel has more than one worksheet


Get all sheet names: x1.sheet_names()

Get the number of sheets:

Get all sheet objects: ()

Find by sheet name: x1.sheet_by_name("test")

Find by index: x1.sheet_by_index(3)

# -*- coding:utf-8 -*-

import xlrd
import os

filename = ""
filePath = ((), filename)

print filePath

# 1. Open the file
x1 = xlrd.open_workbook(filePath)

# 2, get sheet object
print 'sheet_names:', x1.sheet_names()  # Get all sheet names
print 'sheet_number:',         # Get the number of sheets
print 'sheet_object:', ()       # Get all sheet objects
print 'By_name:', x1.sheet_by_name("test")  # Find by sheet name
print 'By_index:', x1.sheet_by_index(3)  # Search by index

Output:

sheet_names: [u' plan', u'team building', u'modile', u'test']
sheet_number: 4
sheet_object: [< object at 0x10244c190>, < object at 0x10244c150>, < object at 0x10244c110>, < object at 0x10244c290>]
By_name: < object at 0x10244c290>
By_index: < object at 0x10244c290>

4. Get the summary data of the sheet:

Gets the sheet name:

Get the total number of rows:

Get the total number of columns:

# -*- coding:utf-8 -*-

import xlrd
import os
from datetime import date,datetime

filename = ""
filePath = ((), filename)
print filePath

# Open the file
x1 = xlrd.open_workbook(filePath)

# Get summary data for the sheet
sheet1 = x1.sheet_by_name("plan")
print "sheet name:",    # get sheet name
print "row num:",   # get sheet all rows number
print "col num:",   # get sheet all columns number

Output:

sheet name: plan
row num: 31
col num: 11

Information.https:///article/

https:///article/

II. Improvement

III. Errors

1.Cannot open .xlsx file pandas cannot open .xlsx file,: Excel xlsx file; not supported

Installed version too high, low version support

You can install the old version of xlrd and run it in cmd:

pip uninstall xlrd
pip install xlrd==1.2.0

It is also possible to open .xlsx files with openpyxl instead of xlrd:

df=pandas.read_excel(‘',engine=‘openpyxl')

summarize

To this article on how to use Python to deal with excel table data in the article is introduced to this, more related Python to deal with excel data content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!