用Python读取、操作Excel可以看这篇:Python Pandas使用举例
这一篇主要介绍一些格式设置。
1.生成Excel文件 以下是读取Excel文件后,生成一个新的Excel文件的示例。
1 2 3 4 5 6 7 8 9 10 11 12 import osimport pandas as pdproj_path = os.getcwd() ex_file = os.path.join(proj_path, 'test.xlsx' ) df = pd.read_excel(ex_file) output_file = os.path.join(proj_path, 'output_test.xlsx' ) df.to_excel(output_file, index=False )
2.设置字体并且居中对齐 生成的表格中默认的字体是“宋体”。比如把字体设置为:Arial
居中对齐 :
horizontal=’center’:水平居中
vertical=’center’:垂直居中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import osimport pandas as pdfrom openpyxl.styles import Font, Alignmentproj_path = os.getcwd() ex_file = os.path.join(proj_path, 'test.xlsx' ) df = pd.read_excel(ex_file) output_file = os.path.join(proj_path, 'output_test.xlsx' ) with pd.ExcelWriter(output_file, engine='openpyxl' ) as writer: df.to_excel(writer, index=False ) worksheet = writer.sheets['Sheet1' ] for row in worksheet.iter_rows(): for cell in row: cell.font = Font(name='Arial' , size=11 ) cell.alignment = Alignment(horizontal='center' , vertical='center' )
3.设置合适的列宽 1).添加了 get_column_width
函数来精确计算字符串宽度:
中文字符计为2.5个单位宽度
非中文字符计为1个单位宽度
2).对每个单元格内容使用这个函数计算实际需要的宽度 3).最后统一加4个单位的边距
如果宽度还是不够的话,可以这么做:
增加中文字符的权重(从2.5改为更大的数值)
增加边距(从+4改为更大的数值)
或者两者都调整
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import osimport pandas as pdfrom openpyxl.styles import Font, Alignmentproj_path = os.getcwd() ex_file = os.path.join(proj_path, 'test.xlsx' ) df = pd.read_excel(ex_file) output_file = os.path.join(proj_path, 'output_test.xlsx' ) with pd.ExcelWriter(output_file, engine='openpyxl' ) as writer: df.to_excel(writer, index=False ) worksheet = writer.sheets['Sheet1' ] for row in worksheet.iter_rows(): for cell in row: cell.font = Font(name='Arial' , size=11 ) cell.alignment = Alignment(horizontal='center' , vertical='center' ) def get_column_width (text ): width = 0 for char in str (text): if '\u4e00' <= char <= '\u9fff' : width += 2.5 else : width += 1 return width for column in worksheet.columns: max_width = 0 column_name = column[0 ].column_letter for cell in column: try : cell_width = get_column_width(cell.value) if cell_width > max_width: max_width = cell_width except : pass adjusted_width = max_width + 4 worksheet.column_dimensions[column_name].width = adjusted_width
4.设置边框线 1).导入了边框相关的类:Border和Side 2).创建了一个统一的边框样式 thin_border:
四个边都使用细线样式(’thin’)
包括左、右、上、下四个边
3).在设置单元格样式时,同时设置了字体、对齐和边框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 import osimport pandas as pdfrom openpyxl.styles import Font, Alignment, Border, Sideproj_path = os.getcwd() ex_file = os.path.join(proj_path, 'test.xlsx' ) df = pd.read_excel(ex_file) output_file = os.path.join(proj_path, 'output_test.xlsx' ) with pd.ExcelWriter(output_file, engine='openpyxl' ) as writer: df.to_excel(writer, index=False ) worksheet = writer.sheets['Sheet1' ] thin_border = Border( left=Side(style='thin' ), right=Side(style='thin' ), top=Side(style='thin' ), bottom=Side(style='thin' ) ) for row in worksheet.iter_rows(): for cell in row: cell.font = Font(name='Arial' , size=11 ) cell.alignment = Alignment(horizontal='center' , vertical='center' ) cell.border = thin_border def get_column_width (text ): width = 0 for char in str (text): if '\u4e00' <= char <= '\u9fff' : width += 2.5 else : width += 1 return width for column in worksheet.columns: max_width = 0 column_name = column[0 ].column_letter for cell in column: try : cell_width = get_column_width(cell.value) if cell_width > max_width: max_width = cell_width except : pass adjusted_width = max_width + 4 worksheet.column_dimensions[column_name].width = adjusted_width
5.设置标题行样式 1).设置标题行的行高 默认行高通常是15左右
2).冻结首行 ‘A2’表示从第2行开始冻结,也就是冻结第1行
3).设置【F列标题】的填充色为【黄色】 4).设置【E列】所有有内容的单元格填充为【橙色】 5).设置【B列】所有有内容的单元格字体为【红色】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 worksheet.row_dimensions[1 ].height = 25 worksheet.freeze_panes = 'A2' f_cell = worksheet['F1' ] f_cell.fill = PatternFill(start_color='FFFF00' , end_color='FFFF00' , fill_type='solid' ) for cell in worksheet['E' ]: if cell.value: cell.fill = PatternFill(start_color='FFA500' , end_color='FFA500' , fill_type='solid' ) for cell in worksheet['B' ]: if cell.value: cell.font = Font(name='Arial' , size=11 , color='FF0000' )
最后完整效果如下:
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 import osimport pandas as pdfrom openpyxl.styles import Font, Alignment, Border, Side, PatternFillproj_path = os.getcwd() ex_file = os.path.join(proj_path, 'test.xlsx' ) df = pd.read_excel(ex_file) output_file = os.path.join(proj_path, 'output_test.xlsx' ) with pd.ExcelWriter(output_file, engine='openpyxl' ) as writer: df.to_excel(writer, index=False ) worksheet = writer.sheets['Sheet1' ] thin_border = Border( left=Side(style='thin' ), right=Side(style='thin' ), top=Side(style='thin' ), bottom=Side(style='thin' ) ) worksheet.row_dimensions[1 ].height = 25 for row in worksheet.iter_rows(): for cell in row: cell.font = Font(name='Arial' , size=11 ) cell.alignment = Alignment(horizontal='center' , vertical='center' ) cell.border = thin_border f_cell = worksheet['F1' ] f_cell.fill = PatternFill(start_color='FFFF00' , end_color='FFFF00' , fill_type='solid' ) for cell in worksheet['E' ]: if cell.value: cell.fill = PatternFill(start_color='FFA500' , end_color='FFA500' , fill_type='solid' ) for cell in worksheet['B' ]: if cell.value: cell.font = Font(name='Arial' , size=11 , color='FF0000' ) def get_column_width (text ): width = 0 for char in str (text): if '\u4e00' <= char <= '\u9fff' : width += 2.5 else : width += 1 return width for column in worksheet.columns: max_width = 0 column_name = column[0 ].column_letter for cell in column: try : cell_width = get_column_width(cell.value) if cell_width > max_width: max_width = cell_width except : pass adjusted_width = max_width + 4 worksheet.column_dimensions[column_name].width = adjusted_width worksheet.freeze_panes = 'A2'