Posts: 7
Threads: 2
Joined: Jun 2018
Hello all. I'm wondering if someone can direct me to the proper documentation for how to write text to a file that is formatted in different ways, such as making the font bold or italicized, or changing the font sizes, etc. Ideally it would be a .doc file.
Thank you!
John
Posts: 8,147
Threads: 159
Joined: Sep 2016
Posts: 7
Threads: 2
Joined: Jun 2018
(Jun-19-2018, 04:33 AM)buran Wrote: https://python-docx.readthedocs.io/en/latest/
Thanks very much! Just two quick questions: is there no built-in way to do these things? And, do you know if this works with .doc (not .docx) files?
The documentation says:
Quote:You can open any Word 2007 or later file this way (.doc files from Word 2003 and earlier won’t work).
But I'm not sure if this means .doc files will work at all. Not sure when they got replaced by .docx. Maybe it was 2003.
Thanks!
Posts: 8,147
Threads: 159
Joined: Sep 2016
Jun-19-2018, 05:57 AM
(This post was last modified: Jun-19-2018, 05:58 AM by buran.)
(Jun-19-2018, 04:41 AM)JohnJSal Wrote: is there no built-in way to do these things? This is the python way. I guess you refer to Standard Python Library. It is the core, there are thousands of third-party packages available through PyPi. And yet others that are not on PyPI.
(Jun-19-2018, 04:41 AM)JohnJSal Wrote: do you know if this works with .doc (not .docx) files No, it's just for docx files. doc and docx are different formats. For doc files you will need to use pywin32 - Python extensions for Windows. It provides access to much of the Win32 API, the ability to create and use COM objects, and the Pythonwin environment.
Posts: 7
Threads: 2
Joined: Jun 2018
(Jun-19-2018, 05:57 AM)buran Wrote: (Jun-19-2018, 04:41 AM)JohnJSal Wrote: is there no built-in way to do these things? This is the python way. I guess you refer to Standard Python Library. It is the core, there are thousands of third-party packages available through PyPi. And yet others that are not on PyPI.
Oh, so this is already installed? It looked like I had to install it myself through pip. Either way, I will give it a try.
Thanks!
Posts: 8,147
Threads: 159
Joined: Sep 2016
(Jun-19-2018, 02:41 PM)JohnJSal Wrote: Oh, so this is already installed? Exactly the opposite. Python Standard Library provides core functionality and python-docx is one of the third-party packages on PyPI that extend the core functionality. You need to install it via pip.
Now there are some python distributions that have some external modules pre-installed, but I refer to pure python distribution, available at python.org
Posts: 7
Threads: 2
Joined: Jun 2018
Posts: 2
Threads: 0
Joined: Jan 2025
Jan-18-2025, 01:32 PM
(This post was last modified: Jan-20-2025, 01:12 PM by karimali.)
To write formatted text (such as bold, italic, or changing font sizes) to a file in Python, you can use libraries like python-docx for creating and editing .docx files. Here's a simple example of how you can create a document with formatted text:
from docx import Document
from docx.shared import Pt
# Create a new Document
doc = Document()
# Add a paragraph with formatted text
para = doc.add_paragraph()
# Add text with bold and italic formatting
para.add_run('This text is bold and italicized.').bold = True
para.add_run(' This part is italicized.').italic = True
# Add a larger font size
run = para.add_run(' This text has a larger font size.')
run.font.size = Pt(14)
# Save the document
doc.save('formatted_text.docx') This script will create a .docx file where some text is bold, some italicized, and other parts have a larger font size.
For further customization, you can refer to the python-docx documentation, where you'll find more options for formatting your text.
Additionally, if you’re interested in making your file or profile more stylis, you can use stylishname to generate unique and creative names or fonts for your online profiles, including game usernames.
Posts: 1,087
Threads: 143
Joined: Jul 2017
If you are happy with PDFs, you can use reportlab and have absolute and minute control over every aspect of formatting.
Read the reportlab_userguide.pdf, especially from page 72.
Here a small example of the almost endless possibilities:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.colors import pink, green, brown, white, black
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.styles import getSampleStyleSheet
path2sizes_pdf = 'reportlab/pdfs/text_sizes.pdf'
stylesheet=getSampleStyleSheet()
c = canvas.Canvas(path2sizes_pdf, pagesize=A4)
def fonts(canvas):
from reportlab.lib.units import inch
text = "Now is the time for all good men to..."
x = 3 * inch
y = 8 * inch
size = 13
for font in canvas.getAvailableFonts():
canvas.setFont(font, size)
canvas.drawString(x,y,text)
canvas.setFont("Helvetica", size)
canvas.drawRightString(x,y, font+":")
y = y-size
size = size + 1
fonts(c)
c.showPage() # closes the page, can't add more to this page
c.save()
path2colour_pdf = 'reportlab/pdfs/text_colours.pdf'
mycolours = [pink, green, brown, white, black]
def colours(canvas):
from reportlab.lib.units import inch
text = "Now is the time for all good men to..."
x = 2 * inch
y = 8 * inch
for colour in mycolours:
canvas.setFont("Helvetica", 14)
c.setFillColor(colour)
canvas.drawString(x,y,text)
y = y-15
colours(c)
c.showPage() # closes the page, can't add more to this page
c.save() You can direct reportlab to any font you like and then use that font.
from reportlab.pdfbase import pdfmetrics
# important for getting a Chinese ttf
fontpath = '/home/pedro/.local/share/fonts/'
ttfFile = os.path.join(fontpath, '萌萌哒情根深种-中文.ttf')
pdfmetrics.registerFont(TTFont("Chinese", ttfFile))
Posts: 5
Threads: 0
Joined: Jan 2025
Jan-19-2025, 09:02 AM
(This post was last modified: Jan-19-2025, 09:30 AM by Gribouillis.)
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");
|