Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError while acess cell._tc.bottom #1458

Open
mepwang opened this issue Dec 27, 2024 · 3 comments
Open

ValueError while acess cell._tc.bottom #1458

mepwang opened this issue Dec 27, 2024 · 3 comments

Comments

@mepwang
Copy link

mepwang commented Dec 27, 2024

OS: updated ubuntu 24.04
Python: 3.12.8
Python-Docx: 1.1.2

test.docx

while parsing table in docx, ValueError exception occurred:
ValueError: no tc element at grid_offset=3

in line: bottom = cell._tc.bottom

code:

    for row_idx, row in enumerate(table.rows):
        for cell_idx, cell in enumerate(row.cells):
            top = cell._tc.top
            bottom = cell._tc.bottom
            left = cell._tc.left
            right = cell._tc.right

docx file: see test.docx in attachment

@mepwang
Copy link
Author

mepwang commented Dec 27, 2024

it's a bug in oxml/table.py, line 79, function tc_at_grid_offset(self, grid_offset: int) -> CT_Tc

@alterlv
Copy link

alterlv commented Dec 31, 2024

I also encountered this error

@mepwang
Copy link
Author

mepwang commented Dec 31, 2024

I also encountered this error

fix tc_at_grid_offset implementation in file src/docx/oxml/table.py:

    def tc_at_grid_offset(self, grid_offset: int) -> CT_Tc:
        """The `tc` element in this tr at exact `grid offset`.

        Raises ValueError when this `w:tr` contains no `w:tc` with exact starting `grid_offset`.
        """
        # -- account for omitted cells at the start of the row --
        
        if grid_offset < self.grid_before:
            raise ValueError(f"no `tc` element at grid_offset={grid_offset}")
        
        cell_dict = dict()
        cell_index = 0
        for tc in self.tc_lst:
            for _ in range(tc.grid_span):
                cell_dict[cell_index] = tc
                cell_index += 1
                
        if grid_offset not in cell_dict:
            raise ValueError(f"no `tc` element at grid_offset={grid_offset}")
        
        return cell_dict[grid_offset]
    
        # remaining_offset = grid_offset - self.grid_before

        # for tc in self.tc_lst:
        #     # -- We've gone past grid_offset without finding a tc, no sense searching further. --
        #     if remaining_offset < 0:
        #         break
        #     # -- We've arrived at grid_offset, this is the `w:tc` we're looking for. --
        #     if remaining_offset == 0:
        #         return tc
        #     # -- We're not there yet, skip forward the number of layout-grid cells this cell
        #     # -- occupies.
        #     remaining_offset -= tc.grid_span

        # raise ValueError(f"no `tc` element at grid_offset={grid_offset}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants