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!