Git Product home page Git Product logo

Comments (9)

scanny avatar scanny commented on September 27, 2024

I like your suggestion of adding an optional index parameter to the add methods. That seems like a natural extension and initially strikes me as cleaner than adding a separate set of `insert_x()' methods.

Another API approach that seems useful is something like paragraph.insert_paragraph_after() and .insert_paragraph_before(). I've come across situations where you have a paragraph you're essentially using as a cursor position and want to add items on one side or the other, i.e. above or below. I suppose that extends to insert_x_before/after() methods on Run and perhaps in-run objects as well (text, break, picture, etc.) although at the in-run level that does seem like it might proliferate methods in an unsightly way, n^2 where n is the number of in-run object types.

This notion ultimately takes one to a cursor concept, either an empty insertion point or a continuous range of characters, as is provided by the MS API in their Range object. That notion hadn't initially appealed to me, both because it would be a very significant implementation challenge and because it didn't seem terrifically intuitive to learn. Also I suspected it was somewhat forced on that design because the API works on a 'live' document running in an application window and that API mimics what happens when using the mouse or keyboard to select a range of text on which to operate. Now that I get further along in the project I do appreciate some of its virtues though, in particular concentrating this n^2 number of method calls down to n number. Also, it essentially introduces the concept of character and operations at that level, even though that's below the level of a single element in the XML.

I believe your second suggestion works today if you add .text to the left hand side, e.g. paragraphs[3].text = "foobar". I'm not sure I see an advantage to allowing direct string assignments to a paragraph object as a replacement for that. There is more than one reason to assign a string value to a paragraph object, e.g. paragraph = 'Heading 1' and it strikes me it contributes to readability to require the programmer to be explicit about which property they mean,paragraph.style` in this second example.

I suppose at the moment I'd be inclined to favor your first suggestion. It would be a minor extension to the API and seems like it would support the most important use cases if implemented on Paragraph and Run.

from python-docx.

scanny avatar scanny commented on September 27, 2024

Hi Evan, I just had another idea here I wanted to bounce off you.

Basically its having an .insert_paragraph_before() and .insert_paragraph_after() method, but a bit more abstract.

The idea would be actually to introduce a BlockElement class that both Paragraph and Table inherit from. Then BlockElement would have .insert_paragraph_before() (and after) and .insert_table_before() and after. The main concept being that you can use any block-level element as a 'cursor' to specify the relative position for inserting any new block-level element.

Extending the idea to runs, there would be something like RunLevelElement having .insert_run_before() and after, and whatever other run-level elements there are.

I think there would be a pretty elegant implementation for these, because the essential add operations are already there, and it would just be a case of getting the element inserted on the right side of a peer element.

What do you think?

from python-docx.

bslatkin avatar bslatkin commented on September 27, 2024

I ended up needing to work around this limitation, and here's what I did:

r = CT_R.new()
para._p.insert(1, r)
run = Run(r)
run.style = 'my style'
run.add_text('my text')

Pretty brittle because I needed to insert the run before everything else but after the paragraph's styling item. But it worked! Would be great if this were part of the python-docx API.

from python-docx.

scanny avatar scanny commented on September 27, 2024

@bslatkin can you say a little more about the use case this is a part of? I need a little more context to validate the API ideas I have about this one. Is it that you have an existing paragraph that has some content in it and you need to insert text ahead of it but in the same paragraph?

from python-docx.

bslatkin avatar bslatkin commented on September 27, 2024

@scanny Exactly.

from python-docx.

scanny avatar scanny commented on September 27, 2024

btw, you can do something like this that might be more robust for the time being:

r_cursor = r_element_to_insert_ahead_of()  # perhaps p.r_lst[0] or paragraph.runs[0]._r
r = CT_R.new()
r_cursor.addprevious(r)
...

from python-docx.

scanny avatar scanny commented on September 27, 2024

Added Paragraph.insert_paragraph_before() as feature #68.

Considering also adding Paragraph.insert_paragraph_after() pending identification of differentiated use case.

from python-docx.

igorsavinkin avatar igorsavinkin commented on September 27, 2024
r = CT_R.new()
para._p.insert(1, r)
run = Run(r)
run.style = 'my style'
run.add_text('my text')

I cannot import CT_R . How to do it?
from docx.oxml.text.paragraph import CT_R does not work...

from python-docx.

scanny avatar scanny commented on September 27, 2024

It's in docx.oxml.text.run.
https://github.com/python-openxml/python-docx/blob/master/docx/oxml/text/run.py#L22

from python-docx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.