Formatting Issues

How to Format Programming Code Blocks in Word 365 with WPS Writer

Rana GarciaRana Garcia Nov 13, 2026 869 views

Question details

The user needs to format Python, JavaScript, PHP, Java, and C++ code blocks in a Word 365 e-book so they stay readable and consistent before PDF conversion.

Product
WPS Office Writer for Microsoft Word 365 documents
Device & OS
not provided
Scenario
The user is preparing an e-book that contains code snippets in several languages and plans to export or convert the Word document to PDF.
Observed behavior
Code blocks need a durable formatting solution that survives PDF conversion without losing readability or syntax colors.
Before you start

Open the Word 365 e-book in WPS Office Writer and place the cursor where the first code block should appear. If the code is in Visual Studio Code, keep the source file or selection ready so you can test the paste option.

Solution 1Recommended

Create a dedicated code paragraph style with shading and line numbering

Use this solution when you need consistent formatting for Python, JavaScript, PHP, Java, and C++ code examples throughout the e-book.

A paragraph style keeps all code blocks uniform and is easier to maintain than manually formatting each code example. Shading and line numbers also remain intact during PDF export.

1
Open the Styles pane

Open your document in WPS Office Writer, click the Home tab, and select the Styles dialog launcher to open the Styles pane.

2
Create a new code style

Click New Style, name it CodeBlock, choose a monospaced font such as Consolas or Courier New, set a slightly smaller size, and confirm.

3
Apply shading

Select a code block, click the Shading control in the Home tab’s Paragraph group, and choose light gray or another theme color.

4
Enable line numbers

With the code block selected, go to Page Layout > Line Numbers and choose Continuous, or suppress line numbers for non-code styles.

Use style-based formatting: Style-based formatting keeps updates uniform and survives PDF conversion better than manual highlighting.
Direct solution with WPS Writer

Format Word code blocks cleanly in WPS Office Writer

Open your Word 365 e-book in WPS Office Writer, create a reusable code paragraph style with shading and line numbers, then export to PDF without losing code readability.

  1. 1. Open the Word document: Launch WPS Office Writer and open the .docx e-book that contains your code blocks.
  2. 2. Create a code style: On the Home tab, open the Styles pane, create a style named CodeBlock, and set a monospaced font such as Consolas.
  3. 3. Apply shading and line numbers: Select each code block, apply paragraph shading from the Home tab, and enable line numbers from the Page Layout tab.
  4. 4. Paste or update source code: Paste code from Visual Studio Code with Keep Source Formatting or apply the CodeBlock style after pasting.
  5. 5. Export to PDF: Go to File > Export to PDF, check the preview, and save the final e-book.
Reusable code styles keep Python, JavaScript, PHP, Java, and C++ blocks consistent.Full compatibility with Microsoft Word .docx files and PDF export.Free, lightweight Office suite with a familiar ribbon interface.Preserve syntax colors when pasting from Visual Studio Code.No Microsoft 365 subscription required for document formatting.
QA img-9

Frequently Asked Questions

Can I format Python and JavaScript code blocks the same way in WPS Writer?

Yes. Create one CodeBlock paragraph style and apply it to all language blocks. The style controls font, shading, and line numbers, while syntax highlighting depends on how the code is pasted or embedded.

Why does code lose syntax colors when pasted into Word 365?

Word and WPS often paste code as plain text or convert source formatting. Use the Keep Source Formatting paste option when available, or insert an embedded OpenDocument Text object to preserve syntax colors.

How do I prevent line numbers from appearing on normal paragraphs?

Apply line numbering only to the CodeBlock style, or use Page Layout > Line Numbers and suppress line numbering for the current paragraph on non-code styles.

Does this formatting survive PDF conversion?

Yes. Paragraph shading, monospaced fonts, and line numbers are layout properties that export cleanly to PDF when you use WPS Office Writer or Word 365.

Can I use this method for C++ and Java code blocks?

Yes. The same CodeBlock style works for C++, Java, PHP, Python, JavaScript, and other programming language examples in a Word 365 e-book.