0

I have few text documents in a folder and would like to convert them to PDF Format. I am able to do it individually with the below code. Is there any way to select all the text files from a folder, convert and save them with the original name (as it was for text documents)? Below is the code I used to convert each file:


from fpdf import FPDF
pdf = FPDF()

pdf.add_page()
pdf.set_font("Arial", size = 8)

f = open("C:\\Users\\rc06587\\Desktop\\Text Doc\\Sample\\New folder (2)\\exp_000004593_cn23rk__09042020000000_citrixsystemsinc_92080217mr_00384.TXT", "r")

for x in f:
    pdf.cell(10, 4, txt = x, ln = 1, align = 'L')

pdf.output("exp_000004593_cn23rk__09042020000000_citrixsystemsinc_92080217mr_00384.TXT.pdf")

Olivier
  • 13,283
  • 1
  • 8
  • 24
Sai Teja
  • 1
  • 2

2 Answers2

0

Use os.listdir() to get the files in the directory you want, filter for text files, and then do your conversion.

from fpdf import FPDF

def convert_one_file(path):
    pdf = FPDF()

    pdf.add_page()
    pdf.set_font("Arial", size = 8)

    f = open(path, "r")

    for x in f:
        pdf.cell(10, 4, txt = x, ln = 1, align = 'L')

    pdf.output(path + ".PDF")
    
path = "..."
for f in os.listdir(path):
    if os.path.isfile(os.sep.join((path, f))) and f.endswith(".txt"):
        convert_one_file(os.sep.join((path, f)))

A more elegant solution would probably be to use pathlib to join the paths.

etnguyen03
  • 580
  • 4
  • 19
  • Thanks for taking time and answering this. I am ending up with an error -- "Traceback (most recent call last): File "C:\Users\rc06587\Documents\TexttoPDF\main.py", line 21, in if os.path.isfile(os.sep.join(path, f)) and f.endswith(".txt"): TypeError: str.join() takes exactly one argument (2 given)" Inputs on how to fix this would be appreciated. – Sai Teja Nov 13 '22 at 16:35
  • Sorry, `.join()` takes a list/tuple, not multiple items. Use `os.sep.join((path, f))` instead. – etnguyen03 Nov 13 '22 at 16:48
  • Sorry. I am completely new to this. Could you help with the code. Below is the code am currently using: import os from fpdf import FPDF def convert_one_file(path): pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=8) f = open(path, "r") for x in f: pdf.cell(10, 4, txt=x, ln=1, align='L') pdf.output(path + ".PDF") path = "C:\\Users\\rc06587\\Desktop\\Text Doc\\Sample\\New folder (2)\\" for f in os.listdir(path): if os.path.isfile(os.sep.join(path, f)) and f.endswith(".txt"): convert_one_file(os.sep.join(path, f)) – Sai Teja Nov 13 '22 at 17:39
  • I got it using the below code: import os from fpdf import FPDF def convert_one_file(path): pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=8) f = open(path, "r") for x in f: pdf.cell(10, 4, txt=x, ln=1, align='L') pdf.output(path + ".PDF") path = "C:\\Users\\rc06587\\Desktop\\New folder (2)\\" for f in os.listdir(path): if os.path.isfile(os.sep.join((path, f))): convert_one_file(os.sep.join((path, f))) But the issue is the output is file is in PDF format but being saved as ".txt" at the end. Any help? – Sai Teja Nov 14 '22 at 10:29
0

In windows you dont need python or fpdf just use the command line

enter image description here

set any command line desired to rotate through file list and write them to pdf adapt any cmd to suit. If text is plain, it will become a searchable text.pdf

AllTXT2PDF.cmd

FORFILES /M *.txt /C "cmd /c if @isdir==FALSE write.exe /pt @file \"Microsoft Print to PDF\" \"Microsoft Print to PDF\" \"@file.pdf\""

However as plain text it will be left set default size courier font so for Arial 8 point you need to save as rich text, which is easy enough if you want to style the contents for centering justify colour mixed fonts etc.

enter image description here

enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36