时间:2022-12-06来源:www.pcxitongcheng.com作者:电脑系统城
在商场看到一个超级玛丽的乐高图
感觉使用excel的颜色填充也能画出来,并且可以借助python来实现
1.统一设置行高与列宽
excel表单元格的行与列的默认计量单位是不一样的,设置如何一样的数值并构成正方形:
行的默认单位是:磅; 而一个列宽单位等于“常规”样式中一个字符的宽度
行高1长度为0.365mm,列宽1是2.25mm,比例就是0.365 : 2.25
设置excel表单元格行高为: 27.682
设置excel表单元格列宽为: 4.374
2.根据照片中的图案分别使用 红、黄、蓝、黑颜色进行填充
白色即为默认背景色,完成填充如下:
openpyxl类
1.核心语句如下:
openpyxl.Workbook() #创建excel表
openpyxl.Workbook().save #保存excel表
fill = PatternFill('solid', fgColor='FFFF00') #颜色填充
sheet["A1"].border = border #设置边框
work.row_dimensions[i].height = 27.682 #设置行高
work.column_dimensions[get_column_letter(i)].width = 4.374 #设置列宽
workbook.active["A1"].value = "test001" 设置单元格的值
2.实现代码
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
import openpyxl from openpyxl import load_workbook # d打开excel表 from openpyxl.styles import PatternFill # 填充单元格的颜色 from openpyxl.utils import get_column_letter # 设置行高,列宽 from openpyxl.styles import Border, Side # 设置边框 # 1.创建并打开test.xlsx new_excel = openpyxl.Workbook() new_excel.save( "./test.xlsx" ) wb = load_workbook(filename = 'test.xlsx' ) # 使用第一个sheet作为工作簿 work = wb[wb.sheetnames[ 0 ]] sheet = wb[ 'Sheet' ] # 2.定义颜色填充方法,目前只涉及红、黑、蓝、黄、白 def color_fill( list , color): if color = = "yellow" : # 填充为黄色 fill = PatternFill( 'solid' , fgColor = 'FFFF00' ) elif color = = "red" : fill = PatternFill( 'solid' , fgColor = 'FF0000' ) elif color = = "black" : fill = PatternFill( 'solid' , fgColor = '000000' ) elif color = = "blue" : fill = PatternFill( 'solid' , fgColor = '0000FF' ) elif color = = "white" : fill = PatternFill( 'solid' , fgColor = 'FFFFFF' ) for i in list : work[i].fill = fill # 3.先初始化白色,再分别设置红、黑、蓝、黄色填充 color_white = [] for i in [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ]: for j in range ( 1 , 33 ): s = i + str (j) color_white.append(s) color_fill(color_white, "white" ) list_red = [ "g1" , "h1" , "i1" , "j1" , "k1" , "l1" , "m1" , "n1" , "o1" , "p1" , "q1" , "r1" , "g2" , "h2" , "i2" , "j2" , "k2" , "l2" , "m2" , "n2" , "o2" , "p2" , "q2" , "r2" , "e3" , "f3" , "g3" , "h3" , "i3" , "j3" , "k3" , "l3" , "m3" , "n3" , "o3" , "p3" , "q3" , "r3" , "s3" , "t3" , "u3" , "v3" , "w3" , "x3" , "e4" , "f4" , "g4" , "h4" , "i4" , "j4" , "k4" , "l4" , "m4" , "n4" , "o4" , "p4" , "q4" , "r4" , "s4" , "t4" , "u4" , "v4" , "w4" , "x4" , "i15" , "j15" , "i16" , "j16" , "i17" , "j17" , "o17" , "p17" , "i18" , "j18" , "o18" , "p18" , "i19" , "j19" , "k19" , "l19" , "m19" , "n19" , "o19" , "p19" , "i20" , "j20" , "k20" , "l20" , "m20" , "n20" , "o20" , "p20" , "g21" , "h21" , "k21" , "l21" , "m21" , "n21" , "q21" , "r21" , "g22" , "h22" , "k22" , "l22" , "m22" , "n22" , "q22" , "r22" , "g23" , "h23" , "i23" , "j23" , "k23" , "l23" , "m23" , "n23" , "o23" , "p23" , "q23" , "r23" , "g24" , "h24" , "i24" , "j24" , "k24" , "l24" , "m24" , "n24" , "o24" , "p24" , "q24" , "r24" , "e25" , "f25" , "g25" , "h25" , "i25" , "j25" , "k25" , "l25" , "m25" , "n25" , "o25" , "p25" , "q25" , "r25" , "s25" , "t25" , "e26" , "f26" , "g26" , "h26" , "i26" , "j26" , "k26" , "l26" , "m26" , "n26" , "o26" , "p26" , "q26" , "r26" , "s26" , "t26" , "e27" , "f27" , "g27" , "h27" , "i27" , "j27" , "o27" , "p27" , "q27" , "r27" , "s27" , "t27" , "e28" , "f28" , "g28" , "h28" , "i28" , "j28" , "o28" , "p28" , "q28" , "r28" , "s28" , "t28" ] color_fill(list_red, "red" ) list_black = [ "e5" , "f5" , "g5" , "h5" , "i5" , "j5" , "o5" , "p5" , "e6" , "f6" , "g6" , "h6" , "i6" , "j6" , "o6" , "p6" , "c7" , "d7" , "g7" , "h7" , "o7" , "p7" , "c8" , "d8" , "g8" , "h8" , "o8" , "p8" , "c9" , "d9" , "g9" , "h9" , "i9" , "j9" , "q9" , "r9" , "c10" , "d10" , "g10" , "h10" , "i10" , "j10" , "q10" , "r10" , "c11" , "d11" , "e11" , "f11" , "o11" , "p11" , "q11" , "r11" , "s11" , "t11" , "u11" , "v11" , "c12" , "d12" , "e12" , "f12" , "o12" , "p12" , "q12" , "r12" , "s12" , "t12" , "u12" , "v12" , "c29" , "d29" , "e29" , "f29" , "g29" , "h29" , "q29" , "r29" , "s29" , "t29" , "u29" , "v29" , "c30" , "d30" , "e30" , "f30" , "g30" , "h30" , "q30" , "r30" , "s30" , "t30" , "u30" , "v30" , "a31" , "b31" , "c31" , "d31" , "e31" , "f31" , "g31" , "h31" , "q31" , "r31" , "s31" , "t31" , "u31" , "v31" , "w31" , "x31" , "a32" , "b32" , "c32" , "d32" , "e32" , "f32" , "g32" , "h32" , "q32" , "r32" , "s32" , "t32" , "u32" , "v32" , "w32" , "x32" ] color_fill(list_black, "black" ) list_blue = [ "e15" , "f15" , "g15" , "h15" , "k15" , "l15" , "m15" , "n15" , "o15" , "p15" , "e16" , "f16" , "g16" , "h16" , "k16" , "l16" , "m16" , "n16" , "o16" , "p16" , "c17" , "d17" , "e17" , "f17" , "g17" , "h17" , "k17" , "l17" , "m17" , "n17" , "q17" , "r17" , "s17" , "t17" , "u17" , "v17" , "c18" , "d18" , "e18" , "f18" , "g18" , "h18" , "k18" , "l18" , "m18" , "n18" , "q18" , "r18" , "s18" , "t18" , "u18" , "v18" , "a19" , "b19" , "c19" , "d19" , "e19" , "f19" , "g19" , "h19" , "q19" , "r19" , "s19" , "t19" , "u19" , "v19" , "w19" , "x19" , "a20" , "b20" , "c20" , "d20" , "e20" , "f20" , "g20" , "h20" , "q20" , "r20" , "s20" , "t20" , "u20" , "v20" , "w20" , "x20" , "e21" , "f21" , "s21" , "t21" , "e22" , "f22" , "s22" , "t22" ] color_fill(list_blue, "blue" ) list_yellow = [ "k5" , "l5" , "m5" , "n5" , "q5" , "r5" , "k6" , "l6" , "m6" , "n6" , "q6" , "r6" , "e7" , "f7" , "i7" , "j7" , "k7" , "l7" , "m7" , "n7" , "q7" , "r7" , "s7" , "t7" , "u7" , "v7" , "e8" , "f8" , "i8" , "j8" , "k8" , "l8" , "m8" , "n8" , "q8" , "r8" , "s8" , "t8" , "u8" , "v8" , "e9" , "f9" , "k9" , "l9" , "m9" , "n9" , "o9" , "p9" , "s9" , "t9" , "u9" , "v9" , "w9" , "x9" , "e10" , "f10" , "k10" , "l10" , "m10" , "n10" , "o10" , "p10" , "s10" , "t10" , "u10" , "v10" , "w10" , "x10" , "g11" , "h11" , "i11" , "j11" , "k11" , "l11" , "m11" , "n11" , "g12" , "h12" , "i12" , "j12" , "k12" , "l12" , "m12" , "n12" , "g13" , "h13" , "i13" , "j13" , "k13" , "l13" , "m13" , "n13" , "o13" , "p13" , "q13" , "r13" , "s13" , "t13" , "g14" , "h14" , "i14" , "j14" , "k14" , "l14" , "m14" , "n14" , "o14" , "p14" , "q14" , "r14" , "s14" , "t14" , "a21" , "b21" , "c21" , "d21" , "i21" , "j21" , "o21" , "p21" , "u21" , "v21" , "w21" , "x21" , "a22" , "b22" , "c22" , "d22" , "i22" , "j22" , "o22" , "p22" , "u22" , "v22" , "w22" , "x22" , "a23" , "b23" , "c23" , "d23" , "e23" , "f23" , "s23" , "t23" , "u23" , "v23" , "w23" , "x23" , "a24" , "b24" , "c24" , "d24" , "e24" , "f24" , "s24" , "t24" , "u24" , "v24" , "w24" , "x24" , "a25" , "b25" , "c25" , "d25" , "u25" , "v25" , "w25" , "x25" , "a26" , "b26" , "c26" , "d26" , "u26" , "v26" , "w26" , "x26" ] color_fill(list_yellow, "yellow" ) # 4.设置行高、列宽 sheet = wb.active # 获取活动表 # 统一设置行高与列宽 width = 4.374 # 设置宽度 height = 27.682 # 设置高度 print ( "row:" , work.max_row, "column:" , work.max_column) # 打印行数,列数 for i in range ( 1 , work.max_row + 1 ): work.row_dimensions[i].height = height for i in range ( 1 , work.max_column + 1 ): work.column_dimensions[get_column_letter(i)].width = width # 5.边框控制 # sheet.title = "边框控制" border = Border(left = Side(border_style = 'thin' , color = '000000' ), right = Side(border_style = 'thin' , color = '000000' ), top = Side(border_style = 'thin' , color = '000000' ), bottom = Side(border_style = 'thin' , color = '000000' )) for i in [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ]: for j in range ( 1 , 33 ): s = i + str (j) sheet[s].border = border # 6.保存excel表 wb.active[ "A1" ].value = "超级玛丽" wb.save( 'test.xlsx' ) |
3.运行结果
row: 32 column: 24
查看目录下的test.xlsx文件,同第二步手工绘制的excel表结果一致
到此这篇关于Python利用openpyxl类实现在Excel中绘制乐高图案的文章就介绍到这了
2023-03-17
python flask项目打包成docker镜像发布的过程2023-03-17
python调试模块ipdb详解2023-03-17
python使用openai生成图像的超详细教程python cron定时任务触发接口自动化巡检 apscheduler报错:Run time of job …… next run at: ……)” was missed by misfire_grace_time参数 找到任务超时的根本原因...
2023-03-15