python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.63k stars 1.13k forks source link

feature: _Cell.add_picture() #10

Open marazmiki opened 10 years ago

marazmiki commented 10 years ago

I understand I cannot do something like table_cell[0].add_picture() as well as with document?

Can you implement this feautre or I I shouldn't want this issue? :-)

scanny commented 10 years ago

Hi Mikhail, this is definitely a feature that's on the roadmap, although it's perhaps not entirely clear what the specific API call(s) will be for it.

It will likely be some months before it arrives though, so you might want to dig into it yourself if you need it in a hurry.

You should be able to re-use about 90% of the code from document.add_picture(). It would just be swapping in a paragraph from inside the table instead of creating a new one at the end of the document.

Now that I think about it for a minute, a paragraph.add_picture() or perhaps a run.add_picture() method would do the trick, because you can get a paragraph and/or run inside a table cell.

Let me know what you think or if you need more to go on.

As far as issues go, we'd want to be more specific on a feature request about this. Adding a picture is one thing, the other non-text content you can add is actually another (nested) table. I'm sure they would be done separately and that the picture would come well before a nested table.

ljean commented 10 years ago

Hello,

I also need this feature. I've looked at the possibility to add run.add_picture but it seems that the structure of the run is different from the document and I don't see how to reuse the code of document.add_picture()

What I need to do is to have an image and a text side by side, which seems not possible at the moment. Table may be the right way to do that.

Can you please give more advices about implementing it.

Best luc

scanny commented 10 years ago

There's some fresh action on this topic in this issue: https://github.com/python-openxml/python-docx/issues/14

And some additional detail here: https://github.com/python-openxml/python-docx/issues/12#issuecomment-35953742

I think we've got the implementation pretty well worked out, now it's down to how to get it into the build.

scanny commented 10 years ago

Added Run.add_picture() in release v0.7.1 a couple days ago. This allows a picture to be added in a table cell. If the table cell is fresh from an add_table() call, the code looks like this:

paragraph = cell.paragraphs[0]
run = paragraph.add_run()
run.add_picture('image.png')

The add_picture() call has the same signature as Document.add_picture() and appears in the documentation.

I'm leaving this issue open because I'm considering adding a _Cell.add_picture() method that does what one would typically want in most cases with a single call.

JLLeitschuh commented 9 years ago

Could you add documentation about this on the main page. That would be awesome!