Inserting data into a non-organized table in word from SMO file

19 views Asked by At

Relatively new to programming, ive been banging my head against a wall now for several day (on my own time)

I need to export data from a list that ive collected through a sliced text and a collect numbers function. Ive come a long way by identifing tables in a word document, and completing this import/export process for other tables. However, the table I am trying to send data to this time is not well-built. (snippet included below).

Furthermore, in word it is a "frozen" heading. meaning it repeats itself at the following page. (its a designed template for work, friggin nightmare).

So theres 3 problems I need to solve.

  1. alternating goal columns (second/third)
  2. merged rows
  3. repeating headings.

Id love some advice on this. Not one for giving up, but its getting there.

Goal table (copied from word to excel for ease of interpretation, goal entry being the column of which I want to enter my list


# Open the existing Document object
doc = Document(Word_file)


# Find the '{Størrelser}' table
table_title = '{Størrelser}'
table = None
for tbl in doc.tables:
    if tbl.cell(0, 0).text.strip() == table_title:
        table = tbl
        break


## Add the values to the specific cell
for i, value in enumerate(Kontrollskjema):  # start=0 by default e
    # Calculate row index, +1 to start from the second row
    row_idx = i + 1
    # Set column index to 2 for the third column for rows 2,3,4,5 and 8,9,10,11
    # Else set it to 1 for the second column
    col_idx = 2 if row_idx in [2, 3, 4, 5, 8, 9, 10, 11] else 1

    # Check if the row exists
    if row_idx < len(table.rows):
        # Check if the row has the required column
        if len(table.rows[row_idx].cells) > col_idx:
            cell = table.cell(row_idx, col_idx)  # cell(row_idx, col_idx), 0-indexed
            cell.text = str(value)
            paragraph = cell.paragraphs[0]
            run = paragraph.runs[0]
            run.font.name = 'Avenir Next LT Pro'
            run.font.size = Pt(10)
            paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
        else: 
            print(f"Row {row_idx + 1} does not have a column {col_idx + 1}.")
    else:
        print(f"Row {row_idx + 1} does not exist.")


print(f"the length of the variable Kontrollskjema is {len(Kontrollskjema)}")

print(Kontrollskjema)

def print_rows_per_column(table):
    # Initialize a list to hold the count of rows for each column
    column_counts = []

    # Iterate over the rows of the table
    for row in table.rows:
        # Count the number of cells (columns) in the row
        column_count = len(row.cells)
        # If the list of column counts is not long enough, extend it
        while len(column_counts) < column_count:
            column_counts.append(0)
        # Increment the count for each column in the row
        for i in range(column_count):
            column_counts[i] += 1

    # Print the number of rows for each column
    for i, count in enumerate(column_counts, start=1):
        print(f"The number of rows in column {i} is {count}")

# Usage
print_rows_per_column(table)




#Output

# 26
Row 30 does not have a column 2.
Row 31 does not have a column 2.
Row 32 does not have a column 2.
Row 33 does not have a column 2.
Row 34 does not have a column 2.
Row 35 does not exist.
Row 36 does not exist.
the length of the variable Kontrollskjema is 35
[353, 135, 141, 16, 566, 1396, 0.19, 0.93, 0.2, 0.8, 0.06, 2.8, 0.06, 417, 9.0, 19, 2.5, 1.7, 0.0, 0.81, 25, 20.3, 0.0, 'Oppvarming 16/7/52\n', 'Oppvarming 16/7/52\nKjøling 24/7/52\nVentilasjon 24/7/52\nBelysning 16/7/52\nUtsyr 16/7/52\nPersoner 24/7/52\n', 1.95, 1.95, 3.0, 1.8, 3.4, 0.0, 1.5, 0.55, 0.2, '0.74/1.0/0.84/0.78']
The number of rows in column 1 is 29
The number of rows in column 2 is 29
The number of rows in column 3 is 28
The number of rows in column 4 is 28
The number of rows in column 5 is 28
The number of rows in column 6 is 28

0

There are 0 answers