官方文檔:xlsxwriter 官方文檔html
微博只對經常使用的方法作了總結和coding,詳細方法請參考官方文檔excel
常規寫入數據:code
寫入字符串write_string()
import xlsxwriter workbook = xlsxwriter.Workbook("file_name") worksheet = workbook.add_worksheet("sheet_name") worksheet.write_string(row, col, 'Your text here') # 把excel看爲座標,row第幾行, col第幾列 worksheet.write_string('A2', 'or here') # A2爲excel對應的位置 workbook.close()
import xlsxwriter workbook = xlsxwriter.Workbook("file_name.xls") worksheet = workbook.add_worksheet("sheet_name") worksheet.write_number(4, 5, 2.02) # 把excel看爲座標,row第幾行, col第幾列 worksheet.write_number('A2', 10) # A2爲excel對應的位置 workbook.close()
import xlsxwriter if __name__ == "__main__": workbook = xlsxwriter.Workbook("file_name.xls") worksheet = workbook.add_worksheet("sheet_name") worksheet.merge_range(2, 1, 3, 3, 'Merged Cells') # 2,1 表示excel的B3 3,3 表示excelD4 # worksheet.merge_range('B3:D4', 'Merged Cells',) workbook.close()
import xlsxwriter if __name__ == "__main__": workbook = xlsxwriter.Workbook("file_name.xls") worksheet = workbook.add_worksheet("sheet_name") worksheet.set_row(0, 60) # 第一行的高度設爲60 worksheet.set_column(0, 3, 30) # 從第一列到第二列的寬度設爲30 workbook.close()
import xlsxwriter if __name__ == "__main__": workbook = xlsxwriter.Workbook("file_name.xls") worksheet = workbook.add_worksheet("sheet_name") log_add = 'F:\\baiyang\Python\Code\logo.bmp' # 圖片地址 worksheet.insert_image(0, 0, log_add, options={"x_offset": 20, "y_offset": 0}) # 在某個單元格插入圖片,0,0 對應單元格A1, x_offset左右偏移,y_offset上下偏移 worksheet.insert_image("A2",log_add, options={"x_offset": 20, "y_offset": 0}) # 在某個單元格插入圖片,0,0 對應單元格A1,options爲設置在單元格內的位置, worksheet.set_row(0, 60) # 第一行的高度設爲60 worksheet.set_column(0, 3, 30) # 從第一列到第二列的寬度設爲30 workbook.close()
import xlsxwriter if __name__ == "__main__": workbook = xlsxwriter.Workbook("file_name.xls") worksheet = workbook.add_worksheet("sheet_name") merge_format = workbook.add_format({ 'bold': True, 'border': 6, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#D7E4BC', }) worksheet.merge_range('B3:D4', 'Merged Cells', merge_format) # merge_format爲定義的樣式 workbook.close()
import xlsxwriter if __name__ == "__main__": workbook = xlsxwriter.Workbook("file_name.xls") worksheet = workbook.add_worksheet("sheet_name") merge_format = workbook.add_format({ 'bold': True, 'border': 6, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#D7E4BC', }) red = workbook.add_format({'color': 'red', "bold": True, "font_name": "微軟雅黑"}) blank = workbook.add_format({"bold": True, "font_name": "微軟雅黑"}) row = 0 col = 0 string1 = "你好!" # 第一個樣式, string2 = "早安!" # 第二個樣式, 一次類推能夠寫n個樣式,後邊接着的是該樣式的參數 worksheet.write_rich_string(row, col, blank, string1, red, string2, merge_format)# row,col定位單元格 workbook.close()