Python Forum
Using Pypdf2 write a string to a pdf file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Pypdf2 write a string to a pdf file
#1
I will read 130+ excel files in as lines of strings in Python. I want to write each line to a pdf. Each excel file is just 1 A5 landscape sheet. I can batch print pdfs in a bash shell easily.

I import PyPDF2

I can create a pdf or a series pdf files with:

with open(path + fileName, 'wb') as out:
        pdf_writer.write(out)
but I'm too dumb to see how to write a string to this pdf. If I try to write a string variable, I just get errors. If I convert the string to bytes, I just get errors.

How do I get string into my pdf??

string = 'any old string'
Reply
#2
Creating a new pdf from scratch does not appear to be something PyPDF2 does. It appears to just be for manipulating existing pdfs, such as taking pages from one or more pdfs and making a new pdf out of them. See the about page for the project.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
ReportLab might be a better choice.
https://www.reportlab.com/
tutorial: https://www.blog.pythonlibrary.org/2010/...-tutorial/
Reply
#4
(Apr-11-2019, 03:16 PM)Larz60+ Wrote: I want to write each line to a pdf
Make each line into new a text file.
Output:
# lines.txt line 1 line 2 line 3 line 4
with open('lines.txt') as f:
    for index, line in enumerate(f, 1):
        with open(f'{index}.txt', 'w', encoding='utf-8') as f_out:
             f_out.write(line)
Then use eg txt2pdf.
So can write a script that find all 1.txt 2.txt ...ect,then call txt2pdf with subprocess.
import subprocess
import os

path = r'E:\div_code\pdf1/'
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)])
Output:
1.pdf 2.pdf 3.pdf 4.pdf
Reply
#5
Thank you very much! I can use that!

Question: where should I put text2pdf.py? I mean, so that when I run the python script in bash, bash will find text2pdf.py?

I think I expressed myself badly. I will read each row of each excel file as a string, then write the strings of 1 excel file to 1 pdf, so that 1 pdf contains the data of 1 excel file.

Also, I was advised to use fpdf. I have not tried this yet, ran out of time yesterday.

# https://pyfpdf.readthedocs.io/en/latest/
import fpdf #pip3 install fpdf

pdf = fpdf.FPDF(format='letter') #pdf format
pdf.add_page() #create new page
pdf.set_font("Arial", size=12) # font and textsize
pdf.cell(200, 10, txt="your text", ln=1, align="L")
pdf.cell(200, 10, txt="your text", ln=2, align="L")
pdf.cell(200, 10, txt="your text", ln=3, align="L")
pdf.output("test.pdf")
Reply
#6
fpdf hasn't been updated since January 20, 2015
there is fpdf2 which is right up to date see: https://pypi.org/project/fpdf2/
Reply
#7
(Apr-11-2019, 09:56 PM)Pedroski55 Wrote: Question: where should I put text2pdf.py? I mean, so that when I run the python script in bash, bash will find text2pdf.py?
Work and run all from same folder.
(Apr-11-2019, 09:56 PM)Pedroski55 Wrote: I will read each row of each excel file as a string, then write the strings of 1 excel file to 1 pdf, so that 1 pdf contains the data of 1 excel file
You still not expressed yourself clearly,when you read "each excel file as a string" do you save this to text to eg .txt files?
If all row is text files(.txt),then just run my script and all will text(.txt) files be pdf's.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 365 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,375 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,314 Nov-09-2023, 10:56 AM
Last Post: mg24
  PyPDF2 deprecation problem gowb0w 5 3,532 Sep-21-2023, 12:38 PM
Last Post: Pedroski55
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  ModuleNotFoundError: No module named 'PyPDF2' Benitta2525 1 1,393 Aug-07-2023, 05:32 AM
Last Post: DPaul
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,501 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Pypdf2 will not find text standenman 2 878 Feb-03-2023, 10:52 PM
Last Post: standenman

Forum Jump:

User Panel Messages

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