The content shared with you in this article is how to operate excel in the python environment. It has certain reference value. Friends in need can refer to it
1. Available Third-party libraries
handle excel tables in python. Commonly used libraries include xlrd (read excel) tables, xlwt (write excel) tables, openpyxl (read and write excel tables), etc. xlrd is more efficient than openpyxl when reading excel tables with large data, so I used the two libraries xlrd and xlwt when writing scripts. None of these library files provide the function of modifying the content of existing excel tables. Generally, the content in the original excel can only be read out, processed, and then written into a new excel file.
You can use pip search excel
to check and you can see more development packages.
2. Frequently Asked Questions
When using python to process excel tables, I found two more difficult problems: unicode encoding and the time recorded in excel.
Because python's default character encoding is unicode, when printing Chinese read from excel or reading an excel table or sheet with Chinese names, the program prompts the error UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128). This is because in Windows, Chinese uses the gb2312 encoding method, and Python treats it as unicode and ascii to decode it, which is why it reports an error. Use VAR.encode(‘gb2312’) to solve the problem of printing Chinese. (It’s strange that sometimes the results can be printed out, but what is displayed is not Chinese, but a bunch of codes.) If you want to read data from an excel table with a Chinese file name, you can add 'u' before the file name to indicate The Chinese file name is encoded in unicode.
In excel, both time and date are represented by floating point numbers. It can be seen that when the cell of 'March 20, 2013' is expressed in the 'regular' format, the content becomes '41353'; when the cell format is changed to date, the content becomes 'March 2013' 20th'. After using xlrd to read the date and time in excel, a floating point number is obtained. So it doesn't matter if the date and time written to Excel are a floating point number. You only need to change the representation of the table to date and time to get the normal representation. In excel, use the floating point number 1 to represent December 31, 1899.
3. Commonly used functions
The following mainly introduces the date-related functions in xlrd, xlwt, and datetime.
import xlrdimport xlwtfrom datetimedef testXlrd(filename): book=xlrd.open_workbook(filename) sh=book.sheet_by_index(0) print "Worksheet name(s): ",book.sheet_names()[0] print 'book.nsheets',book.nsheets print 'sh.name:',sh.name,'sh.nrows:',sh.nrows,'sh.ncols:',sh.ncols print 'A1:',sh.cell_value(rowx=0,colx=1) #如果A3的内容为中文 print 'A2:',sh.cell_value(0,2).encode('gb2312')def testXlwt(filename): book=xlwt.Workbook() sheet1=book.add_sheet('hello') book.add_sheet('word') sheet1.write(0,0,'hello') sheet1.write(0,1,'world') row1 = sheet1.row(1) row1.write(0,'A2') row1.write(1,'B2') sheet1.col(0).width = 10000 sheet2 = book.get_sheet(1) sheet2.row(0).write(0,'Sheet 2 A1') sheet2.row(0).write(1,'Sheet 2 B1') sheet2.flush_row_data() sheet2.write(1,0,'Sheet 2 A3') sheet2.col(0).width = 5000 sheet2.col(0).hidden = True book.save(filename)if __name__=='__main__': testXlrd(u'你好。xls') testXlwt('helloWord.xls') base=datetime.date(1899,12,31).toordinal() tmp=datetime.date(2013,07,16).toordinal() print datetime.date.fromordinal(tmp+base-1).weekday()
Related recommendations:
Use Python to process Excel files
Python processing Excel modules
Comparison of various operations of excel modules in python
The above is the detailed content of How to operate excel in python environment. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools