1. 程式人生 > 其它 >Python excel轉換為table 主要是合併單元格問題解決

Python excel轉換為table 主要是合併單元格問題解決

我們公司最近又開始搞這些神奇的需求了

把excel表格 轉換為 html裡面的table,數量估計有幾百個,分了我幾十個,尼瑪,不想像他們那樣一個一個手搓,傷不起。。。
決定用python解決,後面附上程式碼

程式碼,未完成版



import os
import openpyxl
from openpyxl import Workbook
from copy import deepcopy
from openpyxl.utils import get_column_letter


# 原文:https://www.cnblogs.com/liuda9495/p/9039732.html



workbook2 = Workbook()




def in_area(arr_area, row, col):# 判斷是否在區域中
    for area in arr_area:
        if row >= area['r1'] and row <= area['r2'] and col >= area['c1'] and col <= area['c2']:
            #print('in_area')
            #print(area)            
            return area
    #print('not_in_area')
    return None


def is_first(area, row, col):# 判斷是否是區域的第一個單元格
    min_row = area['r1']  if area['r1'] < area['r2'] else area['r1']
    min_col = area['c1']  if area['c1'] < area['c2'] else area['c2']    
    if row==min_row and col==min_col:
        #print('is_first')
        #print('min_row,min_col', min_row, min_col)    
        return True
    #print('not_first')
    return False


def create_worksheet(path):

    #path='test1.xlsx'
    workbook = openpyxl.load_workbook(path)# 載入excel
    name_list = workbook.sheetnames# 所有sheet的名字
    
    for index, value in enumerate(name_list):
        print(index, value)    
        
        worksheet = workbook[name_list[index]]# 讀取第一個工作表

        # 獲取所有 合併單元格的 位置資訊
        # 是個可迭代物件,單個物件型別:openpyxl.worksheet.cell_range.CellRange
        # print後就是excel座標資訊
        m_list = worksheet.merged_cells

        l = deepcopy(m_list)# 深拷貝
        arr_area = []
        
        # 拆分合並的單元格 並填充內容
        for m_area in l:
            
            # 這裡的行和列的起始值(索引),和Excel的一樣,從1開始,並不是從0開始(注意)
            r1, r2, c1, c2 = m_area.min_row, m_area.max_row, m_area.min_col, m_area.max_col

            worksheet.unmerge_cells(start_row=r1, end_row=r2, start_column=c1, end_column=c2)
            print('區域:', m_area, '  座標:', r1, r2, c1, c2)
            arr_area.append({'r1':r1,'r2':r2,'c1':c1,'c2':c2,'m_area':str(m_area)})
            
            # # 獲取一個單元格的內容
            # first_value = worksheet.cell(r1, c1).value

            # # 區域 資料填充
            # for r in range(r1, r2+1):# 遍歷行        
                # if c2 - c1 > 0:# 多個列,遍歷列
                    # for c in range(c1, c2+1):
                        # worksheet.cell(r, c).value = first_value
                # else:# 一個列
                    # worksheet.cell(r, c1).value = first_value


        strHtml = ''
        
        # 遍歷行何列 座標從0開始
        for x in range(worksheet.max_row):
            strHtml += '<tr>'
            for y in range(worksheet.max_column):
                row = x + 1
                col = y + 1
                cellValue = worksheet.cell(row, col).value
                # print('單元格內容:', cellValue)
                
                area = in_area(arr_area, row, col)
                if not area is None:
                    if is_first(area, row, col):
                        rowspan = area['r2']-area['r1']+1
                        colspan = area['c2']-area['c1']+1
                        print('合併單元格:', area)
                        print('rowspan,colspan:', rowspan, colspan)
                        strHtml = '<td rowspan="{}" colspan="{}">{}</td>'.format(rowspan, colspan, cellValue)
                        print(strHtml)
                    else:
                        print('在合併單元格中,跳過')
                else:
                    print('單獨的單元格')
                
                
                #if cellValue is not None:
                    #print('null')
                    
            strHtml += '</tr>'

        print('arr_area------------')
        print(len(arr_area))
        print(arr_area)
        
        area = in_area(arr_area, 5, 1)
        if not area is None:
            is_first(area, 5, 1)        











#def each_files():    
    #pathDir =  os.listdir('./files/')
    #for index, value in enumerate(pathDir):
        #filepath2 = './files/' + value
        #print(filepath2)
        #create_worksheet(filepath2)
        


#each_files()
#workbook2.save('test2.xlsx')



# 遍歷行
# 遍歷列
# 單元格 在不在區域中
# 是否是 區域的開頭
# 如果是開頭,則用區域。如果不是開頭,則跳過



# 判斷一個單元格(行和列) 是否在區域中
# 判斷一個單元格 是否是區域的開頭







create_worksheet('E://1.xlsx')