Python Forum
How best to format text in a simple text file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How best to format text in a simple text file?
#1
I have text documents. They consist of 1 sentence on each line, maybe 35 lines. I want to add the correct sentence from the answers text file under each line and give the whole thing a bit of formatting.

I think pprint presents text neatly. Can it do simple formatting and write to a text file?

I basically want an indent and some space at the top and/or bottom and some line spacing.

When I batch print the text files I have from a bash terminal, the text is left, no margin, top, no margin.

I can think of ways to add spaces left and above, but I think there is probably a module to handle this.

What's the best way to do this?

How would you do it?

Maybe I should use the docx module? I'd rather stick with simple text files if I can.
Reply
#2
Not sure about a module but you could make something easily.
Bash typically has 80 columns, but that and the number of lines can change. If you want to center the text you can ask bash the columns and rows of the current window.
Use subprocess.check_output to run a shell command and return the output as a string.
tput lines and tput cols are the terminal commands that should tell you the height and width respectively.
Then just split your space based on the length of your strings.
Reply
#3
Hello,

I don't get the point here... If you write the text file yourself, you have all the control of adding blank lines, spaces, tabs etc. Sure, you have to take care of that yourself, as there is no module with automagically guesses what you want to file to look like and formats it like this.

I we recommend to put the text into a list of strings with the intented formatting and than write as a text file to the hard drive.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  open a text file using list() Pedroski55 2 114 Feb-25-2026, 06:57 PM
Last Post: noisefloor
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 7,272 Dec-16-2025, 05:29 PM
Last Post: zodazy
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 858 Nov-04-2025, 11:51 AM
Last Post: buran
Question Parse Markdown / get the plain text SpongeB0B 8 4,290 Oct-07-2025, 06:14 PM
Last Post: noisefloor
  replace or remove text from many text files Curbie 21 3,245 Jul-07-2025, 02:05 PM
Last Post: Curbie
  Picamera2 add text to camera preview GigiG 0 992 May-26-2025, 11:46 AM
Last Post: GigiG
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 36,670 May-20-2025, 12:26 PM
Last Post: hanmen9527
  Paste text with caret already positioned inside a placeholder; Wehaveall 2 1,704 May-14-2025, 01:12 AM
Last Post: armorerratic
  subprocess check_output text cut off Axel_Erfurt 5 1,826 Feb-20-2025, 02:15 PM
Last Post: DeaD_EyE
  Python - Hidden Text / Html Mail python1337 1 4,492 Feb-08-2025, 10:47 AM
Last Post: python1337

Forum Jump:

User Panel Messages

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