-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
On the same line. How could I copy a table and paste it in another paragraph or section ? tables = self.document.tables |
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) |
Thanks Steve the function work. |
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) |
Thanks Steve. |
Another useful workaround. Thanks! |
1 similar comment
Another useful workaround. Thanks! |
I wish to do the same but i didn't found a method called "addnext", it already exists or it must be created? Thanks |
|
Thanks. Awesome the function "copy_table_after" works! What is the best way to remove all row except the first one? Thanks again. |
Nevermind, I found howto remove a row in #83 |
|
paragraph = paragraph_to_precede_new_table # however you get this 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 |
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 |
Not a solution but related question #1190 |
Hello, The code works fine but the search_phrase remains in the output document. |
@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. |
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) |
How do I copy a table and insert it behind the original table ,I use
copy table and original table are merged |
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. |
paragraph = paragraph_to_precede_new_table # however you get this paragraph |
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.
The text was updated successfully, but these errors were encountered: