python Excel數據寫入



import xlwt from xlrd import * from xlutils.copy import copy from datetime import datetime import time path = r"H:\pycharm\untitled2\python_study\test\write.xlsx" def write(): """寫入單個數據""" rb = xlwt.Workbook() sheet = rb.add_sheet(u'sheet1',cell_overwrite_ok=True) sheet.write(1,1,"foo") #向第0行第0列寫入foo rb.save(path) def wirte02(): """寫入一列數據,不能修改""" f = xlwt.Workbook() #創建工作簿 sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #創建sheet l_=range(5) x = [42,4,14,14,14] for i in l_: sheet1.write(0,i,x[i]) #write的第一個,第二個參數時坐標, 第三個是要寫入的數據 sheet1.write(1,i,x[i]) #sheet1.write(0,0,start_date,set_style('Times New Roman',220,True)) f.save("2.xlsx")#保存文件 def write03(): """修改數據""" rb =open_workbook(path) wb = copy(rb) s = wb.get_sheet(0) s.write(0,1,"new data") wb.save("new_data.xls") print ("write finished") def style1(): """設置樣式""" font = xlwt.Font() # Create the font font.name = 'Times New Roman' font.bold = True font.underline = True font.italic = True font.colour_index = 2 style0 = xlwt.XFStyle() style0.font = font # Add font to Style wb = xlwt.Workbook() ws = wb.add_sheet('A Test Sheet') style1 = xlwt.XFStyle() style1.num_format_str = 'D-M-Y' ws.write(0, 0, 'Test', style0) # ws.write(2, 2, xlwt.Formula("A3+B3")) ws.write(2,2,time.strftime( "%Y-%m-%d %X", time.localtime()),style1) #寫入當前時間,精確到分鍾 wb.save('example425.xls') if __name__=="__main__": write() write02() write03() style1()

    

    

   

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM