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
Brick Number stored as text with openpyxl CAD79 1 218 Mar-19-2024, 08:31 PM
Last Post: deanhystad
  Text parsing Arik 5 309 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  replace text in a txt cartonics 19 2,075 Jan-30-2024, 06:58 AM
Last Post: Athi
  Text conversion to lowercase is not working ineuw 3 400 Jan-16-2024, 02:42 AM
Last Post: ineuw
  Script that alternates between 2 text messages DiscoMatic 1 493 Dec-12-2023, 03:02 PM
Last Post: buran
  Decryption not working if key has same symbol like text Paragoon2 0 292 Nov-11-2023, 09:32 PM
Last Post: Paragoon2
  Replace a text/word in docx file using Python Devan 4 2,857 Oct-17-2023, 06:03 PM
Last Post: Devan
  Python script that deletes symbols in plain text nzcan 3 642 Sep-05-2023, 04:03 PM
Last Post: deanhystad
  Form that puts diacritics on the words in the text Melcu54 13 1,383 Aug-22-2023, 07:07 AM
Last Post: Pedroski55
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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