Comments (9)
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.
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.
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.
@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.
@scanny Exactly.
from python-docx.
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.
Added Paragraph.insert_paragraph_before()
as feature #68.
Considering also adding Paragraph.insert_paragraph_after()
pending identification of differentiated use case.
from python-docx.
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.
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)
- Inline support for SVG file stream HOT 3
- How to add internal top and bottom table cell spacings?
- Inserting a new page before last page in the word document using python HOT 1
- Auto refresh Table of Contents using docx HOT 1
- OSS-Fuzz Integration
- Highlight certain words of a paragraph to bold HOT 1
- Paragraphs/Numbering in Table of Contents Document Regions HOT 3
- no read apis HOT 1
- feat: distinguish "no such file" from "not a ZIP"
- Workaround to set <w:bidi/> property on a paragraph's properties <w:pPr>? HOT 2
- docx2pdf issue HOT 1
- Python-docx detects a single paragraph with a single run with a single style -- when it should detect multiple paragraphs each having their own style HOT 9
- How do I add an xml object by coordinates?
- Values in tables are being splitted into multiple rows if there is \n in them HOT 2
- easily replacing / removing&adding image or text in header HOT 2
- support for Text Form Fields and Checkboxes
- Setting placeholder text in list of tables via field code 'TOC \\h \\z \\c "Table"'
- custom_xml_parts not available on Linux HOT 1
- Autoescape should be TRUE by default HOT 1
- Set line spacing does not display properly in word HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-docx.