[英]How to Convert .txt file to .pdf
i have been trying to convert .txt to .pdf, using the code below我一直在尝试使用下面的代码将 .txt 转换为 .pdf
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] data.txt
将如下: aaaaa: 4 bb: 5 cccccc: 9[data.txt_file][1]
but when convert this data.txt
and the result Data.pdf
will look like:但是当转换这个
data.txt
和结果Data.pdf
将如下所示:
aaaaa: 4 bb: 5 cccccc: 9[Data.pdf_file][2] aaaaa:4 bb:5 cccccc:9[Data.pdf_file][2]
the required converted file should be like same as data.txt
.所需的转换文件应该与
data.txt
相同。
i have also use to get the data.txt
using this piece of code我也使用这段代码来获取
data.txt
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.运行代码后,将创建
data.txt
。 [1]: https://i.stack.imgur.com/5Roql.jpg [2]: https://i.stack.imgur.com/SfJCU.jpg [1]: https : //i.stack.imgur.com/5Roql.jpg [2]: https : //i.stack.imgur.com/SfJCU.jpg
The Aspose.Word Cloud SDK for Python can easily convert Text files to PDF and it honors text styling.适用于 Python的Aspose.Word Cloud SDK可以轻松地将文本文件转换为 PDF,并支持文本样式。 You may try the following sample code.
您可以尝试以下示例代码。 Put your text file on the same level for processing.
将您的文本文件放在同一级别进行处理。
I'm a developer evangelist at Aspose.我是 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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.