Sunday, 15 March 2015

python - How to pass/output a whitespace \x20 character to docx -


I am using python-docx to create and write a doc file. My application will write a word at a time I used

  p.add_run (res.decode ('unicode_escape'));  

To write the word Unicode, one at a time. However, whenever there was a white space character between / between add_run () , then it would fail very badly and I would have a word consented.

I tried the following:

  1. repr (res) works but it will produce unnecessary (quotation) characters
  2. res = r '\ x20' no effect
  3. res = '' '' '' no effect
  4. res = r '' no effect

I have the white space to the res variable Use the above expressions to specify and then try to add the current paragraph to p.add_run. (Res) turned

Finally I way I was writing stories. Rather than writing a word / part, I collected a whole line and then wrote it in the document.

Although I have solved the problem , I still want to get insight the cause of this behavior

is a sequence of characters that all have the same character formatting. In general, you will write the same formatting in the same run to avoid lifting all "container" overhead (about 20 bytes each), where you do not need it. So you have a recommended approach that has ended with the solution .

But if you want "my little lamb was kidnapped", then addressing the missing whotspace question, and all of it were in the same font (regular Be bold or italic to be a different font), you will use it:

  p.add_run ("My little lamb was there.")   
  p.add_run ("my") p.add_run ("was") p.add_run ("a") p .add_run (" Little ") p.add_run (" lamb ")  

Whatever you were asked, which is" MaryHeadlightmbamb "because you have failed to provide leading and / or previous White place you wanted

  p.add_run ("Mary")   

If you Instead of:

 ) p.add_run ("was") p.add_run ("a") p.add_run ("little") p.add_run ("lamb.") < If you post an example of the actual input and the specific unexpected production probably I can offer more through explanation.  

About 20 bytes of the overhead, adding different runs containing 'Foo' and 'Bar' text creates XML in this way:

   & Lt; W: r & gt; & Lt; W: t & gt; Bar & lt; / W: t & gt; & Lt; / W: r>  

While the same run with "text" text reduces this XML to 22 characters, if you do not use new lines and indentation whitespace for beautiful printing (which Will be fired on savings). ):

  & lt; W: r & gt; & Lt; W: t & gt; Foobar & lt; / W: t & gt; & Lt; / W: r>  

Some of these will be compressed because the .docx file is a zip archive, I think it is only my engineering habit that is to reduce the waste which is more to me Look for affordable solutions :)


No comments:

Post a Comment