Summary:
Write data to Microsoft Excel, use xlwt, its official website: https://pypi.org/project/xlwt/
Installation command:
pip/pip3 install xlwt
Simple application as follows:
# ! / Usr / bin / Python # - * - Coding: UTF-8 - * - Import xlwt # added to set the default encoding to avoid: UnicodeEncodeError: 'ASCII' CODEC CAN not encode characters ... Import SYS reload (SYS) sys.setdefaultencoding ( ' UTF-. 8 ' ) # style related DEF CreateStyle (rowx, COLX, value): # initialization pattern style = xlwt.XFStyle () font = xlwt.Font () # set Font alignment = xlwt.Alignment () # set the arrangement format borders = xlwt.Borders () # set border = xlwt.Pattern pattern () # set the background color IF rowx == 0: font.name = U ' Chinese Choi ' elif rowx ==. 1 : IF COLX% 2 == 0: Font.Bold = True the else : Font.ITALIC = True elif rowx == 2 : pattern.pattern_fore_colour = COLX pattern.pattern = xlwt.Pattern.SOLID_PATTERN elif rowx ==. 3 : alignment.horzXlwt.Alignment.HORZ_CENTER = # horizontally centered alignment.vert = xlwt.Alignment.VERT_CENTER # vertical center elif rowx ==. 4 : borders.left = COLX borders.top = COLX borders.bottom = COLX IF COLX% 2 == 0: borders.right = xlwt.Borders.THICK the else : borders.right = xlwt.Borders.THIN # Definition format style.font = font style.borders = Borders style.alignment = Alignment style.pattern = pattern return style DEF CreateNewExcel (): # Create a new Excel the fact that newBook = xlwt.Workbook (encoding = ' UTF-. 8 ' ) # Create a new Sheet NewSheet = newbook.add_sheet ( ' NewSheet ' ) # write the contents for rowx in Range (0,10 ): for COLX in Range (0,10 ): R & lt = rowx # line position COLX = C # column position label = STR (COLX) # SUMMARY style = CreateStyle (R & lt, C, label) # style newSheet.write (R & lt, C, label, style) # save excel, to save the format .xls subject, If you save .xlsx, then open the file will complain newbook.save ( ' NewExcel.xls ' ) IF __name__ == ' __main__ ' : CreateNewExcel ()
Thickened portion of subject code, the effect is as follows:
For the main pattern is written in the cell are summarized as follows:
style | Introduction |
font | Operating the font, such as font type, color, size, and so whether the bold |
alignment | Alignment, such as horizontal centering, vertical centering, etc. |
borders | Borders, whether to add solid or dashed, etc. |
pattern | Related Background Color |
protection | Protected Mode related ??? |
Reference may be: xlwt / Formatting.py
Next we setting them down in order to save space only subject code and paste:
font
# Style related DEF CreateFontStyle (rowx, COLX, value): # initialization pattern style = xlwt.XFStyle () # Set Font font = xlwt.Font () font.name = ' Airal ' # font type, font type of reference Excel, if Chinese characters, note that adding u format . Font.Bold = True # whether bold Font.ITALIC = True # is italicized font.struck_out = True # whether to add strikethrough font.height = 0x00C8 # text height font.outline = False # font = False .shadow # = 0x7FF font.colour_index # text color, may be color-coded with reference to the bottom font._weight = 0x0190 # # underlined related, mainly: None, single underline, the double underline # Type: UNDERLINE_NONE UNDERLINE_SINGLE UNDERLINE_SINGLE_ACC UNDERLINE_DOUBLE UNDERLINE_DOUBLE_ACC font.underline = xlwt.Font. UNDERLINE_SINGLE # types: ESCAPEMENT_NONE, ESCAPEMENT_SUPERSCRIPT, ESCAPEMENT_SUBSCRIPT font.escapement = xlwt.Font.ESCAPEMENT_NONE # types: FAMILY_NONE, FAMILY_ROMAN, FAMILY_SWISS, FAMILY_MODERN, FAMILY_SCRIPT, FAMILY_DECORATIVE font.family = xlwt.Font.FAMILY_NONE # 类型有:CHARSET_ANSI_LATIN, CHARSET_SYS_DEFAULT, CHARSET_SYMBOL, CHARSET_APPLE_ROMAN, CHARSET_ANSI_JAP_SHIFT_JIS, # CHARSET_ANSI_KOR_HANGUL, CHARSET_ANSI_KOR_JOHAB, CHARSET_ANSI_CHINESE_GBK, CHARSET_ANSI_CHINESE_BIG5, # CHARSET_ANSI_GREEK, CHARSET_ANSI_TURKISH, CHARSET_ANSI_VIETNAMESE, CHARSET_ANSI_HEBREW, CHARSET_ANSI_ARABIC, # CHARSET_ANSI_BALTIC, CHARSET_ANSI_CYRILLIC, CHARSET_ANSI_THAI, CHARSET_ANSI_LATIN_II, CHARSET_OEM_LATIN_I font.charset = xlwt.Font.CHARSET_SYS_DEFAULT # 定义格式 style.font = font return style
alignment
DEF CreateAlignmentStyle (rowx, COLX, value): # initialization pattern style = xlwt.XFStyle () # Set arrangement pattern Alignment = xlwt.Alignment () alignment.inde = 0 # alignment.merg = 0 # # levels associated # Type: HORZ_GENERAL , HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, # HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED alignment.horz = xlwt.Alignment.HORZ_CENTER # vertical correlation # type: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED alignment.vert = xlwt.Alignment.VERT_CENTER # 类型: DIRECTION_GENERAL, DIRECTION_LR, DIRECTION_RL alignment.dire = xlwt.Alignment.DIRECTION_GENERAL # 类型: ORIENTATION_NOT_ROTATED, ORIENTATION_STACKED, ORIENTATION_90_CC, ORIENTATION_90_CW alignment.orie = xlwt.Alignment.ORIENTATION_NOT_ROTATED # 类型: ROTATION_0_ANGLE, ROTATION_STACKED alignment.rota = xlwt.Alignment.ROTATION_0_ANGLE # 类型: WRAP_AT_RIGHT, NOT_WRAP_AT_RIGHT alignment.wrap = xlwt.Alignment.NOT_WRAP_AT_RIGHT # 类型: SHRINK_TO_FIT, NOT_SHRINK_TO_FIT alignment.shri = xlwt.Alignment.NOT_SHRINK_TO_FIT style.alignment = alignment return style
borders
DEF CreateBorderStype (rowx, COLX, value): # initialize style style = xlwt.XFStyle () # set the border Borders = xlwt.Borders () # style-related # type: NO_LINE, THIN, MEDIUM, DASHED , DOTTED, THICK, DOUBLE, HAIR borders.left = xlwt.Borders.NO_LINE # borders.right = xlwt.Borders.THIN # borders.top = xlwt.Borders.MEDIUM # borders.bottom = xlwt.Borders.DASHED # borders.diag = xlwt.Borders.THICK # # color correlation, the end of the reference color associated borders.left_colour = 0x40 borders.right_colour = 0x40 borders.top_colour = 0x40 borders.bottom_colour = 0x40 borders.diag_colour = 0x40 # 类型: NEED_DIAG1, NEED_DIAG2, NO_NEED_DIAG1, NO_NEED_DIAG2 borders.need_diag1 = xlwt.Borders.NO_NEED_DIAG1 # borders.need_diag2 = xlwt.Borders.NO_NEED_DIAG2 # style.borders = borders return style
pattern
DEF CreatePatternStype (rowx, COLX, value): # initialization pattern style = xlwt.XFStyle () # set the border pattern = xlwt.Pattern () # mode dependent, type: NO_PATTERN, SOLID_PATTERN pattern.pattern = xlwt.Pattern.SOLID_PATTERN pattern. pattern_fore_colour =. 1 # pattern.pattern_back_colour = 2 # style.pattern = pattern return style
protection
DEF CreatePatternStype (rowx, COLX, value): # initialization pattern style = xlwt.XFStyle () # Set Border Protection = xlwt.Protection () protection.cell_locked =. 1 protection.formula_hidden = 0 style.protection = Protection return style
Above, on the set of colors, reference: xlwt / Style.py, summarizes some of the commonly used colors:
0x0A 0x0E crimson red pink black 0x10 0x08 0x14 white 0x09 violet
blue turquoise 0x0C 0x36 0x12 green and dark blue ice blue light blue 0x1F 0x30 0x12 dark blue
emerald green 0x0F 0x0B 0x11 green light green dark green 0x3A 0x2A olive green 0x3B
pale yellow 0x2B yellow yellow 0x0D 0x13 0x15 blue coral stone gray 0x32 0x1D
gray gray 40 0x37 25 0x16 50 0x17 gray brown gray 80 0x3F 0x3C 0x1C dark purple
sky blue brown 0x2F 0x28 0x33 golden orange light orange 0x34 0x35 0x1E blue ocean
rose 0x2D