Package: HTTPS://PYPI.PYTHON.ORG/PYPI/XLRD ImportImport xlrdOpen Exceldata = Xlrd.open_workbook (' Demo.xls ') #注意这里的workbook首字母是小写View the name of the file that contains the sheetdata.sheet_names ()Get the first worksheet, or by index order or sheet nametable = data.sheets () [0]table = data.sheet_by_index (0)table = data.sheet_by_name (U ' Sheet1 ')Get the number of rows and columnsnrows = table.nrowsncols = Table.ncolsGet the values (arrays) for the entire row and the entire columntable.row_values (i)table.col_values (i)Looping rows, getting the list of indexesFor rownum in range (table.nrows):print table.row_values (rownum)Cellcell_a1 = Table.cell (0,0). Valuecell_c4 = Table.cell (2,3). ValueUse row and column indexes, respectivelycell_a1 = table.row (0) [0].valuecell_a2 = table.col (1) [0].valueA simple writerow = 0col = 0ctype = 1 # type 0 empty,1 string, 2 number, 3 date, 4 Boolean, 5 errorvalue = ' Lixiaoluo 'XF = 0 # Extended Formatting (default is 0)Table.put_cell (Row, col, CType, value, XF)Table.cell (0,0) # Text: U ' lixiaoluo 'Table.cell (0,0). Value # ' Lixiaoluo 'Xlwt
Http://pypi.Python.org/pypi/xlrd
Simple to use
Import XLWT
Import XLWT
Create a new Excel file
File = XLWT. Workbook () #注意这里的Workbook首字母是大写, no words
Create a new sheet
Table = File.add_sheet (' sheet name ')
Write Data Table.write (rows, columns, value)
Table.write (0,0, ' test ')
If you repeat the operation on a cell, it throws a
Returns error:# exception:attempt to overwrite cell:# sheetname=u ' sheet 1 ' rowx=0 colx=0
So when opened, add cell_overwrite_ok=true to solve
Table = file.add_sheet (' sheet name ',cell_overwrite_ok=true)
Save File
File.save (' Demo.xls ')
In addition, use the style
style = XLWT. Xfstyle () # Initialize style
Font = XLWT. Font () #为样式创建字体
Font.Name = ' Times New Roman '
Font.Bold = True
Style.font = Font #为样式设置字体
Table.write (0, 0, ' Some bold times text ', style) # using styles
XLWT allows you to format cells or entire rows. You can also add links and formulas. You can read the source code, where there are examples:
dates.py, showing how to set different data formats
hyperlinks.py, showing how to create a hyperlink (Hint:you need to use a formula)
merged.py, show how to merge lattice
row_styles.py, shows how to apply a style to an entire row of squares.
Python operations Excel Read-write (using XLRD and XLRT)