Ziv-Barber / officegen

Standalone Office Open XML files (Microsoft Office 2007 and later) generator for Word (docx), PowerPoint (pptx) and Excell (xlsx) in javascript. The output is a stream.
MIT License
2.66k stars 471 forks source link

Paragraph styles #257

Open psinghsp opened 6 years ago

psinghsp commented 6 years ago

Is it possible to tag a paragraph with built in word styles like Header1, Header2, Normal? I do not know if these are defined inside the document or they are part of Microsoft Word.

The reason why I ask this is that after I generate the document, I want to manually generate table of contents. Since all the paragraphs after generation show up as "normal", it is not possible to generate a TOC.

Any idea how to achieve this?

MidaAiZ commented 6 years ago

I meet the same problem too. I have tried like this, but it doesn't work.

  p = docx.createP();
  p.addText('test heading1 format');
  p.options.pStyleDef = 'Heading1'

It looks like this when converted into xml code in document.xml:

  <w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13">
      <w:pPr><w:pStyle w:val="Heading1"/></w:pPr>
      <w:r>
        <w:t>test H1 format</w:t>
      </w:r>
   </w:p>

The xml code below is original code generated by Microsoft Word in document.xml. it uses 'Heading1' property to create format H1, and it works well. I can not figure out why. As I see, they have the same tag or property;

<w:p w14:paraId="035E4301" w14:textId="77777777" w:rsidR="00161C7E" w:rsidRDefault="00AC5C69" w:rsidP="00AC5C69">
      <w:pPr><w:pStyle w:val="Heading1"/>
        <w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:eastAsia="zh-CN"/></w:rPr>
      </w:pPr>
      <w:r>
        <w:rPr><w:lang w:eastAsia="zh-CN"/></w:rPr>
        <w:t>T</w:t>
      </w:r>
      <w:r>
        <w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:eastAsia="zh-CN"/></w:rPr>
        <w:t>est H1 format</w:t>
      </w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/>
</w:p>

image

colinjeanne commented 5 years ago

Yes, this is possible but you need to add your own stylesheet. See https://github.com/Ziv-Barber/officegen/pull/259.

brockfanning commented 4 years ago

I'm just wondering if there is any documentation or a working example of this. I have been unable to create any paragraphs with a style. I don't need anything custom - just the usual heading 1 would be a great start.

Yattabyte commented 3 years ago

Another year later, still no hits on how to do this

Ziv-Barber commented 3 years ago

Sorry for the late response :( I'm now back to maintain this library.

For your question:

There's an option called force_style that allowing you to declare the paragraph style:

let newP = docx.createP();
newP.options.force_style = 'Heading1';
Yattabyte commented 3 years ago

Sorry for the late response :( I'm now back to maintain this library.

For your question:

There's an option called force_style that allowing you to declare the paragraph style:

let newP = docx.createP();
newP.options.force_style = 'Heading1';

With the latest version available via the node package manager, the above example character-for-character does not yield text in the Heading1 style on my system, viewing with latest version of Word available on macOS and LibreOffice

Ziv-Barber commented 3 years ago

Let me check it.

beingvishalt commented 7 months ago

.options.force_style = 'Heading1';

No this doesn't work