Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How best to format text in a simple text file?
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.
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.
Pedroski55 likes this post

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
Pedroski55 likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Rename file from value in text file Nuge93 1 97 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk
  Write text expander in Python constantin01 5 148 Jan-16-2020, 08:28 AM
Last Post: buran
  Handling a .txt text reinalazaro 7 267 Jan-11-2020, 09:24 PM
Last Post: reinalazaro
  Preserve xml file format tanffn 3 121 Jan-03-2020, 09:35 AM
Last Post: Larz60+
  get docstring to work with another block of text at the top Skaperen 4 209 Dec-28-2019, 10:37 PM
Last Post: Skaperen
  To Copy text values to Excel using Python ksasi2k3 16 2,224 Dec-26-2019, 11:59 AM
Last Post: ashutoshdeodhar
  Sublime text 3 and lxml DreamingInsanity 2 155 Dec-22-2019, 01:49 PM
Last Post: DreamingInsanity
  Detect end of line in text file including line breaks DanielM 4 190 Dec-18-2019, 11:57 AM
Last Post: Malt
  Highlight and remove specific string of text itsalmade 5 230 Dec-11-2019, 11:58 PM
Last Post: micseydel
  Can't seem to figure out how to delete several lines from a text file Cosmosso 9 350 Dec-10-2019, 11:09 PM
Last Post: Cosmosso

Forum Jump:

Users browsing this thread: 1 Guest(s)