Can Pandas read and modify a single Excel file worksheet (tab) without modifying the rest of the file?

前端 未结 6 925
清歌不尽
清歌不尽 2020-12-08 21:19

Many spreadsheets have formulas and formatting that Python tools for reading and writing Excel files cannot faithfully reproduce. That means that any file I want to create p

6条回答
  •  时光取名叫无心
    2020-12-08 22:08

    Required: call path to exist excels file.

    Input: List string.

    Output: append row.

    from datetime import datetime,timedelta
    from openpyxl import load_workbook,Workbook
           
       def write_log_excels(status):
                   try:
                       # Point
                       log_list = ["1","2","3","4","5,"6","7","8",  "9"]
                       date_n = datetime.now()
                       date_n = date_n.strftime("%Y-%m-%d %H:%M:%S")
                       sdate = date_n
       
                       wk = load_workbook('filename.xlsx')
                       wh = wk.active
                       lenth = wh.max_row
                       # wk.close()
                       pl = log_list
                       if lenth == 0:
                           # ws = Workbook()
                           # wb = ws.active
                           wh['A1'] = 'TITLE1'
                           wh['B1'] = 'TITLE2'
                           wh['C1'] = 'TITLE3'
                           wh['D1'] = 'TITLE4'
                           wh['E1'] = 'TITLE5'
                           wh['F1'] = 'TITLE6'
                           wh['G1'] = 'TITLE7'
                           wh['H1'] = 'TITLE8'
                           wh['I1'] = 'TITLE9'
                           lenth = 1
                       if pl is not None:
                           w = lenth + 1
                           wh['A{}'.format(w)] =  pl[0]
                           wh['B{}'.format(w)] =  pl[1]
                           wh['C{}'.format(w)] =  pl[2]
                           wh['D{}'.format(w)] =  pl[3]
                           wh['E{}'.format(w)] =  pl[4]
                           wh['F{}'.format(w)] =  pl[5]
                           wh['G{}'.format(w)] =  pl[3]
                           wh['H{}'.format(w)] =  pl[4]
                           wh['I{}'.format(w)] =  pl[5]
                       wk.save('filename.xlsx')
           
                       log_list.clear()
                   except Exception as e:
                       print('write_log_excels :' + str(e))
           write_log_excels('')
    

    Or using this for auto create col,row.

    def work_sheet(wsheet):
        data_sheet = []
        col = [] #column in sheet
        for c in range(wsheet.max_column):
            #got alphabels with max_(len)_column found in worksheet
            col.append(string.ascii_uppercase[c])
    
        for r in range(2,wsheet.max_row + 1):
            data_row = []
            for c in range(len(col)):
                #got values exactly with "sheet[colum-row]"
                data = wsheet['{}{}'.format(col[c],r)].value
                data_row.append(data)
            data_sheet.append(data_row)
        return data_sheet
    

提交回复
热议问题