Skip to content

Writing to a Textbox. #524

Open
Open

Description

@MattadFelicem

Is there a way currently to create and write into a text box within a word file using python-docx?
I am looking to enter formatted text directly into a text box and potentially even position it at a specific location on the page. Ideally I would like to have the text box have no fill or border as well.

Activity

  1. Benjamin-T commented on Jul 24, 2018

    @Benjamin-T

    I don't think this is currently supported, see #413. However, if you would like to see it implemented, try to find out what would be minimally required to get this working. A good place to start is creating a textbox in Word, save the file and open it using the SDK tool (Open XML SDK 2.5 Productivity Tool) supplied by Microsoft. This is a powerful tool to see what word is actually doing under the hood.

    in the case of the Textbox, I think a place to start would be finding a way for python-docx to accept mc:AlternateContent.

    image

          <mc:AlternateContent>
              <mc:Choice Requires="wps">
                <w:drawing>
                  <wp:anchor distT="45720" distB="45720" distL="114300" distR="114300" simplePos="0" relativeHeight="251659264" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1">
                    <wp:simplePos x="0" y="0" />
                    <wp:positionH relativeFrom="column">
                      <wp:align>center</wp:align>
                    </wp:positionH>
                    <wp:positionV relativeFrom="paragraph">
                      <wp:posOffset>182880</wp:posOffset>
                    </wp:positionV>
                    <wp:extent cx="2360930" cy="1404620" />
                    <wp:effectExtent l="0" t="0" r="22860" b="11430" />
                    <wp:wrapSquare wrapText="bothSides" />
                    <wp:docPr id="217" name="Text Box 2" />
                    <wp:cNvGraphicFramePr>
                      <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" />
                    </wp:cNvGraphicFramePr>
                    <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                      <a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
                        <wps:wsp>
                          <wps:cNvSpPr txBox="1">
                            <a:spLocks noChangeArrowheads="1" />
                          </wps:cNvSpPr>
                          <wps:spPr bwMode="auto">
                            <a:xfrm>
                              <a:off x="0" y="0" />
                              <a:ext cx="2360930" cy="1404620" />
                            </a:xfrm>
                            <a:prstGeom prst="rect">
                              <a:avLst />
                            </a:prstGeom>
                            <a:solidFill>
                              <a:srgbClr val="FFFFFF" />
                            </a:solidFill>
                            <a:ln w="9525">
                              <a:solidFill>
                                <a:srgbClr val="000000" />
                              </a:solidFill>
                              <a:miter lim="800000" />
                              <a:headEnd />
                              <a:tailEnd />
                            </a:ln>
                          </wps:spPr>
                          <wps:txbx>
                            <w:txbxContent>
                              <w:p w:rsidR="006931C9" w:rsidRDefault="006573C3">
                                <w:r>
                                  <w:t>Test</w:t>
                                </w:r>
                                <w:bookmarkStart w:name="_GoBack" w:id="0" />
                                <w:bookmarkEnd w:id="0" />
                              </w:p>
                            </w:txbxContent>
                          </wps:txbx>
                          <wps:bodyPr rot="0" vert="horz" wrap="square" lIns="91440" tIns="45720" rIns="91440" bIns="45720" anchor="t" anchorCtr="0">
                            <a:spAutoFit />
                          </wps:bodyPr>
                        </wps:wsp>
                      </a:graphicData>
                    </a:graphic>
                    <wp14:sizeRelH relativeFrom="margin">
                      <wp14:pctWidth>40000</wp14:pctWidth>
                    </wp14:sizeRelH>
                    <wp14:sizeRelV relativeFrom="margin">
                      <wp14:pctHeight>20000</wp14:pctHeight>
                    </wp14:sizeRelV>
                  </wp:anchor>
                </w:drawing>
              </mc:Choice>
              <mc:Fallback>
                <w:pict>
                  <v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">
                    <v:stroke joinstyle="miter" />
                    <v:path gradientshapeok="t" o:connecttype="rect" />
                  </v:shapetype>
                  <v:shape id="Text Box 2" style="position:absolute;margin-left:0;margin-top:14.4pt;width:185.9pt;height:110.6pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:3.6pt;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:3.6pt;mso-position-horizontal:center;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:marg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nhtbFBL&#xA;AQItABQABgAIAAAAIQA4/SH/1gAAAJQBAAALAAAAAAAAAAAAAAAAAC8BAABfcmVscy8ucmVsc1BL&#xA;AQItABQABgAIAAAAIQAmy+1iJQIAAEcEAAAOAAAAAAAAAAAAAAAAAC4CAABkcnMvZTJvRG9jLnht&#xA;bFBLAQItABQABgAIAAAAIQBIWydy2wAAAAcBAAAPAAAAAAAAAAAAAAAAAH8EAABkcnMvZG93bnJl&#xA;di54bWxQSwUGAAAAAAQABADzAAAAhwUAAAAA&#xA;">
                    <v:textbox style="mso-fit-shape-to-text:t">
                      <w:txbxContent>
                        <w:p w:rsidR="006931C9" w:rsidRDefault="006573C3">
                          <w:r>
                            <w:t>Test</w:t>
                          </w:r>
                          <w:bookmarkStart w:name="_GoBack" w:id="1" />
                          <w:bookmarkEnd w:id="1" />
                        </w:p>
                      </w:txbxContent>
                    </v:textbox>
                    <w10:wrap type="square" />
                  </v:shape>
                </w:pict>
              </mc:Fallback>
            </mc:AlternateContent>
    
  2. MattadFelicem commented on Jul 24, 2018

    @MattadFelicem
    Author

    Thank you for the quick response! However, I am a bit beyond my level of understanding with continuing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions