python reads and rewrites excel.
(Compare xlwt, openpyxl, xlrd)
- xlwt does not support writing xlsx files.
- Openpyxl does not support reading xls files.
- Scheduled task xlrd supports reading xls,xlsx files.
- Scheduled tasks are recommended to use xlrd for reading files and openpyxl for writing files.
# I, xlrd read # 1. Introduce the library & Download the library xlrd pip install xlrd # Download pip show xlrd # Show version pip install xlrd==1.2.0 # Download the specified version import xlrd # Import workBook = xlrd.open_workbook('D:\project\', 'rb') # Open the file workBook = xlrd.open_workbook(r'D:\project\') allSheetNames = workBook.sheet_names() # Get the names of all sheets (list type) SheetName1= workBook.sheet_names()[0] # By index number print(allSheetNames, SheetName1) #Output: ['Sheet1', 'Sheet2', 'Sheet3'] Sheet1 # Get the contents of the sheet sheet1_content1 = workBook.sheet_by_index(0) # The sheet index starts at 0 sheet1_content2 = workBook.sheet_by_name('sheet1') # Get by sheet name # Get whole rows and columns of values (arrays) print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols) # Get whole rows and columns of values (arrays) rows = sheet1_content1.row_values(3) # Get the fourth line cols = sheet1_content1.col_values(2) # Get the contents of the third column print(rows) print(cols ) # Get cell contents (three ways) print(sheet1_content1.cell(1, 0).value) print(sheet1_content1.cell_value(2, 2)) print(sheet1_content1.row(2)[2].value)
II. python write data
1, xlwt package to write Excel files
Limitations of xlwt Write Library: It can only write to newly created excel.
(Write to open the document and make a copy of the available copy)
The xls file generated in xlwt supports up to 65536 rows of data
Create table to write data # Batch write false data to execl import xlwt,faker,random wb=() sheet002=wb.add_sheet("002") head=["Name","Age","Gender"] for h in head: (0,(h),h) #Use a for loop to write the data row by row, column by column, value by value, using subscripts for columns. fake=() for i in range(1,101): (i, 0, ()) (i, 1, (10,60)) (i, 2, (['Male','Female'])) ("")
#2 Copying a table to write data import xlwt import xlrd import rd = xlrd.open_workbook("", formatting_info = True) # Open the file wt = (rd) # Copy sheets = wt.get_sheet(0) # Read the first worksheet (m, n, "I love you!") # Write to cells in row m-1 and column n-1 ("") # save (a file etc) (computing)
2, openpyx can only read xlsx can not read xls documents
xl = openpyxl.load_workbook('D:\project\', data_only=True) # Set up worksheets sheet1 = [0] for i in range(1, 24): (i, 3).value = cvalue # Save the form ('D:\project\')
III. Summary
python There are many toolkits that provide excel interaction.
- I. Finding the right fit
- II. Distinguishing between the rights and functions of different packages
As:(xlsx is not supported in newer versions of xlrd, you need to roll back older versions to use it)
- Third, there is a problem debug run to find the problem point, reasonable and as soon as possible to solve the problem!
to this article on the use of python excel interactive tools details of the article is introduced to this, more related python excel interactive tools content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!