![]() |
Using Pypdf2 write a string to a pdf file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using Pypdf2 write a string to a pdf file (/thread-17452.html) |
Using Pypdf2 write a string to a pdf file - Pedroski55 - Apr-11-2019 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' RE: Using Pypdf2 write a string to a pdf file - ichabod801 - Apr-11-2019 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. RE: Using Pypdf2 write a string to a pdf file - Larz60+ - Apr-11-2019 ReportLab might be a better choice. https://www.reportlab.com/ tutorial: https://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/ RE: Using Pypdf2 write a string to a pdf file - snippsat - Apr-11-2019 (Apr-11-2019, 03:16 PM)Larz60+ Wrote: I want to write each line to a pdfMake each line into new a text file.
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)])
RE: Using Pypdf2 write a string to a pdf file - Pedroski55 - Apr-11-2019 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") RE: Using Pypdf2 write a string to a pdf file - Larz60+ - Apr-11-2019 fpdf hasn't been updated since January 20, 2015 there is fpdf2 which is right up to date see: https://pypi.org/project/fpdf2/ RE: Using Pypdf2 write a string to a pdf file - snippsat - Apr-11-2019 (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 fileYou 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. |