Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Convert Font Family and Text Size from the Command Line

· 6 min read · Home Tech

In this article
  1. Why Convert Fonts from the Command Line?
  2. Understanding How Fonts Are Stored in Documents
  3. Converting Fonts in HTML and CSS Files
  4. Using Pandoc for Markdown and reStructuredText
  5. Converting Office Documents with LibreOffice
  6. Common Problems and How to Avoid Them
  7. Comparing Tools: Which One Should You Use?
  8. Frequently Asked Questions

You can convert a document's font family and text size directly from the command line using tools like pandoc (for markup formats), sed or perl (for quick replacements in plain text or HTML), and libreoffice --convert-to (for office documents). The exact command depends on your source format and target output, but the core idea is that font settings are stored as text (CSS, XML, or style tags) that you can search, replace, or regenerate programmatically.

Why Convert Fonts from the Command Line?

Graphical word processors and design tools let you change fonts with a few clicks. But when you're working with many files, automated pipelines, or version-controlled documents, doing it by hand is slow and error-prone. The command line offers several advantages:

  • Batch processing: Apply the same font change to hundreds of files in one command.
  • Reproducibility: Scripts can be re-run and shared, ensuring consistent results.
  • Integration: Font conversion can be part of a larger build or publishing workflow (e.g., converting Markdown to PDF with a specific font).
  • Precision: You can target specific elements (headings, body, code blocks) rather than changing everything at once.

Understanding How Fonts Are Stored in Documents

Before you can convert, you need to know where font information lives. Different document formats store fonts in different ways:

  • Plain text (.txt): No font information at all. You can only change the text content, not its appearance.
  • Markdown (.md) and reStructuredText (.rst): These are plain text with lightweight markup. They don't specify fonts directly; instead, you define fonts in the converter's settings or a CSS file when exporting to HTML or PDF.
  • HTML (.html): Fonts are specified via CSS (inline style attributes, <style> blocks, or external stylesheets). You can search for font-family and font-size properties.
  • Office documents (.docx, .odt): These are ZIP archives containing XML files. Font settings live in styles.xml, document.xml, and similar files. You can edit them directly, but it's safer to use a tool like LibreOffice that understands the format.
  • PDF (.pdf): PDFs store fonts as embedded objects. You cannot simply replace text; you need a PDF editor or a tool like cpdf or qpdf (though they don't handle font substitution well). Often it's easier to regenerate the PDF from the source.

Converting Fonts in HTML and CSS Files

If your document is HTML or uses CSS, you can use sed or perl for simple replacements. For example, to change the font family for the entire document from Arial to Georgia:

  1. Use sed -i 's/Arial/Georgia/g' index.html to replace all occurrences of “Arial” with “Georgia”.
  2. To change the base font size from 16px to 18px, run: sed -i 's/font-size: 16px/font-size: 18px/g' index.html.
  3. If you have a separate stylesheet, apply the same commands to style.css.

This approach works for simple, consistent changes. For more complex logic (e.g., change only headings), you're better off using a script with perl or a tool like python with a CSS parser.

Using Pandoc for Markdown and reStructuredText

Pandoc is a universal document converter that can transform Markdown, reStructuredText, LaTeX, and more into HTML, PDF, DOCX, and many other formats. Font conversion isn't a direct feature, but you can control fonts when producing output:

  • When converting to PDF via LaTeX, you can specify the main font with the -V mainfont option: pandoc input.md -o output.pdf -V mainfont=Georgia.
  • For HTML output, use -c style.css to attach a stylesheet that defines the font family and size.
  • For DOCX output, you can modify the reference document with --reference-doc=custom.docx where you've already set the desired fonts in Word.

Pandoc doesn't change the source's font settings (because Markdown doesn't have any), but it lets you define the output's appearance.

Converting Office Documents with LibreOffice

LibreOffice can convert between office formats and also apply styles via command line. To convert a DOCX to PDF with a specific font, you can use a macro or a style template. A simpler approach is to convert the document to HTML first, change the CSS, then convert back to DOCX:

  1. Convert DOCX to HTML: libreoffice --headless --convert-to html document.docx.
  2. Edit the resulting HTML file's CSS (as described above).
  3. Convert back to DOCX: libreoffice --headless --convert-to docx document.html.

This method preserves most formatting but may lose some complex features. For better fidelity, consider using a macro that manipulates the styles directly.

Common Problems and How to Avoid Them

  • Encoding issues: If your document contains non-ASCII characters (e.g., em dashes, accented letters), sed may corrupt them. Use perl -pi -e 's/.../.../g' with proper UTF-8 handling, or use python with pathlib and encoding='utf-8'.
  • Over-replacement: A simple sed replacement might change font names in URLs or file paths. Be specific with your patterns (e.g., include font-family: in the search).
  • CSS specificity: If you change a base font size, some elements might still have their own sizes. You may need to use !important or a more targeted selector.
  • Nested XML: Editing DOCX XML directly can break the file if you're not careful. Always make a backup and test the output.

Comparing Tools: Which One Should You Use?

  • sed/perl: Best for quick, one-off replacements in HTML or CSS. No dependencies beyond standard Unix tools.
  • Pandoc: Ideal for Markdown/LaTeX workflows where you need consistent output across many files. Requires installation but is widely available.
  • LibreOffice: The go-to for Office documents when you need to preserve complex formatting. It's heavier but reliable.
  • Python scripts: For complex logic (e.g., conditional changes based on element type), a Python script with BeautifulSoup or lxml gives you full control.

Frequently Asked Questions

Can I change fonts in a PDF from the command line?

Directly editing a PDF's font is difficult. PDFs embed fonts as objects, and replacing them requires specialized tools like pdffonts to inspect, and cpdf or ghostscript to manipulate, but font substitution is not straightforward. The best approach is to regenerate the PDF from the source document with the desired font settings.

Will these commands work on Windows?

Yes, most of these tools have Windows versions. sed and perl are available via Cygwin or Git Bash. Pandoc and LibreOffice have native Windows installers. The commands are the same, though file paths may need different quoting.

How do I change only the font size of headings, not the body?

In HTML/CSS, target the heading selectors, e.g., h1, h2, h3 { font-size: 24px; }. In Markdown, you can't specify per-element sizes directly; you'd need to use a custom CSS file when exporting. For DOCX, you'd modify the Heading 1, Heading 2, etc. styles in the styles.xml or via a macro.

What if my document uses multiple font families?

You can replace each family individually. For example, sed -i 's/Times New Roman/Georgia/g; s/Arial/Helvetica/g' file.html will change both. If you want to unify everything to one font, you can replace all occurrences of any font-family declaration with a single value, but be careful not to break fallback lists.

Is there a way to preview the changes before applying them?

Yes. Instead of using -i (in-place), run the command without it to output to stdout or a new file. For example, sed 's/Arial/Georgia/g' index.html > new.html. Then open new.html in a browser to check. Similarly, for LibreOffice, you can convert to a temporary PDF first.