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.65k stars 471 forks source link

How to create heading formats like h1? #282

Closed MidaAiZ closed 5 years ago

MidaAiZ commented 5 years ago

How can I create heading formats like h1 h2 h3... ? 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

MidaAiZ commented 5 years ago

Hello @Ziv-Barber , could you please help me? I found it may be a bug caused by missing stylesheets.

ellieejlee commented 5 years ago

I'd find this feature very useful too.

colinjeanne commented 5 years ago

You need to add your own stylesheet XML. This was implemented in https://github.com/Ziv-Barber/officegen/pull/259.