Python利用openpyxl類實(shí)現(xiàn)在Excel中繪制樂高圖案
一、背景
在商場看到一個(gè)超級(jí)瑪麗的樂高圖
感覺使用excel的顏色填充也能畫出來,并且可以借助python來實(shí)現(xiàn)
二、excel表如何繪制正方形
1.統(tǒng)一設(shè)置行高與列寬
excel表單元格的行與列的默認(rèn)計(jì)量單位是不一樣的,設(shè)置如何一樣的數(shù)值并構(gòu)成正方形:
行的默認(rèn)單位是:磅; 而一個(gè)列寬單位等于“常規(guī)”樣式中一個(gè)字符的寬度
行高1長度為0.365mm,列寬1是2.25mm,比例就是0.365 : 2.25
設(shè)置excel表單元格行高為: 27.682
設(shè)置excel表單元格列寬為: 4.374
2.根據(jù)照片中的圖案分別使用 紅、黃、藍(lán)、黑顏色進(jìn)行填充
白色即為默認(rèn)背景色,完成填充如下:
三、python實(shí)現(xiàn)方案
openpyxl類
1.核心語句如下:
openpyxl.Workbook() #創(chuàng)建excel表
openpyxl.Workbook().save #保存excel表
fill = PatternFill('solid', fgColor='FFFF00') #顏色填充
sheet["A1"].border = border #設(shè)置邊框
work.row_dimensions[i].height = 27.682 #設(shè)置行高
work.column_dimensions[get_column_letter(i)].width = 4.374 #設(shè)置列寬
workbook.active["A1"].value = "test001" 設(shè)置單元格的值
2.實(shí)現(xiàn)代碼
import openpyxl from openpyxl import load_workbook # d打開excel表 from openpyxl.styles import PatternFill # 填充單元格的顏色 from openpyxl.utils import get_column_letter # 設(shè)置行高,列寬 from openpyxl.styles import Border, Side # 設(shè)置邊框 # 1.創(chuàng)建并打開test.xlsx new_excel = openpyxl.Workbook() new_excel.save("./test.xlsx") wb = load_workbook(filename='test.xlsx') # 使用第一個(gè)sheet作為工作簿 work = wb[wb.sheetnames[0]] sheet = wb['Sheet'] # 2.定義顏色填充方法,目前只涉及紅、黑、藍(lán)、黃、白 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.先初始化白色,再分別設(shè)置紅、黑、藍(lán)、黃色填充 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.設(shè)置行高、列寬 sheet = wb.active # 獲取活動(dòng)表 # 統(tǒng)一設(shè)置行高與列寬 width = 4.374 # 設(shè)置寬度 height = 27.682 # 設(shè)置高度 print("row:", work.max_row, "column:", work.max_column) # 打印行數(shù),列數(shù) 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 = "超級(jí)瑪麗" wb.save('test.xlsx')
3.運(yùn)行結(jié)果
row: 32 column: 24
查看目錄下的test.xlsx文件,同第二步手工繪制的excel表結(jié)果一致
到此這篇關(guān)于Python利用openpyxl類實(shí)現(xiàn)在Excel中繪制樂高圖案的文章就介紹到這了,更多相關(guān)Python openpyxl繪制樂高內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用python實(shí)現(xiàn)詞頻統(tǒng)計(jì)分析的代碼示例
詞頻統(tǒng)計(jì)是指在文本或語音數(shù)據(jù)中,統(tǒng)計(jì)每個(gè)單詞或符號(hào)出現(xiàn)的次數(shù),以便對(duì)文本或語音數(shù)據(jù)進(jìn),這篇文章將詳細(xì)介紹分詞后如何進(jìn)行詞頻統(tǒng)計(jì)分析2023-06-06教你用Python寫一個(gè)京東自動(dòng)下單搶購腳本
很多朋友都有網(wǎng)購搶購限量商品的經(jīng)歷,有時(shí)候蹲點(diǎn)搶怎么也搶不到,今天小編帶你們學(xué)習(xí)怎么用Python寫一個(gè)京東自動(dòng)下單搶購腳本,以后再也不用拼手速拼網(wǎng)速啦,快來一起看看吧2023-03-03opencv 圖像加法與圖像融合的實(shí)現(xiàn)代碼
這篇文章主要介紹了opencv 圖像加法與圖像融合的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07初學(xué)python的操作難點(diǎn)總結(jié)(新手必看篇)
下面小編就為大家?guī)硪黄鯇W(xué)python的操作難點(diǎn)總結(jié)(新手必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08PyCharm實(shí)現(xiàn)本地恢復(fù)或查看歷史代碼
這篇文章主要介紹了PyCharm實(shí)現(xiàn)本地恢復(fù)或查看歷史代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Python登錄QQ郵箱發(fā)送郵件的實(shí)現(xiàn)示例
本文主要介紹了Python登錄QQ郵箱發(fā)送郵件的實(shí)現(xiàn)示例,主要就是三步,登錄郵件、寫郵件內(nèi)容、發(fā)送,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2023-08-08python中查找excel某一列的重復(fù)數(shù)據(jù) 剔除之后打印
python查找excel某一列的重復(fù)數(shù)據(jù),剔除之后打印,供大家學(xué)習(xí)參考2013-02-02Python?的counter()函數(shù)解析與示例詳解
在?Python?中,?collections?模塊提供了?Counter?類,用于計(jì)算可迭代對(duì)象中元素的數(shù)量,?Counter?是一個(gè)字典的子類,它以元素作為鍵,以元素出現(xiàn)的次數(shù)作為值進(jìn)行計(jì)數(shù),本文給大家介紹Python?的counter()函數(shù),感興趣的朋友一起看看吧2023-08-08