-
Notifications
You must be signed in to change notification settings - Fork 1.2k
inserting xml-snippet into docx using the python-docx api #55
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
Hi Sub, can you give me an idea what the snippet you need to insert would look like? There are a couple different possible approaches. The best choice probably depends mostly on the size of the snippet, but if you could provide some example XML, like we want to insert something like: Also, if you could mention how you plan to identify the right place, that would be a help, like what object you'll have a reference to as a starting point. |
Hi Scanny, we would need such a feature to use it as a workaround for the following four requirements if not available in a simpler way with the API:
We are still studying how to specify the location and also how to insert the snippet. On 22 May 2014, at 10:46, scanny [email protected] wrote:
|
These would be four separate jobs, each requiring its own method, although the same approach would probably work for all four. In general I'd say these are small enough that inserting the elements one at a time using the lxml API would be the best approach. A good approach is to do a before and after diff using opc-diag (http://opc-diag.readthedocs.org/). You create the simplest possible file containing the "before" situation, like a 2x2 table with regular orientation headers, perhaps saved as "before.docx". Then you make the change you want using Word and save it again as "after.docx". Then you diff the two files with opc-diag with something like:
This should narrow right down what XML changes need to be made. If you can send me one of those diffs for, say, header text orientation, I can give you an example code snippet to get you going. |
as requested for table-header-orientation: I am sending you only the opc-diag diffs found for document.xml and ignoring diffs found for core.xml , app.xml & settings.xml. — sub @@ -21,21 +21,27 @@
@@ -56,7 +62,7 @@
|
Ok, good, this narrows it right down. First thing is we can ignore all the changes to The operative changes are:
I give an example here of the textDirection element since that seems to be the key one: from docx.oxml.shared import OxmlElement, qn
def set_vert_cell_direction(cell):
tc = cell._tc
tcPr = tc.tcPr
textDirection = OxmlElement('w:textDirection')
textDirection.set(qn('w:val'), 'btLr')
tcPr.append(textDirection) cell._tc is the internal reference to the docx.oxml.table.CT_Tc instance containing the Does that give you enough to go on? |
trying it out...
|
i have been able to achieve using your hints:
however i face some challenges for: we would appreciate your help to points a) and b) Begin forwarded message:
|
Pick one and post the diff and I'll see what I can offer in the way of advice :) |
unable to find a workaround for b) mid-document page orientation change, thanks On 24 May 2014, at 00:38, scanny [email protected] wrote:
|
I need to see the diff out of opc-diag like you sent for the first one. |
oops! here they are: landscape2portrait: @@ -22,8 +22,8 @@
portrait2landscape: @@ -20,9 +20,13 @@
On 24 May 2014, at 08:58, scanny [email protected] wrote:
|
You can ignore the w:rsid.. attributes, they're part of the revision tracking scheme. You can also ignore the proofErr bits, those are the red squiggly lines under spelling errors. The w:pgSz element is the one you want. If there is only one section throughout the document, e.g. it is all portrait or all landscape, then you'll find the w:sectPr parent element as the last child of You can access the "sentinel" sectPr = document._document_part._element.body._sentinel_sectPr There is a little more on sections here: |
thanks, will try… On 25 May 2014, at 01:32, scanny [email protected] wrote:
|
found the solution using your tips and thanks a lot @@ -23,33 +23,22 @@
<w:gridCol w:w="2371"/>
<w:gridCol w:w="2371"/>
</w:tblGrid>
- <w:tr w:rsidR="007C1D62" w:rsidTr="007C1D62">
+ <w:tr w:rsidR="006B42ED" w:rsidTr="00633BDB">
<w:trPr>
<w:trHeight w:val="1015"/>
</w:trPr>
<w:tc>
<w:tcPr>
- <w:tcW w:w="2371" w:type="dxa"/>
+ <w:tcW w:w="7113" w:type="dxa"/>
+ <w:gridSpan w:val="3"/>
</w:tcPr>
- <w:p w:rsidR="007C1D62" w:rsidRDefault="007C1D62"/>
+ <w:p w:rsidR="006B42ED" w:rsidRDefault="006B42ED"/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="2371" w:type="dxa"/>
</w:tcPr>
- <w:p w:rsidR="007C1D62" w:rsidRDefault="007C1D62"/>
- </w:tc>
- <w:tc>
- <w:tcPr>
- <w:tcW w:w="2371" w:type="dxa"/>
- </w:tcPr>
- <w:p w:rsidR="007C1D62" w:rsidRDefault="007C1D62"/>
- </w:tc>
- <w:tc>
- <w:tcPr>
- <w:tcW w:w="2371" w:type="dxa"/>
- </w:tcPr>
- <w:p w:rsidR="007C1D62" w:rsidRDefault="007C1D62"/>
+ <w:p w:rsidR="006B42ED" w:rsidRDefault="006B42ED"/>
</w:tc>
</w:tr>
<w:tr w:rsidR="007C1D62" w:rsidTr="007C1D62">
@@ -111,7 +100,7 @@
</w:tc>
</w:tr>
</w:tbl>
- <w:p w:rsidR="00000000" w:rsidRDefault="007C1D62"/>
+ <w:p w:rsidR="00000000" w:rsidRDefault="006B42ED"/>
<w:sectPr w:rsidR="00000000">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/> On 25 May 2014, at 01:32, scanny [email protected] wrote:
|
I was trying to change text direction on a cell using the code above: I get the error: Am I missing something? |
@trampas the tcPr (table cell properties) child element is optional. When it's not present, tc.tcPr returns None. If you get None, you'll need to add a tcPr element before you can add a Most of the relevant XML schema definitions are here: You can get it in the right place with something like this: ...
tcPr = tc.tcPr
if tcPr is None:
tcPr = OxmlElement('w:tcPr')
tc.insert(0, tcPr)
... Note that the ordering of child elements within tcPr is significant, so just appending a textDirection element might cause a "repair-step" error on document load if there's already one or more child elements within the tcPr element. |
@subregi I think you got yours working, right? If not feel free to reopen, closing for now. |
Thank you! I got it working! I also made ability to merge cells and bold text in a cell. My next task is Thanks On Thu, May 29, 2014 at 12:16 AM, scanny [email protected] wrote:
|
Glad to hear it Trampas :) |
I have just upgraded python-docx and the following code no longer works.
Specifically I get the following error: I use the _tr for accessing XML for the row in several places and was For example I set the row height like: Thanks On Thu, May 29, 2014 at 3:13 PM, scanny [email protected] wrote:
|
We need to change header text-orientation of tables. We are aware that this may not be possible with the current state of the API. We identify the xml-snippet to be inserted using opc-diag as suggested elsewhere. Can we use xml-snippet insertion to achieve this? If yes what is the API-command to do the xml-insertion at a specific point of the docx?
-- sub
The text was updated successfully, but these errors were encountered: