Change the Font on Specific Unicode Characters while Saving to PDF with Python.NET

Example Comparison

The screenshots below demonstrate outputs with different settings. The first PDF shows full‑text font substitution, while the second PDF changes only the specific character’s font.

Full Text Substitution Character‑Level Substitution
Full Font Change Selective Font Change

Implementation Steps

To enable character‑level font substitution:

  1. Create a Workbook object
  2. Access worksheet cells using the Worksheet.cells property
  3. Set cell values containing special Unicode characters
  4. Configure PdfSaveOptions with:
    • is_font_substitution_char_granularity = True
  5. Save the workbook to PDF format
import os
from aspose.cells import Workbook, PdfSaveOptions

# For complete examples and data files, please visit https://github.com/aspose-cells/Aspose.Cells-for-.NET
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")

if not os.path.exists(data_dir):
    os.makedirs(data_dir)

# Create workbook object
workbook = Workbook()

# Access the first worksheet
worksheet = workbook.worksheets[0]

# Access cells
cell1 = worksheet.cells.get("A1")
cell2 = worksheet.cells.get("B1")

# Set the styles of both cells to Times New Roman
style = cell1.get_style()
style.font.name = "Times New Roman"
cell1.set_style(style)
cell2.set_style(style)

# Put the values into the cells
cell1.put_value("Hello without Non-Breaking Hyphen")
cell2.put_value("Hello" + chr(8209) + " with Non-Breaking Hyphen")

# Autofit the columns
worksheet.auto_fit_columns()

# Save to PDF without setting PdfSaveOptions.is_font_substitution_char_granularity
workbook.save(os.path.join(data_dir, "SampleOutput_out.pdf"))

# Save to PDF after setting PdfSaveOptions.is_font_substitution_char_granularity to True
opts = PdfSaveOptions()
opts.is_font_substitution_char_granularity = True
workbook.save(os.path.join(data_dir, "SampleOutput2_out.pdf"), opts)

Key Configuration

Use these essential API components:

  • PdfSaveOptions class for PDF rendering settings
  • is_font_substitution_char_granularity property for character‑level font substitution
  • Workbook.save method for output generation