Repository navigation
Add support for editing textbox content - #960
RikVoorhaar wants to merge 1 commit into
Conversation
|
It's generally well worthwhile to determine how the Microsoft Word API accomplishes the task and model after that. The few times I didn't early on turned out to be very costly in terms of rework. Generally I start with a Google search on "vba word {X}", like "how do I do this in Visual Basic" and that will get me into the right neck of the woods. From there I can find the details from the MS Word API documentation: http://msdn.microsoft.com/en-us/library/office/ff837519.aspx. Based on a quick look, it appears that a textbox is a You'll want to understand the difference between the (flowed) "story layer" (aka. document layer, text layer) and the arbitrarily positioned "drawing layer". All "floating" things are in the drawing layer. The existing "inline" picture shapes (like |
|
Sorry for the slow reply -- busy period. What you're saying makes sense, what I did was very much hacked together. What I did works for my personal needs, and I unfortunately don't have much time to dig much deeper into this. I'll close the PR. If anyone wants to work on this, perhaps my work can be a foundation. |
fixes #524, partially addresses #413
Creating text boxes is a little complicated, the main problem is current lack of support for floating shapes. However, editing text boxes is not very hard to implement. My personal use case is the need to create word documents from a template, and templates often contain text boxes.
I have no prior experience working with this package, or XML / word documents in general, so I will need some help with this.
Analysis
It seems that in the OXML standard textboxes always appear inside an
<mc:AlternateContent>element. The reason is that text boxes (and shapes in general) were originally implemented using VML, but since word 2010 it preferentially uses drawingML. This means that now the XML always stores two identical copies of the text box, but for legacy reasons uses two different methods of storing the shape.Therefore I define a textbox by an
<mc:AlternateContent>element with two descendant<w:txbxContent>elements with an xpath as follow:Remarks
Whenever we edit a text box, we need to update these two elements in the exact same way. This is managed by my
TextBoxclass, and the user should never have to care about this.In order to get this to work I had to add namespaces
"mc","v", and"wps"tons.py.It seems text boxes are stored in the first run of the first paragraph of the document, at least for one-page documents. In order to make text boxes easy to access I added a
textboxesproperty to theDocumentclass that recursively searches for all text boxes in the entire document. This is probably not the best way to do this, but I can't think of a better way.I haven't written any tests yet, but at least it works fine for my personal usecase. Since this doesn't support creation of text boxes, but only editing existing ones, we need to include a test document to edit. Not 100% what the best way to go about that is.
Docs also need to be updated