Python Forum
How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? (/thread-11030.html)



How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - JohnJSal - Jun-19-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


RE: How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - buran - Jun-19-2018

https://python-docx.readthedocs.io/en/latest/


RE: How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - JohnJSal - Jun-19-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!


RE: How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - buran - Jun-19-2018

(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.


RE: How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - JohnJSal - Jun-19-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!


RE: How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - buran - Jun-19-2018

(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


RE: How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - JohnJSal - Jun-19-2018

Thanks!