python读取excel中文_python如何读取excel

编程知识 更新时间:2023-04-04 10:14:31

python 读取excel可分为以下几步:首先安装excel库xlrd,然后获取excel文件位置并读取,最后读取sheet和指定的rows和cols内容

今天将要介绍的是如何通过Python语言来读取excel文件,具有一定的参考作用,希望对大家有所帮助。

Python语言来读取excel文件,分为以下几个操作步骤:

(1)首先安装python官方Excel库-->xlrd

(2)获取Excel文件位置并读取

(3)读取sheet

(4)读取指定rows和cols内容

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

import xlrd

from datetime import date,datetime

def read_excel():

#文件位置

ExcelFile=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\TestData.xlsx')

#获取目标EXCEL文件sheet名

print ExcelFile.sheet_names()

#------------------------------------

#若有多个sheet,则需要指定读取目标sheet例如读取sheet2

#sheet2_name=ExcelFile.sheet_names()[1]

#------------------------------------

#获取sheet内容【1.根据sheet索引2.根据sheet名称】

#sheet=ExcelFile.sheet_by_index(1)

sheet=ExcelFile.sheet_by_name('TestCase002')

#打印sheet的名称,行数,列数

print sheet.name,sheet.nrows,sheet.ncols

#获取整行或者整列的值

rows=sheet.row_values(2)#第三行内容

cols=sheet.col_values(1)#第二列内容

print cols,rows

#获取单元格内容

print sheet.cell(1,0).value.encode('utf-8')

print sheet.cell_value(1,0).encode('utf-8')

print sheet.row(1)[0].value.encode('utf-8')

#打印单元格内容格式

print sheet.cell(1,0).ctype

if__name__ =='__main__':

read_excel()

总结:以上就是本篇文章的全部内容了,希望对大家有所帮助。

更多推荐

python读取excel中文_python如何读取excel

本文发布于:2023-04-04 10:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/26a4e03a7f54f02a675dc501b5f41dbf.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中文   python   excel   _python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 42080文章数
  • 14阅读数
  • 0评论数