Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the related operation of python excel?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about the operation of python excel. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Because the related operations on excel are often used, I will tell you about it today.

Python operates excel as well as reading and writing.

Start with reading.

Xlrd

Http://pypi.python.org/pypi/xlrd

Import

Import xlrd

Open excel

File = xlrd.open_workbook ('demo.xls')

View the name of the file that contains sheet

File.sheet_names ()

Get the first worksheet, or by indexing order or worksheet name

Sheet = file.sheets () [0]

Sheet = file.sheet_by_index (0)

Sheet = file.sheet_by_name (upright Sheet1')

Get the number of rows and columns

Nrows = sheet.nrows

Ncols = sheet.ncols

Loop row to get a list of indexes

For rownum in range (sheet.nrows):

Print sheet.row_values (rownum)

Get the values of the entire row and column (array)

Sheet.row_values (I)

Sheet.col_values (I)

Cell (index fetch)

Cell_A1 = sheet.cell (0J0). Value

Cell_C4 = sheet.cell (2 and 3). Value

Use row and column indexes respectively

Cell_A1 = sheet.row (0) [0] .value

Cell_A2 = sheet.col (1) [0] .value

Xlwt

Http://pypi.python.org/pypi/xlrd

Import xlwt

Import xlwt

Create a new excel file

File = xlwt.Workbook () # notice that the initials of Workbook here are capitalized, speechless

Create a new sheet

Sheet = file.add_sheet ('sheet name')

Write data sheet.write (row, column, value)

Sheet.write (0pd0pm)

If you repeat the operation on a cell, a

Returns error:

# Exception: Attempt to overwrite cell:

# sheetname=u'sheet 1' rowx=0 colx=0

So add cell_overwrite_ok=True solution when you open it.

Sheet = file.add_sheet ('sheet name',cell_overwrite_ok=True)

Save Fil

File.save ('demo.xls')

In addition, use style

Style = xlwt.XFStyle () # initialization style

Font = xlwt.Font () # create fonts for styles

Font.name = 'Times New Roman'

Font.bold = True

Style.font = font # set the font for the style

Sheet.write (0,0, 'some bold Times text', style) # uses styles

Xlwt allows cells or entire rows to be formatted. 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 www.2cto.com

Hyperlinks.py, showing how to create a hyperlink (hint: you need to use a formula)

Merged.py, showing how to merge grids

Row_styles.py, showing how to apply Style to the entire grid.

This is how the editor shares the relevant operation of python excel. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report