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?
#1
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
Reply
#2
https://python-docx.readthedocs.io/en/latest/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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!
Reply
#4
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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!
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 364 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Python code for alignment and font size 1418 0 275 Jan-14-2024, 03:56 AM
Last Post: 1418
  write to csv file problem jacksfrustration 11 1,373 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,309 Nov-09-2023, 10:56 AM
Last Post: mg24
  logging: change log file permission with RotatingFileHandler erg 0 959 Aug-09-2023, 01:24 PM
Last Post: erg
  How can I change the uuid name of a file to his original file? MaddoxMB 2 868 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Change font in a list or tuple apffal 4 2,635 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  How do I read and write a binary file in Python? blackears 6 6,015 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Converted EXE file size is too large Rajasekaran 0 1,445 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020