Skip to content

How to insert table in the middle of document #156

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

Open
hervekhg opened this issue Mar 6, 2015 · 23 comments
Open

How to insert table in the middle of document #156

hervekhg opened this issue Mar 6, 2015 · 23 comments
Labels

Comments

@hervekhg
Copy link

hervekhg commented Mar 6, 2015

Hello,
It's possible to insert table in a specific place of document (section place, page number) ? Something similar like "insert_paragraph_before" function but for table ?

All table that i've tried to add are systematically inserted in the bottom of my document, and i don't want it.

@hervekhg
Copy link
Author

hervekhg commented Mar 6, 2015

On the same line. How could I copy a table and paste it in another paragraph or section ?

tables = self.document.tables
tables[0] = tables[1]

@scanny
Copy link
Contributor

scanny commented Mar 7, 2015

Hi Herve, there's no API method for that just yet, but I think you can accomplish what you want with something like this:

def move_table_after(table, paragraph):
    tbl, p = table._tbl, paragraph._p
    p.addnext(tbl)

paragraph = paragraph_to_precede_new_table  # however you get this paragraph
table = document.add_table(...)
move_table_after(table, paragraph)

@hervekhg
Copy link
Author

hervekhg commented Mar 9, 2015

Thanks Steve the function work.
But It's not possible to just copy without moving ? A function copy_table_after ?

@scanny
Copy link
Contributor

scanny commented Mar 10, 2015

Something like this might work for you, depending on what you have in your table:

from copy import deepcopy

def copy_table_after(table, paragraph):
    tbl, p = table._tbl, paragraph._p
    new_tbl = deepcopy(tbl)
    p.addnext(new_tbl)

@hervekhg
Copy link
Author

Thanks Steve.

@jeffreinhart
Copy link

Another useful workaround. Thanks!

1 similar comment
@lovemyliwu
Copy link

Another useful workaround. Thanks!

@fabio-porcedda
Copy link

I wish to do the same but i didn't found a method called "addnext", it already exists or it must be created?

Thanks

@scanny
Copy link
Contributor

scanny commented Mar 7, 2017

.addnext() is an lxml method on an _Element object.

@fabio-porcedda
Copy link

Thanks.

Awesome the function "copy_table_after" works!

What is the best way to remove all row except the first one?

Thanks again.

@fabio-porcedda
Copy link

Nevermind, I found howto remove a row in #83

@amitanand
Copy link

Hi Herve, there's no API method for that just yet, but I think you can accomplish what you want with something like this:

def move_table_after(table, paragraph):
    tbl, p = table._tbl, paragraph._p
    p.addnext(tbl)

paragraph = paragraph_to_precede_new_table  # however you get this paragraph
table = document.add_table(...)
move_table_after(table, paragraph)

@amitanand
Copy link

paragraph = paragraph_to_precede_new_table # however you get this paragraph
I didnt get 'however you get this paragraph'. I have multiple paragraphs in my doc. IS there any way I can reference / choose a pargraph?

@ravi02011993
Copy link

Hi Herve, there's no API method for that just yet, but I think you can accomplish what you want with something like this:

def move_table_after(table, paragraph):
    tbl, p = table._tbl, paragraph._p
    p.addnext(tbl)

paragraph = paragraph_to_precede_new_table  # however you get this paragraph
table = document.add_table(...)
move_table_after(table, paragraph)

Hi Herve, there's no API method for that just yet, but I think you can accomplish what you want with something like this:

def move_table_after(table, paragraph):
    tbl, p = table._tbl, paragraph._p
    p.addnext(tbl)

paragraph = paragraph_to_precede_new_table  # however you get this paragraph
table = document.add_table(...)
move_table_after(table, paragraph)

Hi, I am using the python docx module. I am new to python. I wanted to insert a table between two paragraphs. How can this be done? I looked into the code above and I want to know if there is an api for it now?

Regards
Ravi

@Ginkooo
Copy link

Ginkooo commented Sep 14, 2020

If it messes with other tables in your document, instead of:

p.addnext(tbl)

do:

paragraph.add_run().element.addnext(tbl) # paragraph is not paragraph._p

@buhtz
Copy link

buhtz commented May 17, 2023

Not a solution but related question #1190

@JP-Reis
Copy link

JP-Reis commented Oct 4, 2023

Hello,

The code works fine but the search_phrase remains in the output document.
How could I do to remove just the line where the search_phrase is? I mean I want to find a search_phrase in the document and replace it with the table.

@scanny
Copy link
Contributor

scanny commented Oct 4, 2023

@JP-Reis A table is a block-item (as opposed to an inline item). So is a paragraph. So a table can only appear between two paragraphs (or other tables), not inside one.

So you could insert a table between two paragraphs or replace a paragraph with a table. But you can't replace a phrase with a table per-se.

You could potentially split the paragraph, removing the phrase, and insert the table between the two fragments. But you see my point I expect.

@JP-Reis
Copy link

JP-Reis commented Oct 4, 2023

I'm afraid I didn't explain it well or I didn't understand your answer.
What I'm trying to do is use a word template with some tags. These tags mark the location where I want to insert some elements, in this case tables.

Template example:
image

The output document should be as follow:
image

Don't know if its possible to do something similar to this.

Thanks in advance.

@scanny
Copy link
Contributor

scanny commented Oct 4, 2023

Okay, that seems straightforward enough. The approach at the top here should get that done for you: #156 (comment)

The only item remaining would be getting rid of the "placeholder" paragraph which you can do with:

paragraph._p.getparent().remove(paragraph._p)

@bio-Tao
Copy link

bio-Tao commented Oct 8, 2023

How do I copy a table and insert it behind the original table ,I use

copied_table = deepcopy(table)
table._tbl.addnext(copied_table._tbl)

copy table and original table are merged

@scanny
Copy link
Contributor

scanny commented Oct 9, 2023

Are they merged in the XML or does the client (which one are you using?) just merge them for display purposes?

I'm betting it's just being visually merged. Try putting a blank paragraph between the two tables.

@Mishidesh
Copy link

paragraph = paragraph_to_precede_new_table # however you get this paragraph
I didnt get 'however you get this paragraph'. I have multiple paragraphs in my doc. IS there any way I can reference / choose a pargraph?

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

No branches or pull requests