A simple example of reading an Excel file#!/usr/bin/env python#-*-coding:utf-8-*-import xlrdfrom xlrd.book import bookfrom xlrd.sheet import Sheetfrom Xlrd.shee T Import Cellworkbook = Xlrd.open_workbook (' account information. xlsx ') Sheet_names = Workbook.sheet_names () # sheet = Workbook.sheet_by_ Name (' account information ') sheet = workbook.sheet_by_index (1) # Loops All rows of the
, XF) Table.cell (0,0) #单元格的值 ' Table.cell (0,0). Value #单元格的值 'Third, demo codeThe demo code is simple enough to read the Excel data.#-*-coding:utf-8-*-Importxdrlib, sysImportxlrddefOpen_excel (file='File.xls'): Try: Data=xlrd.open_workbook (file)returnDataexceptexception,e:PrintStr (e)#get data parameters in Excel table According to index: File:excel file path Colnameindex: Table header column name is
useImport XLWTImport XLWTCreate a new Excel filefile = xlwt. Workbook () #注意这里的Workbook首字母是大写, no wordsCreate a new sheetTable = 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 aReturns error:# exception:attempt to overwrite cell:# sheetname=u ' sheet 1 ' rowx=0 colx=0So when opened, add cell_overwrite_o
This article describes a script that uses Python to write a statistical LAN that is not shut down and generates an Excel table (sample code)
#!/udict/bin/env python#-*-coding:utf_8-*-#Date: 2016/10/17#author:wangpeng#blog:http:// Wangpengtai.blog.51cto.comimport subprocessimport nmapimport time,datetimeimport xlrd,xls
This article describes the Excel file read-write class implemented by Python. Share to everyone for your reference. Specific as follows:
#coding =utf-8####################################################### #filename: excelrw.py#author:defias#date : 2015-4-27#function:read or write
= Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, = maroon, + = Dark Green, 18 = Dark Blue, Dark Yellow, almost brown), = Dark Magenta, = Teal, = Light gray, Dark Gray, the list goes On ... style = XLWT. Xfstyle () # Create the pattern Style.pattern = pattern# Add pattern to style Worksheet.write (0, 0, ' Cell Conte NTS ', style) Workbook.save (' Excel_workbook.xls ') Todo:things left to Document- Panes -- separate views which are always in view- Border Colors (documented above, but n
Part 1. Module Introduction:There are two modules for reading and writing to Excel tables using Python, respectively:1. Read module to Excel table Xlrd 0.9.3:: https://pypi.python.org/pypi/xlrdIn English: The package was for reading data and formatting information from Excel files. 2.
Following the previous article using XLRD to read Excel, this article to introduce, how to write Excel, write Excel we need to use a third-party library XLWT, and xlrd, like xlrd means read XLS,XLWT represents write xls, Also curr
Following the previous article using XLRD to read Excel, this article is about how to write Excel, write Excel we need to use the Third-party library XLWT, and XLRD, Xlrd said Read XLS,XLWT represents write xls, Also, the current
You might need to sort out some documents in your actual work, or record some data, and using Python to manipulate Excel might help you.Read operation:#Encoding:utf-8 #设置编码方式ImportXlrd#Import xlrd Module#Open the Excel file for the specified file pathXlsfile= R'D:\AutoPlan\apisnew.xls' Book= Xlrd.open_workbook (Xlsfile)#get the Book object for
Following the previous article using XLRD to read Excel, this article to introduce, how to write Excel, write Excel we need to use a third-party library XLWT, and xlrd, like xlrd means read XLS,XLWT represents write xls, Also curr
, Colx for the column subscript, all starting from 0.
Book: A book object that points to its own subordinate
Name
Cell (ROWX,COLX): Returns the Cell object of the ROWX row Colx column
Cell_type (ROWX,COLX)
Cell_value (ROWX,COLX)
Ncols
Col (Colx): Returns the Cell object sequence for the specified row?
Col_slice (Colx,start_rowx=0,end_rowx=none)
Col_types (Colx,start_rowx=0,end_rowx=none)
Col_values (Colx,start_rowx=0,end_rowx=none)
Nrows
Row (
1 #!usr/bin/env python2 #-*-coding:utf-8-*-3 Importxlrd4 ImportXLWT5 fromXlutils.copyImportCopy6 ImportOS7 8data = Xlrd.open_workbook ('M3000.xls')#open an Excel table9Table = data.sheets () [0]#The sheet read by this method is read-only and cannot be writtenTenrows =table.nrows OneNcols =Table.ncols A -datanew = copy (data)#copy a new Excel table -sheetnew = Datanew.get_sheet (0)#The sheet read by this m
= Workbook.add_format () format_title.set_border (1) Format_title.set_bg_color (' #cccccc ') Format_text = Workbook.add_format () format_text.set_border (1) # # Write cell Operation Worksheet.set_column (' A:k ', #) # #设置A到K列宽度20像素worksheet. Write_row (' A1 ', Title,format_title) # # The title list has a A1 beginning to be written horizontally, and the format is Format_titleworksheet.write_row (' A2 ', Text,format_text) workbook.close () # #记得将工作表关闭T
, ' Test ', Def_style ())* Cell size does not automatically adjust depending on the size and number of contentSheet.row (x). Height = ...Sheet.col (x). Width = ...To adjust the cell size* To set the cell's background color, border, etc., you can set other properties of the style, such asBackground color to set Style.pattern, such as:PTN = XLWT. Pattern ()Ptn.pattern = XLWT. Pattern.solid_patternPtn.pattern_fore_colour = color codeStyle.pattern = PTNborder = XLWT. Borders ()Border.left = XLWT. Bo
Write Excel with Python XlwtinstallPip Install XLWTXLWT. Workbook ObjectHe has 2 common methods, one for saving and one for Add_sheet adding worksheets.1. Save2. Add_sheetWorksheets Objectis created by the workbook object. You can write the cell directly. You can also return to the Rows class, and then the rows to
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.