From Python XLRD, XLWT


xlrd

Import XLRD, import XLRD

Open the Excel file. XLSX = xlrd.open_workbook(path), where path is the absolute path of the file

Get the sheet name, xlsx.sheet_names(), and return the list

Get the sheet object, sheet = xlsX.sheets ()[0], and get the first sheet

Get the number of rows and columns in the sheet, sheet.ncols sheet.nrows

Gets the contents of line I, sheet.row_values(I), which returns a list

Select sheet. Row_values (I) from sheet.row_values(I)[j]

xlwt

Import XLWT, import XLWT

Create a Workbook, XLSX = xlwt.Workbook(encoding=” UTF-8 “), set the encoding to UTF-8

Add sheet, sheet = xlsx.add_sheet(“sheet1”, True), parameter: sheet name; Whether to allow writes to be overwritten. The default is False. If False, an error will be reported when writing to be overwritten

Set the cell width, sheet.col(0).width = 256 * num, set the first column width, num is the number of characters, 256 is the width of a single character

Write cell, sheet.write(I, j, content, style), argument: write cell (counting from 0) in row I and column J, style is cell style

Write, sheet. Write_merge (topRow, bottomRow, leftCol, rightCol, Content, style)

Save the workbook, xlsx.save(path), note that the contents written must be the same as the encoding of the workbook, otherwise an error will be reported when saving. For example, if the encoding is set to UTF-8, all the contents written must be utF-8 encoding

The style is set

New alignment: Alignment = xlwt.alignment ()

Horz = xlwt.align.horz_center

Set alignment center to xlwt.vert = xlwt.vert_center

Align.wrap = xlwt.align.wrap_AT_right

Create a new font, font = xlwt.font ()

Font bold = True

Set the font size to 12 * 20

Font. Name = “SimSun”

New Borders, borders = xlwt.borders ()

Set the table width, borders.left = xlwt.borders.thin

Create a new style, style = xlwt.xfstyle ()

Set the alignment for style, style.alignment = alignment

Set font, style.font = font for style

Set borders for style, style.borders = borders

Finally, use style when writing cells, for more details on the style Settings in the reference article

hyperlinks

Create link, link = ‘HYPERLINK(“%s”;” %s”)’ % (str1, str2), str1 is the link address, either the file path (remember to change the slash to a double slash) or the URL address, str2 is the text displayed in the cell

Write (I, j, xlwt.formula (link), style) and write row I column J, style style optional