Reputation: 19
i have been trying to convert .txt to .pdf, using the code below
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=15)
f = open("data.txt", "r")
for x in f:
pdf.cell(200, 10, txt=x, ln=1, align='L')
pdf.output("Data.pdf")
data.txt
will be as below:
aaaaa: 4
bb: 5
cccccc: 9[data.txt_file][1]
but when convert this data.txt
and the result Data.pdf
will look like:
aaaaa: 4 bb: 5 cccccc: 9[Data.pdf_file][2]
the required converted file should be like same as data.txt
.
i have also use to get the data.txt
using this piece of code
letter = ['aaaaa', 'bb', 'cccccc']
number = [4, 5, 9]
for line in zip(letter, number):
print('{:6}: {}'.format(*line))
i have used tabs in print statement.
after running code the data.txt
will be created.
[1]: https://i.sstatic.net/5Roql.jpg
[2]: https://i.sstatic.net/SfJCU.jpg
Upvotes: 1
Views: 1476
Reputation: 939
The Aspose.Word Cloud SDK for Python can easily convert Text files to PDF and it honors text styling. You may try the following sample code. Put your text file on the same level for processing.
I'm a developer evangelist at Aspose.
# For complete examples and data files, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-python
# Import module
import asposewordscloud
import asposewordscloud.models.requests
from shutil import copyfile
# Please get your Client ID and Secret from https://dashboard.aspose.cloud.
client_id='xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx'
client_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxx'
words_api = asposewordscloud.WordsApi(client_id,client_secret)
words_api.api_client.configuration.host='https://api.aspose.cloud'
filename = 'test.txt'
dest_name = 'test.PDF'
#Convert Text to PDF
request = asposewordscloud.models.requests.ConvertDocumentRequest(document=open(filename, 'rb'), format='pdf')
result = words_api.convert_document(request)
copyfile(result, dest_name)
print("Result {}".format(result))
Upvotes: 1