如何从文本中删除最新的换行符?

时间:2019-11-25 08:31:58

标签: python-3.x pyfpdf

我有一个代码可以解析文件夹中的所有文件并将文本写入pdf文件。我想删除文本末尾的换行符,因为在某些格式下,脚本会错误地创建空白页。我该怎么办?

import os
import re
from fpdf import FPDF
ext = [".png", ".ico", ".gif", ".svg", ".woff", ".jpg", ".ttf", ".woff2", "xlsx", ".pdf", ".pkl"]


for item in os.listdir():
    if os.path.isfile(os.path.join(item)):
        pass
    elif os.path.isdir(os.path.join(item)):
        pdf = FPDF()
        pdf.add_font('RobotoMono', '', 'RobotoMono-Regular.ttf', uni=True)
        pdf.add_font('RobotoMono', 'B', 'RobotoMono-Bold.ttf', uni=True)
        directory = os.getcwd()
        directory = directory + '/{}'.format(item)
        txt = None
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file.endswith(tuple(ext)):
                    continue
                else:
                    print(file)
                    prev_page = 1
                    pdf.add_page()
                    pdf.set_font('RobotoMono', 'B', 15)
                    if txt is None:
                        txt = item
                        pdf.cell(70)
                        pdf.write(6, item)
                        pdf.set_font("RobotoMono", '', 12)
                        pdf.set_y(-10)
                        page = 'Страница ' + str(pdf.page_no())
                        pdf.cell(350, -12, page, 0, 0, 'C')
                        pdf.set_font('RobotoMono', 'B', 15)
                        pdf.add_page()
                    text = os.path.join(root, file)
                    text = re.split(item, text)
                    pdf.cell(175, 20, txt=text[1], ln=1, align="C")
                    pdf.set_font("RobotoMono",'', 12)
                    with open(os.path.join(root, file), 'r') as f:
                        sss = f.readlines()
                        for s in sss:
                            show_y = pdf.get_y()
                            cur_page = pdf.page_no()
                            #pdf.write(7, s.rstrip())
                            pdf.write(7, s)
                            if prev_page != cur_page:
                                prev_page = pdf.page_no()
                                pdf.set_y(-10)
                                page = 'Страница ' + str(cur_page)
                                pdf.cell(350, -12, page, 0, 0, 'C')
                                show2_y = pdf.get_y()
                                if show2_y != show_y:
                                    pdf.set_y(show_y + 7)
        pdf.output("{}.pdf".format(item))

0 个答案:

没有答案