title: How to copy all the paragraphs and styles within a heading to another MS Word file
body: <p>I am using <code>Python docx</code> library and try to copy paragraphs under a few headings in many .docx files to a single new word document.</p>
  
  <p>In those original .docx files, there are some custom styles created when I copy/paste the text from website, and I also manually adjusted some styles (e.g., I adjusted the paragraph spacing).</p>
  
  <p>Below is my code:</p>
  
  <pre><code>from docx import Document
  from docx.enum.style import WD_STYLE_TYPE
  from docx.shared import Length, Pt
  import copy
  document = Document("test.docx")
  
  target = Document("target.docx")
  styles = target.styles
  
  # for s in document.styles:
  #     if s.name not in target.styles:
  #         styles = target.styles
  #         style = target.styles.add_style(s.name, WD_STYLE_TYPE.PARAGRAPH)
  #         style.base_style = styles['Normal']
  
  
  def get_para_data(output_doc_name, paragraph):
      """
      Write the run to the new file and then set its font, bold, alignment, color etc. data.
      """
      output_para = output_doc_name.add_paragraph()
  
      output_para.style = paragraph.style
  
      output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
      output_para.paragraph_format.space_before = Pt(paragraph.paragraph_format.space_before.pt) if  paragraph.paragraph_format.space_before else None
      output_para.paragraph_format.space_after = Pt(paragraph.paragraph_format.space_after.pt) if  paragraph.paragraph_format.space_after else None
      output_para.paragraph_format.line_spacing_rule = paragraph.paragraph_format.line_spacing_rule
      output_para.paragraph_format.line_spacing = paragraph.paragraph_format.line_spacing
      output_para.paragraph_format.first_line_indent = paragraph.paragraph_format.first_line_indent
  
      for run in paragraph.runs:
          output_run = output_para.add_run(run.text)
          output_run.font.highlight_color = run.font.highlight_color
          output_run.font.shadow = run.font.shadow
          # output_run.shadow = run.shadow
          # Run's bold data
          output_run.bold = run.bold
          # Run's italic data
          output_run.italic = run.italic
          # Run's underline data
          output_run.underline = run.underline
          output_run.font.name = run.font.name
          output_run.font.size = run.font.size
          # Run's color data
          output_run.font.color.rgb = run.font.color.rgb
          # Run's font data
          output_run.style.name = run.style.name
      # Paragraph's alignment data
  
  # for s in document.styles:
  #     if s.name not in target.styles:
  #         styles = target.styles
  #         style = target.styles.add_style(s.name, WD_STYLE_TYPE.PARAGRAPH)
  #         style.base_style = styles['Normal']
  
  for para in document.paragraphs:
      get_para_data(target, para)
  
  target.save("target.docx")
  </code></pre>
  
  <p>I tried to manually import the styles into target.docx but the style is still not the same. </p>
  
  <p>My question is: what is the correct way to copy and paste the paragraphs with some modified styles into a new document? I do not mind manually import the style because in the end I just need to create a single document.  </p>
  
  <p>If Python cannot do it, is there any way using C#?</p>
  
  <p>The line spacing and space_before/after are also not right.</p>