您好,登錄后才能下訂單哦!
這篇文章主要講解了“python如何使用xlwt操作excel”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“python如何使用xlwt操作excel”吧!
pip install xlwt
import xlwt # 創建一個workbook并設置編碼 workbook = xlwt.Workbook(encoding = 'utf-8') # 添加sheet worksheet = workbook.add_sheet('飛兔小哥') # 寫入excel, 參數對應 行, 列, 值 worksheet.write(1,0, label = '我愛autofelix') # 保存 workbook.save('xlwt教學.xls')
import xlwt workbook = xlwt.Workbook(encoding = 'ascii') worksheet = workbook.add_sheet('飛兔小哥') # 初始化樣式 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, '帶樣式的單元格', style) # 保存文件 workbook.save('xwlt單元格樣式教學.xls')
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飛兔小哥') worksheet.write(0, 0,'我愛autofelix') # 設置單元格寬度 worksheet.col(0).width = 3000 workbook.save('xwlt單元格寬度教學.xls')
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飛兔小哥')# 創建模式對象Create the Patternpattern = xlwt.Pattern()# May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12pattern.pattern = xlwt.Pattern.SOLID_PATTERN#設置模式顏色 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...pattern.pattern_fore_colour = 5# 創建樣式對象Create the Patternstyle = xlwt.XFStyle()# 將模式加入到樣式對象Add Pattern to Stylestyle.pattern = pattern#向單元格寫入內容時使用樣式對象styleworksheet.write(0, 0, '單元格內容', style)workbook.save('xwlt單元格背景色教學.xls')import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飛兔小哥') # 創建模式對象Create the Pattern pattern = xlwt.Pattern() # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12 pattern.pattern = xlwt.Pattern.SOLID_PATTERN #設置模式顏色 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... pattern.pattern_fore_colour = 5 # 創建樣式對象Create the Pattern style = xlwt.XFStyle() # 將模式加入到樣式對象Add Pattern to Style style.pattern = pattern #向單元格寫入內容時使用樣式對象style worksheet.write(0, 0, '單元格內容', style) workbook.save('xwlt單元格背景色教學.xls')
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飛兔小哥') # 創建對其格式的對象 Create Alignment alignment = xlwt.Alignment() #水平居中 May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED alignment.horz = xlwt.Alignment.HORZ_CENTER #我上下對齊 May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED alignment.vert = xlwt.Alignment.VERT_CENTER #創建樣式對象 Create Style style = xlwt.XFStyle() # 將格式Alignment對象加入到樣式對象Add Alignment to Style style.alignment = alignment #寫入的時候調用樣式style worksheet.write(0, 0, '單元居中', style) workbook.save('xlwt單元格對齊教學.xls')
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飛兔小哥') worksheet.write(0, 0, xlwt.Formula('HYPERLINK("https://www.baidu.com";"百度")')) workbook.save('xwlt單元格超鏈接教學.xls')
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飛兔小哥') worksheet.write(0, 0, 5) # Outputs 5 #第一行第一列5 worksheet.write(0, 1, 2) # Outputs 2 #第一行第二列2 #(A1[5] * A2[2]) 第二行第一列,輸出第一行數字乘積 worksheet.write(1, 0, xlwt.Formula('A1*B1')) #(A1[5] + A2[2]) 第二行第二列,輸出第一行數字之和 worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)')) workbook.save('xwlt添加公式教學.xls')
import xlwt import datetime workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飛兔小哥') style = xlwt.XFStyle() #其他格式包括: 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 style.num_format_str = 'M/D/YY' worksheet.write(0, 0, datetime.datetime.now(), style) workbook.save('xwlt日期格式教學.xlsx')
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飛兔小哥') worksheet.write_merge(0, 1, 0, 3, '合并從第一行到第二行,第一列到第四列') #創建字體示例 font = xlwt.Font() #bold設置為黑體字 font.bold = True # Create Style style = xlwt.XFStyle() # Add Bold Font to Style style.font = font worksheet.write_merge(2,3, 0, 3, '合并從第三行到第四行,第一列到第四列', style) workbook.save('xwlt合并單元格教學.xls')
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飛兔小哥')# 創建邊框對象Create Bordersborders = xlwt.Borders()#對邊框對象進行操作,指定邊框上下左右的邊框類型為虛線# 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.left = xlwt.Borders.DASHEDborders.right = xlwt.Borders.DASHEDborders.top = xlwt.Borders.DASHEDborders.bottom = xlwt.Borders.DASHED#指定上下左右的邊框顏色為0x40borders.left_colour = 0x40borders.right_colour = 0x40borders.top_colour = 0x40borders.bottom_colour = 0x40# Create Style #創建樣式對象style = xlwt.XFStyle()# 將設置好的邊框對象borders 加到樣式對象style中。Add Borders to Stylestyle.borders = borders#向單元格第一行第一列寫入“單元格內容”,并使用創建好的樣式對象styleworksheet.write(0, 0, '單元格內容', style)workbook.save('xwlt添加邊框教學.xls')import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飛兔小哥') # 創建邊框對象Create Borders borders = xlwt.Borders() #對邊框對象進行操作,指定邊框上下左右的邊框類型為虛線 # 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.left = xlwt.Borders.DASHED borders.right = xlwt.Borders.DASHED borders.top = xlwt.Borders.DASHED borders.bottom = xlwt.Borders.DASHED #指定上下左右的邊框顏色為0x40 borders.left_colour = 0x40 borders.right_colour = 0x40 borders.top_colour = 0x40 borders.bottom_colour = 0x40 # Create Style #創建樣式對象 style = xlwt.XFStyle() # 將設置好的邊框對象borders 加到樣式對象style中。Add Borders to Style style.borders = borders #向單元格第一行第一列寫入“單元格內容”,并使用創建好的樣式對象style worksheet.write(0, 0, '單元格內容', style) workbook.save('xwlt添加邊框教學.xls')
感謝各位的閱讀,以上就是“python如何使用xlwt操作excel”的內容了,經過本文的學習后,相信大家對python如何使用xlwt操作excel這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。