How to Format Programming Code Blocks in Word 365 with WPS Writer
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.
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.
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.
Open your document in WPS Office Writer, click the Home tab, and select the Styles dialog launcher to open the Styles pane.
Click New Style, name it CodeBlock, choose a monospaced font such as Consolas or Courier New, set a slightly smaller size, and confirm.
Select a code block, click the Shading control in the Home tab’s Paragraph group, and choose light gray or another theme color.
With the code block selected, go to Page Layout > Line Numbers and choose Continuous, or suppress line numbers for non-code styles.
Paste from Visual Studio Code with Keep Source Formatting
Best when the code is already syntax-highlighted in Visual Studio Code and you want to keep that coloring in the document.
Insert an embedded OpenDocument Text object for syntax-colored code
Use this solution when you want an isolated code editor-like block that preserves syntax colors independently of the main e-book style.
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. Open the Word document: Launch WPS Office Writer and open the .docx e-book that contains your code blocks.
- 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. 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. Paste or update source code: Paste code from Visual Studio Code with Keep Source Formatting or apply the CodeBlock style after pasting.
- 5. Export to PDF: Go to File > Export to PDF, check the preview, and save the final e-book.

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.




