Python Forum
How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file?
#11
(Jan-19-2025, 09:02 AM)Keville_35 Wrote: This script creates a simple .docx file with some bold and italic text:
It doesn't look like Python code. Which language is that? Javascript?
« We can solve any problem by introducing an extra level of indirection »
Reply
#12
Here's a richtext example for PySide6

https://doc.qt.io/qtforpython-6/examples...tedit.html

[Image: textedit.png]
Pedroski55 likes this post
Reply
#13
(Jan-19-2025, 09:02 AM)Keville_35 Wrote: This script creates a simple .docx file with some bold and italic text:

import { Document, Paragraph, TextRun } from "docx"; import { writeFileSync } from "fs"; // Create document const doc = new Document({ sections: [{ properties: {}, children: [ new Paragraph({ children: [ new TextRun({ text: "Hello World", bold: true, }), new TextRun({ text: " This is a new paragraph.", italics: true, }), ], }), ], }], }); // Used to export the file into a .docx file const buffer = await Packer.toBuffer(doc); // Write the file writeFileSync("My Document.docx", buffer); console.log("Document created successfully");

Is this the simplest way? Maybe be careful and try step by step
Larz60+ write Feb-13-2025, 06:27 AM:
link removed
Reply
#14
from docx import Document from docx.shared import Pt from docx.enum.text import WD_PARAGRAPH_ALIGNMENT # 创建一个文档对象 doc = Document() # 添加标题 doc.add_heading('Formatted Text Example', level=1) # 添加一个段落,并设置加粗、斜体和字体大小 paragraph = doc.add_paragraph() run = paragraph.add_run("This text is bold, italic, and larger.") run.bold = True run.italic = True run.font.size = Pt(16) # 添加另一个段落并改变字体 paragraph2 = doc.add_paragraph() run2 = paragraph2.add_run("This text uses a custom font and size.") run2.font.name = 'Arial' run2.font.size = Pt(14) # 居中对齐 paragraph2.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 保存文档 doc.save("formatted_text.docx") print("Word document created successfully!")
Quote:pip install python-docx
you must install python-docx.
buran write May-20-2025, 06:14 PM:
spam link removed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to write/overwrite data in a txt. file according to inp Quinn 2 1,543 Aug-12-2025, 04:20 PM
Last Post: Quinn
  How to write variable in a python file then import it in another python file? tatahuft 4 2,199 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,132 Oct-17-2024, 01:15 AM
Last Post: Winfried
  Print text with big font and style tomtom 6 23,932 Aug-13-2024, 07:26 AM
Last Post: yazistilleriio
  What does .flush do? How can I change this to write to the file? Pedroski55 3 2,195 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 2,741 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Python code for alignment and font size 1418 0 1,370 Jan-14-2024, 03:56 AM
Last Post: 1418
  write to csv file problem jacksfrustration 11 8,295 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 5,687 Nov-09-2023, 10:56 AM
Last Post: mg24
  logging: change log file permission with RotatingFileHandler erg 0 3,530 Aug-09-2023, 01:24 PM
Last Post: erg

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.