Python之读写Excel
现有的Excel分为两种格式:xls(Excel 97-2003)和xlsx(Excel 2007及以上)。
Python处理Excel文件主要是第三方模块库xlrd、xlwt、pyexcel-xls、xluntils和pyExcel-erator等,此外Pandas中也带有可以读取Excel文件的模块(read_excel)。
基于扩展知识的目的,本文使用xlrd模块读取Excel数据。
[安装]
# 读取
pip install xlrd
# 写入
pip install xlwt
xlrd使用:
import xlrd # 导入库 # 打开文件 xlsx = xlrd.open_workbook('demo.xlsx') # 查看所有sheet列表 print('All sheets: %s' % xlsx.sheet_names()) 如果只有一个sheet的话, 会输出: All sheets: ['Sheet1'] 查看sheet中数据 sheet1 = xlsx.sheets()[0] # 获得第1张sheet,索引从0开始 sheet1_name = sheet1.name # 获得名称 sheet1_cols = sheet1.ncols # 获得列数 sheet1_nrows = sheet1.nrows # 获得行数 print('Sheet1 Name: %s\nSheet1 cols: %s\nSheet1 rows: %s' % (sheet1_name, sheet1_cols, sheet1_nrows)) 输出: sheet1 = xlsx.sheets()[0] # 获得第1张sheet,索引从0开始 sheet1_name = sheet1.name # 获得名称 sheet1_cols = sheet1.ncols # 获得列数 sheet1_nrows = sheet1.nrows # 获得行数 print('Sheet1 Name: %s\nSheet1 cols: %s\nSheet1 rows: %s' % (sheet1_name, sheet1_cols, sheet1_nrows)) 查看sheet每行数据明细: for i in range(sheet1_nrows): # 逐行打印sheet1数据 print(sheet1.row_values(i)) 输出: ['ID_number', 'Status', 'Create_Time', 'Business_City'] ['431381198109106573', '有效', 42725.0, '深圳市'] ['431381198809122734', '有效', 42725.0, '深圳市'] … ['431381198901176911', '有效', 42725.0, '深圳市'] ['43138119870827275X', '有效', 42725.0, '深圳市']
上述操作只是将数据从Excel中读取出来,将读取的数据转换为数组便可以进行矩阵计算。由于矩阵计算大多是基于数值型数据实现的,因此上述数据将无法适用于大多数科学计算场景,这点需要注意。
xlwt使用:
import xlwt # 创建一个workbook 设置编码 workbook = xlwt.Workbook(encoding = 'utf-8') # 创建一个worksheet worksheet = workbook.add_sheet('My Worksheet') # 写入excel # 参数对应 行, 列, 值 worksheet.write(1,0, label = 'this is test') # 保存 workbook.save('Excel_test.xls')
更多demo:
import xlwt workbook = xlwt.Workbook(encoding = 'ascii') worksheet = workbook.add_sheet('My Worksheet') style = xlwt.XFStyle() # 初始化样式 font = xlwt.Font() # 为样式创建字体 font.name = 'Times New Roman' font.bold = True # 黑体 font.underline = True # 下划线 font.italic = True # 斜体字 style.font = font # 设定样式 worksheet.write(0, 0, 'Unformatted value') # 不带样式的写入 worksheet.write(1, 0, 'Formatted value', style) # 带样式的写入 workbook.save('formatting.xls') # 保存文件 设置单元格宽度: import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') worksheet.write(0, 0,'My Cell Contents') # 设置单元格宽度 worksheet.col(0).width = 3333 workbook.save('cell_width.xls') 输入日期到单元格: import xlwt import datetime workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') style = xlwt.XFStyle() style.num_format_str = 'M/D/YY' # Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0 worksheet.write(0, 0, datetime.datetime.now(), style) workbook.save('Excel_Workbook.xls') 向单元格添加公式: import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') worksheet.write(0, 0, 5) # Outputs 5 worksheet.write(0, 1, 2) # Outputs 2 worksheet.write(1, 0, xlwt.Formula('A1*B1')) # Should output "10" (A1[5] * A2[2]) worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)')) # Should output "7" (A1[5] + A2[2]) workbook.save('Excel_Workbook.xls') 单元格添加超链接: import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') worksheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")')) # Outputs the text "Google" linking to http://www.google.com workbook.save('Excel_Workbook.xls') 合并列和行: import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') worksheet.write_merge(0, 0, 0, 3, 'First Merge') # Merges row 0's columns 0 through 3. font = xlwt.Font() # Create Font font.bold = True # Set font to Bold style = xlwt.XFStyle() # Create Style style.font = font # Add Bold Font to Style worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style) # Merges row 1 through 2's columns 0 through 3. workbook.save('Excel_Workbook.xls') 设置单元格内容的对齐方式: import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') alignment = xlwt.Alignment() # Create Alignment alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED style = xlwt.XFStyle() # Create Style style.alignment = alignment # Add Alignment to Style worksheet.write(0, 0, 'Cell Contents', style) workbook.save('Excel_Workbook.xls') 为单元格添加边框: import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') borders = xlwt.Borders() # Create Borders borders.left = xlwt.Borders.DASHED DASHED虚线 NO_LINE没有 THIN实线 # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D. borders.right = xlwt.Borders.DASHED borders.top = xlwt.Borders.DASHED borders.bottom = xlwt.Borders.DASHED borders.left_colour = 0x40 borders.right_colour = 0x40 borders.top_colour = 0x40 borders.bottom_colour = 0x40 style = xlwt.XFStyle() # Create Style style.borders = borders # Add Borders to Style worksheet.write(0, 0, 'Cell Contents', style) workbook.save('Excel_Workbook.xls') 单元格设置背景色: import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') pattern = xlwt.Pattern() # Create the Pattern pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12 pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on... style = xlwt.XFStyle() # Create the Pattern style.pattern = pattern # Add Pattern to Style worksheet.write(0, 0, 'Cell Contents', style) workbook.save('Excel_Workbook.xls')