Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Apr 02, 2025 6:44 am

The code below (specifally the AppendField) generates a font error for some reason.
As there is no documentation on AppendField for python - I'm not sure what I am doing wrong.

Code: Select all
    bSec = doc.AddSection()

    # Insert a 3-column header in the new section
    # Column 0 -> Subject
    # Column 1 -> Security
    # Column 2 -> Version
    header = bSec.HeadersFooters.Header
    # Clear any existing paragraphs in the header:
    bSec.HeadersFooters.Header.Paragraphs.Clear()

    # Create a new table directly on the header, specifying True to enable the default borders or styles
    hfTable = bSec.HeadersFooters.Header.AddTable(True)

    # Define the table shape
    hfTable.ResetCells(1, 3)

    # Cell 0: Subject
    subjectVal = doc.BuiltinDocumentProperties.Subject or "No Subject"
    c0Para = hfTable.Rows[0].Cells[0].AddParagraph()
    c0Para.AppendText(subjectVal)

    # Cell 1: Security level
    securityVal = data.get("security", {}).get("level", "Confidential")
    c1Para = hfTable.Rows[0].Cells[1].AddParagraph()
    c1Para.AppendText(securityVal)

    # Cell 2: A doc variable field named "A1"
    c2Para = hfTable.Rows[0].Cells[2].AddParagraph()
    c2Para.AppendField("Version", FieldType.FieldDocVariable)


Code: Select all
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/workspaces/docgen/docgen/__main__.py", line 4, in <module>
    main()
  File "/workspaces/docgen/docgen/main.py", line 92, in main
    doc.UpdateTableOfContents()
  File "/home/vscode/.local/lib/python3.12/site-packages/plum/function.py", line 642, in __call__
    return self.f(self.instance, *args, **kw_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.12/site-packages/plum/function.py", line 592, in __call__
    return _convert(method(*args, **kw_args), return_type)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.12/site-packages/spire/doc/Document.py", line 1285, in UpdateTableOfContents
    CallCFunction(GetDllLibDoc().Document_UpdateTableOfContents,self.Ptr)
  File "/home/vscode/.local/lib/python3.12/site-packages/spire/doc/common/__init__.py", line 129, in CallCFunction
    raise SpireException(info)
spire.doc.common.SpireException: Cannot found font installed on the system.:   at sprdv9..ctor() + 0x69d
   at Spire.Doc.Layout.Fields.FieldOptions..ctor() + 0x64
   at sprddy.sprc(sprc6g) + 0x3d
   at sprddy.sprd(sprc6g) + 0x1c
   at sprddy.sprl(sprddr) + 0x6a
   at sprddy.spro(sprddr) + 0xbb
   at sprddy.sprr(sprddr) + 0x5a
   at sprddy.sprb(sprdds) + 0xa7
   at sprddy.spra(sprdd1, sprdd1) + 0x5a
   at sprddy.spra(DocumentObject, sprddy) + 0x57
   at Spire.Doc.Document.sprc() + 0x4c
   at Spire.Doc.Document.spra(Boolean, Boolean, ToPdfParameterList, sprevb) + 0x1d8
   at Spire.Doc.Document.sprc(ToPdfParameterList) + 0x78
   at Spire.Doc.Document.spra(List`1, Boolean) + 0x49
   at Spire.Doc.Document.UpdateTableOfContents() + 0xa5
   at Spire.Doc.AOT.NLDocument.Document_UpdateTableOfContents(IntPtr, IntPtr) + 0x51


Version is set elsewhere:

Code: Select all
    if version_in_json:
        doc.Variables.Add("Version", version_in_json)
    else:
        logging.warning("Version not found in JSON metadata.")
        doc.Variables.Add("Version", "---")


I've also tried:

Code: Select all
    # Cell 2: A doc variable field named "DocumentVersion"
    version_style = ParagraphStyle(doc)
    version_style.Name = "VersionStyle"
    version_style.CharacterFormat.FontName = "Helvetica Neue"
    doc.Styles.Add(version_style)
    c2Para: Paragraph = hfTable.Rows[0].Cells[2].AddParagraph()
    field = c2Para.AppendField("DocumentVersion", FieldType.FieldDocProperty)
    field.ApplyCharacterFormat(version_style.CharacterFormat)


which causes the same font error:

Code: Select all
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/workspaces/docgen/docgen/__main__.py", line 4, in <module>
    main()
  File "/workspaces/docgen/docgen/main.py", line 98, in main
    doc.UpdateTableOfContents()
  File "/home/vscode/.local/lib/python3.12/site-packages/plum/function.py", line 642, in __call__
    return self.f(self.instance, *args, **kw_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.12/site-packages/plum/function.py", line 592, in __call__
    return _convert(method(*args, **kw_args), return_type)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.12/site-packages/spire/doc/Document.py", line 1285, in UpdateTableOfContents
    CallCFunction(GetDllLibDoc().Document_UpdateTableOfContents,self.Ptr)
  File "/home/vscode/.local/lib/python3.12/site-packages/spire/doc/common/__init__.py", line 129, in CallCFunction
    raise SpireException(info)
spire.doc.common.SpireException: Cannot found font installed on the system.:   at sprdv9..ctor() + 0x69d
   at Spire.Doc.Layout.Fields.FieldOptions..ctor() + 0x64
   at sprddy.sprc(sprc6g) + 0x3d
   at sprddy.sprd(sprc6g) + 0x1c
   at sprddy.sprl(sprddr) + 0x6a
   at sprddy.spro(sprddr) + 0xbb
   at sprddy.sprr(sprddr) + 0x5a
   at sprddy.sprb(sprdds) + 0xa7
   at sprddy.spra(sprdd1, sprdd1) + 0x5a
   at sprddy.spra(DocumentObject, sprddy) + 0x57
   at Spire.Doc.Document.sprc() + 0x4c
   at Spire.Doc.Document.spra(Boolean, Boolean, ToPdfParameterList, sprevb) + 0x1d8
   at Spire.Doc.Document.sprc(ToPdfParameterList) + 0x78
   at Spire.Doc.Document.spra(List`1, Boolean) + 0x49
   at Spire.Doc.Document.UpdateTableOfContents() + 0xa5
   at Spire.Doc.AOT.NLDocument.Document_UpdateTableOfContents(IntPtr, IntPtr) + 0x51


I've also tried

Code: Select all
    # Cell 2: A doc variable field named "DocumentVersion"
    version_field_name = "DocumentVersion"
    version_style = ParagraphStyle(doc)
    version_style.Name = "VersionStyle"
    version_style.CharacterFormat.FontName = "Helvetica Neue"
    version_style.CharacterFormat.FontSize = 10
    version_style.CharacterFormat.TextColor = Color.get_Crimson()
    doc.Styles.Add(version_style)
    version_field = hfTable.Rows[0].Cells[2].AddParagraph().AppendField(
        version_field_name, FieldType.FieldDocProperty)

    c2Para = version_field.OwnerParagraph
    c2Para.ApplyStyle(version_style.Name)


Same error.

I've also tried

Code: Select all
    c2Para: Paragraph = hfTable.Rows[0].Cells[2].AddParagraph()
    field = c2Para.AppendField(version_field_name, FieldType.FieldDocVariable)
    field.Code = "DOCVARIABLE \"" + version_field_name+"\""


Same error.

01JQTKJTFXPCJ823TVMP
 
Posts: 3
Joined: Wed Apr 02, 2025 6:39 am

Wed Apr 02, 2025 8:21 am

Hello,

Thanks for your inquiry.
Please note that when creating fields, the program loads the Times New Roman font from the system font directory. Therefore, if this font is not installed on your system, the program will return an error. Please install the Times New Roman font and retest. If you have any other questions, please feel free to write to us.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Wed Apr 02, 2025 9:55 pm

For those ending up here using VSCode devcontainers with a Debian image.
You need to:

Code: Select all
# 1) Create a new repo config that has the main, contrib, and non-free components.
#    Adjust "bookworm" to match whatever codename you're actually on.
sudo sh -c 'echo "deb http://deb.debian.org/debian bookworm main contrib non-free" > /etc/apt/sources.list.d/bookworm.list'

# 2) Update the package lists.
sudo apt-get update

# 3) Accept the EULA for the MS fonts (so it won't prompt interactively).
echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | sudo debconf-set-selections

# 4) Finally, install the fonts and rebuild the cache.
sudo apt-get install -y ttf-mscorefonts-installer fontconfig
sudo fc-cache -fv

01JQTKJTFXPCJ823TVMP
 
Posts: 3
Joined: Wed Apr 02, 2025 6:39 am

Thu Apr 03, 2025 6:28 am

Hello,

Thanks for your reply and sharing of solution. If you have any further questions, please feel free to write back.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc

cron