Set paragraph font in python-docx

Set paragraph font in python-docx

To set the paragraph font in a Word document using the python-docx library, you can follow these steps:

  1. Install the python-docx library if you haven't already:

    pip install python-docx 
  2. Use the following code to create a new Word document, add a paragraph, and set the font for that paragraph:

from docx import Document from docx.shared import Pt from docx.oxml.ns import qn from docx.oxml import OxmlElement # Create a new Document doc = Document() # Add a paragraph paragraph = doc.add_paragraph("This is a sample paragraph with a specific font.") # Set the font size and name for the paragraph run = paragraph.runs[0] run.font.size = Pt(12) run.font.name = 'Arial' # Set the font for East Asian text r = run._element rPr = r.get_or_add_rPr() east_asian_font = OxmlElement('w:eastAsia') east_asian_font.set(qn('w:val'), 'Arial') rPr.append(east_asian_font) # Save the document doc.save('sample.docx') 

Explanation:

  • Import the required modules: Document to create the Word document, Pt to set the font size, and OxmlElement and qn for advanced XML manipulation.
  • Create a new document: doc = Document()
  • Add a paragraph: paragraph = doc.add_paragraph("This is a sample paragraph with a specific font.")
  • Access the run of text within the paragraph: run = paragraph.runs[0]
  • Set the font size: run.font.size = Pt(12)
  • Set the font name: run.font.name = 'Arial'
  • Handle East Asian text fonts if needed:
    • Access the run's XML element: r = run._element
    • Get or add the run properties: rPr = r.get_or_add_rPr()
    • Create and set the East Asian font:
      east_asian_font = OxmlElement('w:eastAsia') east_asian_font.set(qn('w:val'), 'Arial') rPr.append(east_asian_font) 
  • Save the document: doc.save('sample.docx')

Examples

  1. How to change the font of a paragraph in python-docx?

    Changing the font of a paragraph allows you to customize the appearance of text in your Word document.

    # Example code to change the font of a paragraph in python-docx from docx import Document from docx.shared import Pt from docx.enum.text import WD_PARAGRAPH_ALIGNMENT document = Document() paragraph = document.add_paragraph('This is a paragraph with custom font.') font = paragraph.style.font font.name = 'Arial' font.size = Pt(12) 
  2. Set paragraph font style (e.g., bold, italic) in python-docx?

    Applying font styles like bold or italic to a paragraph enhances text formatting.

    # Example code to set paragraph font style in python-docx from docx import Document from docx.shared import Pt from docx.enum.text import WD_PARAGRAPH_ALIGNMENT document = Document() paragraph = document.add_paragraph('This is a paragraph with custom font style.') font = paragraph.style.font font.name = 'Arial' font.size = Pt(12) font.bold = True 
  3. How to change the font size of a paragraph in python-docx?

    Adjusting the font size of a paragraph allows you to control the text's visual presentation.

    # Example code to change the font size of a paragraph in python-docx from docx import Document from docx.shared import Pt from docx.enum.text import WD_PARAGRAPH_ALIGNMENT document = Document() paragraph = document.add_paragraph('This is a paragraph with custom font size.') font = paragraph.style.font font.name = 'Arial' font.size = Pt(16) 
  4. How to set paragraph alignment in python-docx?

    Setting paragraph alignment adjusts the position of text within the document.

    # Example code to set paragraph alignment in python-docx from docx import Document from docx.enum.text import WD_PARAGRAPH_ALIGNMENT document = Document() paragraph = document.add_paragraph('This is a paragraph with custom alignment.') paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER 
  5. Change font color of a paragraph in python-docx?

    Modifying the font color of a paragraph adds visual emphasis to specific text.

    # Example code to change font color of a paragraph in python-docx from docx import Document from docx.shared import RGBColor document = Document() paragraph = document.add_paragraph('This is a paragraph with custom font color.') run = paragraph.add_run() font = run.font font.color.rgb = RGBColor(255, 0, 0) # Red color 
  6. How to apply strikethrough to a paragraph in python-docx?

    Applying strikethrough to a paragraph crosses out text, typically to indicate deletion or correction.

    # Example code to apply strikethrough to a paragraph in python-docx from docx import Document document = Document() paragraph = document.add_paragraph('This is a paragraph with strikethrough.') run = paragraph.add_run() run.font.strike = True 
  7. Set paragraph font family (e.g., Times New Roman, Helvetica) in python-docx?

    Setting the font family of a paragraph allows you to choose from a variety of typefaces.

    # Example code to set paragraph font family in python-docx from docx import Document from docx.shared import Pt document = Document() paragraph = document.add_paragraph('This is a paragraph with custom font family.') font = paragraph.style.font font.name = 'Times New Roman' font.size = Pt(12) 
  8. How to underline text in a paragraph using python-docx?

    Underlining text in a paragraph emphasizes it, typically to signify importance or emphasis.

    # Example code to underline text in a paragraph using python-docx from docx import Document document = Document() paragraph = document.add_paragraph('This is a paragraph with underlined text.') run = paragraph.add_run() run.font.underline = True 
  9. Apply superscript or subscript to text in a paragraph with python-docx?

    Adding superscript or subscript to text in a paragraph is useful for mathematical notations or footnotes.

    # Example code to apply superscript or subscript to text in a paragraph with python-docx from docx import Document document = Document() paragraph = document.add_paragraph('This is a paragraph with superscript and subscript text.') run = paragraph.add_run('superscript') run.font.superscript = True 
  10. How to set paragraph indentation in python-docx?

    Adjusting paragraph indentation controls the spacing and alignment of text within a document.

    # Example code to set paragraph indentation in python-docx from docx import Document from docx.shared import Inches document = Document() paragraph = document.add_paragraph('This is a paragraph with indentation.') paragraph.paragraph_format.left_indent = Inches(0.5) # Adjust indentation as needed 

More Tags

infinity ruby-on-rails-5 internet-connection yum ratingbar intentservice google-visualization c# sas distutils

More Programming Questions

More Trees & Forestry Calculators

More Biology Calculators

More Electronics Circuits Calculators

More Entertainment Anecdotes Calculators