Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
docs for text2pdf
#1
I want to covert some (very simple) text files to pdf. To that end, I imported the text2pdf module. However, I cannot find any documentation on the usage. So I tried things like:

pdf = text2pdf.pdf(file)
pdf = text2pdf.write(file)
pdf = text2pdf.convert(file)

Can anyone point me at documentation for text2pdf.

The reason for this is, I want to use python to print the textfiles. I imported cups and printed a text file:

Quote:conn.printFile('EPSON-L350-Series', '/home/pedro/getEmailtexts/stragglers/17BElate.pdf', 'test', {})

This works great, but there is no kind of formatting. The text is written right along the top edge of the page and very close to the right hand edge.

I tried to print the text as a Libre Office .odt file or a .docx file, but cups can't handle that. When I save the text file as pdf, cups prints it nicely.

Therefore, I would like to batch convert 300+ text files to pdf.
Reply
#2
I don't know this module, is it the one from pypi ? It looks pretty basic, you can simply open the python files and look into the module to see what you can import.

If you like an old solution in linux, you could try a2ps combined with ps2pdf, for example for file 'foo.txt'
Output:
a2ps -B foo.txt --columns=1 -R --borders=no -o foo.ps && ps2pdf foo.ps
then open or print foo.pdf

a2ps was designed to print computer programs, but for simple emails, it should work well.

If there is a problem with the utf8 encoding you can try

Output:
cat foo.txt | iconv -c -f utf-8 -t ISO-8859-1 | a2ps -B --columns=1 -R --borders=no -o foo.ps
Reply
#3
Thanks I'm not sure where it is from, I just ran pip3 install text2pdf

I can't see how to run it in a shell.

I found that text2pdf works in a bash shell. Someone from linuxquestions helped me out:

for i in *.txt; do text2pdf -o "$i".pdf "$i"; done

I have 300+ files to convert, so a script is very useful.
Reply
#4
Link to what library you use.
If look at this there are several with same name,
this one seems update txt2pdf.
Need reportlab,so can use it like this generate bar.txt --> output.pdf.
C:\code
λ pip install reportlab
Collecting reportlab
  Downloading https://files.pythonhosted.org/packages/33/ba/6978e6e62b3503c8e81670e80853f1467b54fe4bac8569646a6d86312ca3/reportlab-3.5.12-cp37-cp37m-win32.whl (2.2MB)
    100% |████████████████████████████████| 2.2MB 2.3MB/s
Requirement already satisfied: pillow>=4.0.0 in c:\python37\lib\site-packages (from reportlab) (5.2.0)
Installing collected packages: reportlab
Successfully installed reportlab-3.5.12

C:\code
λ python txt2pdf.py bar.txt
Writing 'bar.txt' with 80 characters per line and 60 lines per page...
PDF document: 1 pages
Reply
#5
(Dec-16-2018, 11:57 AM)Pedroski55 Wrote: I have 300+ files to convert, so a script is very useful.
If i try to doing it with code used over.
So it's command line based,to not rewrite can use subprocess in a loop.
import glob
import subprocess

for start,file in  enumerate(glob.glob("*.txt"), 1):
    subprocess.run(['python', 'txt2pdf.py', '--output', f'txt{start}.pdf', file])
So glob find all .txt in folder,then use output argument to make new file name each time.
So output will be.
Output:
txt1.pdf txt2.pdf txt3.pdf txt4.pdf ...ect
Reply
#6
On ReportLab:
Blog here: https://www.blog.pythonlibrary.org/2018/...reportlab/

If you are interested in a book on Reportlab, I'd suggest: https://leanpub.com/reportlab
code examples may be helpful: https://github.com/driscollis/reportlabbookcode
Reply
#7
Thanks all! Thanks for the links!

reportlab looks great. I fetched it with pip3 install. It's going to take a while for my puny brain to digest.

The book looks way over my level, but very interesting.

Please excuse my ignorance, but computing is not my thing.

Question: How do I integrate my /path/to/myfiles/ in snippsat's code?

Like this maybe: path = '/home/pedro/mytextfiles/'

then:

for start,file in enumerate(glob.glob(path + "*.txt"), 1):
subprocess.run(['python', 'txt2pdf.py', '--output', f'txt{start}.pdf', file])

Will that work??
Reply
#8
Use code tag.
Pedroski55 Wrote:Question: How do I integrate my /path/to/myfiles/ in snippsat's code?
Yes you can do it like that,if you new to dealing with files/folder always do a test first with eg print().
So with this it should print out all .txt files in mytextfiles folder.
import glob
import subprocess

path = '/home/pedro/mytextfiles/'
for start,file in  enumerate(glob.glob(f"{path}*.txt"), 1):
    print(file)
    #subprocess.run(['python', 'txt2pdf.py', '--output', f'txt{start}.pdf', file])
Example with a solution that keep original file name,if that's needed.
So foo.txt will be named foo.pdf.
import subprocess
import os

path = '/home/pedro/mytextfiles/'
for file in os.scandir(path):
    if file.name.endswith('.txt'):
        #print(file.name)
        subprocess.run(['python', 'txt2pdf.py', '--output', f"{file.name.split('.')[0]}.pdf", os.path.join(path, file.name)])
Reply
#9
Thank you very much! Happy Christmas!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,904 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  pickle docs say bytes in one place, strings in another Skaperen 2 2,145 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  [split] Using this Python version of text2pdf jonsnow1221 1 2,124 May-07-2019, 07:48 PM
Last Post: micseydel
  Using this Python version of text2pdf Pedroski55 1 2,336 Apr-15-2019, 08:12 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