python-3.x odfpy:需要复制现有的表

6mzjoqzu  于 2022-12-05  发布在  Python
关注(0)|答案(2)|浏览(200)

我在使用odfpy和Python 3时遇到了一个问题。我试图复制Opendocument文本文件中的一个现有表。我无法从原始表中获取样式信息。下面是我的代码:

# templateTable is a table.Table that has to be copied
for childNode in templateTable.childNodes:
    if 'style-name' in str(childNode.attributes):
        # the next command fails and the python interpreter tells me
        # 'style-name' is not in list
        style = childNode.getAttribute('style-name')

欢迎帮忙!

smdnsysy

smdnsysy1#

我终于自己找到了一个解决方案。我想和你分享它,以防别人不得不和我一起解决同样的问题。你可以找到代码here

def copyTable(document, tableSource, tableDestination):
    # document is of odf.opendocument
    # tableSource is a string with the name of the source table
    # tableDestination is a string with the name of the destination table
    
    # get all tables in document
    allTables = document.getElementsByType(table.Table)
    templateTable = None
    for xTable in allTables:
        if hasattr(xTable, 'attributes'):
            tableName = xTable.getAttribute('name')
            
            if tableName == tableSource:
                templateTable = xTable
                
    if templateTable is None:
        print('Table not found')
        return
        
    # first need to know about the columns in the original table
    cols = templateTable.getElementsByType(table.TableColumn)
    
    # create a new table for the copy
    copyTable = Table(name=tableDestination)
    
    # run through all columns and create a new column in the destination table
    # for each column in the source table
    for itm in cols:
        colNew = TableColumn()
        # copy the stylename of the column (needed for columnwidth
        colNew.setAttribute('stylename', itm.getAttribute('stylename') )
        
        # add the created column to the destination table
        copyTable.addElement(colNew)

    # get all rows from the original table
    rows = templateTable.getElementsByType(table.TableRow)
    
    #run through all rows
    for tempRow in rows:
        # create a new roz
        tr = TableRow()
        
        # get all cells from the original table
        cells = tempRow.getElementsByType(table.TableCell)
        
        # run through all cells 
        for tempCell in cells:
            # create a new cell
            cell = TableCell()
            
            # copy the attributes
            cell.setAttribute('stylename', tempCell.getAttribute('stylename') )
            cell.setAttribute('numbercolumnsspanned', tempCell.getAttribute('numbercolumnsspanned') )
            cell.setAttribute('numbermatrixcolumnsspanned', tempCell.getAttribute('numbermatrixcolumnsspanned') )
            
            # get all parapgraphs from the cell
            paras = tempCell.getElementsByType(text.P)
            for paraTemp in paras:
                # extract the text
                old_text = teletype.extractText(paraTemp)
                # create a new paragraph
                para_copy = P(text=old_text)
                # append paragraph to the cell
                cell.addElement(para_copy)
            
            # add the cell to the row   
            tr.addElement(cell)
            
        # append row to the destination table
        copyTable.addElement(tr)
        
    
    templateTable.parentNode.addElement(copyTable)
    templateTable.parentNode.addElement(P() )
y1aodyip

y1aodyip2#

对于任何还在这里的人来说,这是简单的:

def fetch_sheet_table_by_name(doc, name_of_sheet):
    for sheet_ in doc.spreadsheet.getElementsByType(Table):
        sheet_name_ = sheet_.getAttribute("name")
        if sheet_name_ == name_of_sheet:
            return sheet_
    return None

def copyTable(document, tableSource, tableDestination, temp_document_name):
    document.save(temp_document_name)
    temp_document = load(temp_document_name)
    table_to_copy = fetch_sheet_table_by_name(temp_document, tableSource)
    table_to_copy.setAttribute("name", tableDestination)
    document.spreadsheet.addElement(table_to_copy)
    return document

相关问题