Use python to read and write excel (xlrd, xlwt)

Source: Internet
Author: User

Get a worksheet

  table = data.sheets()[0]            table = data.sheet_by_index(0)   table = data.sheet_by_name(u)
Get the value of the whole row and the whole column (returned array)Obtain the number of rows and columns6. Get CellsWhen I used it myself, I thought it was still the most useful to get the cell. This is equivalent to giving you a two-dimensional array, and you can do whatever you want. This very useful library code is very concise. However, there are still a number of pitfalls that lead to some time to explore. List them for future reference:1. First, my statistics are based on the name of each table, but debugging found that different names in different tables do not seem to be able to match, and began to suspect the encoding problem, but it was later found that it was due to space. It seems that there is no difference when I enter a few spaces or tabs after some names in excel, however, during program processing, this is two completely different strings. My solution is to add strip () to each obtained string for processing. Good results2. It is also a string matching. when determining whether the string (Chinese) in a cell is equal to what I have provided, it cannot be matched, and unicode does not work well. Baidu has some solutions, but they are both complicated or useless. Finally, I adopted a more flexible method: directly obtain the value I want from excel and then compare it. The effect is good, that is, the general-purpose row is not very good, it cannot be solved. Ii. Write an excel table The xlwt module is used to write an excel table (Http://pypi.python.org/pypi/xlwt ). The general procedure is as follows: 1. Import Module Import xlwt 2. Create a workbook (in fact, it is excel, and save it later)   workbook = xlwt.Workbook(encoding = 'ascii') 3. Create a table
Worksheet = workbook. add_sheet ('My Worksheet ')
4. Write content into cells   worksheet.write(0, 0, label = 'Row 0, Column 0 Value')
5. Save   workbook.save('Excel_Workbook.xls') My requirements are relatively simple, so there is no problem above. The only difference is that we recommend using ascii encoding. Otherwise, there may be some strange phenomena. Of course, xlwt functions are far more than that. It can even set various styles. Attached example
     Please note a useful alternative may be ezodf, which allows you to generate ODS (Open Document Spreadsheet) files  LibreOffice / OpenOffice. You can check them out at:http://packages.python.org/ezodf/    workbook = xlwt.Workbook(encoding =  worksheet = workbook.add_sheet( worksheet.write(0, 0, label =  workbook.save(    workbook = xlwt.Workbook(encoding =  worksheet = workbook.add_sheet( font = xlwt.Font()  font.name =  font.bold = font.underline = font.italic = style = xlwt.XFStyle()  style.font = font  worksheet.write(0, 0, label =  worksheet.write(1, 0, label = , style)  workbook.save(   font.bold = True  font.italic = True  font.struck_out = True  font.underline = xlwt.Font.UNDERLINE_SINGLE  font.escapement = xlwt.Font.ESCAPEMENT_SUPERSCRIPT  font.family = xlwt.Font.FAMILY_ROMAN  font.charset = xlwt.Font.CHARSET_ANSI_LATIN  font.colour_index = font.get_biff_record = font.height = 0x00C8  font.name = font.outline = font.shadow =    workbook = worksheet = workbook.add_sheet( worksheet.write(0, 0,  worksheet.col(0).width = 3333  workbook.save(     workbook = worksheet = workbook.add_sheet( style = style.num_format_str =    workbook.save(    workbook = worksheet = workbook.add_sheet( worksheet.write(0, 0, 5)  worksheet.write(0, 1, 2)  worksheet.write(1, 0, xlwt.Formula())  worksheet.write(1, 1, xlwt.Formula())  workbook.save(    workbook = worksheet = workbook.add_sheet( worksheet.write(0, 0, xlwt.Formula())  workbook.save(  Merging Columns   workbook = worksheet = workbook.add_sheet( worksheet.write_merge(0, 0, 0, 3, )  font = xlwt.Font()  font.bold = True  style = xlwt.XFStyle()  style.font = font  worksheet.write_merge(1, 2, 0, 3, , style)  workbook.save(  Setting the Alignment   workbook = worksheet = workbook.add_sheet( alignment = xlwt.Alignment()  alignment.horz = xlwt.Alignment.HORZ_CENTER  alignment.vert = xlwt.Alignment.VERT_CENTER  style = xlwt.XFStyle()  style.alignment = alignment  worksheet.write(0, 0,  workbook.save(     workbook = worksheet = workbook.add_sheet( borders = xlwt.Borders()  borders.left = xlwt.Borders.DASHED  borders.right = borders.top = borders.bottom = borders.left_colour = 0x40 borders.right_colour = 0x40 borders.top_colour = 0x40 borders.bottom_colour = 0x40 style = xlwt.XFStyle()  style.borders = borders  worksheet.write(0, 0,  workbook.save(    workbook = worksheet = workbook.add_sheet( pattern = xlwt.Pattern()  pattern.pattern = xlwt.Pattern.SOLID_PATTERN  pattern.pattern_fore_colour = 5  style = xlwt.XFStyle()  style.pattern = pattern  worksheet.write(0, 0,  workbook.save(   - Panes -- separate views which are always  - Border Colors (documented above, but  - Border Widths (document above, but  - - - Zoom / - Source Code  reference available at: https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.