0

我有一个代码可以解析文件夹中的所有文件并将文本写入 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))
4

0 回答 0