Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

drake
drake

Posted on

将中文txt文档转为PDF

pipinstallfpdf
Enter fullscreen modeExit fullscreen mode
  • 代码
fromfpdfimportFPDFclassPDF(FPDF):def__init__(self):super().__init__()# 添加中文字体(以苹方为例,macOS默认字体)self.add_font('kaiti','','./kaiti.ttf',uni=True)deffooter(self):self.set_y(-15)self.set_font('kaiti','',8)# 设置中文字体self.cell(0,10,f'{self.page_no()}',0,0,'C')# 页脚classTopdf:def__init__(self,pdf_source_path,txt_file_path):# PDF文件路径self.pdf_file=pdf_source_path# 输出的TXT文件路径self.txt_file=txt_file_pathdefto_pdf(self):# 创建PDF对象pdf=PDF()pdf.set_left_margin(10)pdf.set_right_margin(10)pdf.add_page()pdf.set_auto_page_break(auto=True,margin=15)pdf.set_font('kaiti','',12)# 读取TXT文件并优化文本内容withopen(self.txt_file,'r',encoding='utf-8')asfile:text=file.read()# 将优化后的文本添加到PDFpdf.multi_cell(0,10,text)# 保存PDF文件pdf.output(self.pdf_file)print(f"TXT内容已成功保存到{self.pdf_file}")
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

生活唯一不可或缺的是学习
  • Location
    Dubai
  • Education
    master
  • Joined

More fromdrake

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp