本文主要为大家详细介绍了Python中使用xlwt模块设置单元格的一些基本样式,如设置单元格的背景颜色,下框线,字体,字体的颜色,设置列宽行高,插入简单的图片,希望能帮助到大家。
详细程序如下:
#!/usr/bin/env python # -*- coding: utf-8 -*-” #只对当前文件的中文编码有效 #Create by zhizaiqianli 2015-12-12 Version V1.0 #!/usr/bin/python # Filename : Write_excel_Format.py import os import time from xlwt import * from Mymodule import cur_file_dir filename = 'TestData2.xls' #检测当前目录下是否有TestData2.xls文件,如果有则清除以前保存文件 if os.path.exists(filename): os.remove(filename) print time.strftime("%Y-%m-%d",time.localtime(time.time())) #打印读取到当前系统时间 wbk = Workbook(encoding='utf-8') sheet = wbk.add_sheet('new sheet 1', cell_overwrite_ok=True) #第二参数用于确认同一个cell单元是否可以重设值。 style = XFStyle() #赋值style为XFStyle(),初始化样式 for i in range(0x00,0xff): # 设置单元格背景颜色 pattern = Pattern() # 创建一个模式 pattern.pattern = Pattern.SOLID_PATTERN # 设置其模式为实型 pattern.pattern_fore_colour = i # 设置单元格背景颜色 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, the list goes on... style.pattern = pattern # 将赋值好的模式参数导入Style Line_data = (u'测试表') #创建一个Line_data列表,并将其值赋为测试表,以utf-8编码时中文前加u sheet.write_merge(i, i, 0, 2, Line_data, style) #以合并单元格形式写入数据,即将数据写入以第1/2/3列合并德单元格内 for i in range(0x00,0xff): # 设置单元格内字体样式 fnt = Font() # 创建一个文本格式,包括字体、字号和颜色样式特性 fnt.name = u'微软雅黑' # 设置其字体为微软雅黑 fnt.colour_index = i # 设置其字体颜色 fnt.bold = True style.font = fnt #将赋值好的模式参数导入Style sheet.write_merge(i,i,3,5,Line_data,style) #以合并单元格形式写入数据,即将数据写入以第4/5/6列合并德单元格内 for i in range(0, 0x53): # 设置单元格下框线样式 borders = Borders() borders.left = i borders.right = i borders.top = i borders.bottom = i style.borders = borders #将赋值好的模式参数导入Style sheet.write_merge(i,i,6,8,Line_data,style) #以合并单元格形式写入数据,即将数据写入以第4/5/6列合并德单元格内 for i in range(6, 80): # 设置单元格下列宽样式 sheet.write(0,i,Line_data,style) sheet.col(i).width = 0x0d00 + i*50 path_py = "\\images\python.bmp" #读取插入图片以.py运行时路径,images和.py在同一目录下 path_exe = "\\images\python.bmp" #读取插入图片以.exe运行时路径,.exe可以移到其他任意目录下运行但images和.exe在同一目录下 path = cur_file_dir(path_py,path_exe) #获取文件的相对路径 filename = path #检测当前目录下是否有python.bmp图片, if os.path.exists(filename): print u'python.bmp图片存在' else: print u'python.bmp图片不存在' sheet.insert_bitmap(path, 2, 9) #插入一个图片 wbk.save('TestData2.xls') #保存TestData2.xls文件,保存到脚本或exe文件运行的目录下 raw_input("Enter enter key to exit...") #插入一个输入命令,方便运行exe时一闪而过不到打印信息
以cmd命令运行 Write_excel_Format.py时结果和生成表格如下所示:
相关推荐:
python使用xlrd与xlwt对excel的读写和格式设定
以上是Python中xlwt設定excel單元格字體及格式方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

WebStorm Mac版
好用的JavaScript開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能