转自:https://www.davidfischer.name/2015/08/generating-pdfs-with-and-without-python/

 from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter
import io def write_pdf_file(path, sentlist):
buf = io.BytesIO() # Setup the document with paper size and margins
doc = SimpleDocTemplate(
buf,
rightMargin=inch / 2,
leftMargin=inch / 2,
topMargin=inch / 2,
bottomMargin=inch / 2,
pagesize=letter,
) # Styling paragraphs
styles = getSampleStyleSheet()
# Write things on the document
paragraphs = []
for sent in sentlist:
if sent.strip() == '':
sent = '_'
paragraphs.append(Paragraph(sent, styles['Normal']))
doc.build(paragraphs)
# Write the PDF to a file
with open(path, 'w') as fd:
fd.write(buf.getvalue()) if __name__ == '__main__':
text=['a','b','c']
path='test.pdf'
write_pdf_file(path,text)

【python】用python生成pdf文件的更多相关文章

  1. python之reportlab生成PDF文件

    项目需要,需要自动生成PDF测试报告.经过对比之后,选择使用了reportlab模块. 项目背景:开发一个测试平台,供测试维护测试用例,执行测试用例,并且生成测试报告(包含PDF和excel),将生成 ...

  2. Python数据生成pdf文件

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  3. 深入学习Python解析并解密PDF文件内容的方法

    前面学习了解析PDF文档,并写入文档的知识,那篇文章的名字为深入学习Python解析并读取PDF文件内容的方法. 链接如下:https://www.cnblogs.com/wj-1314/p/9429 ...

  4. 深入学习python解析并读取PDF文件内容的方法

    这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应 ...

  5. Python 使用cx_freeze 生成exe文件【转】

    Python 使用cx_freeze 生成exe文件   在python中比较常用的python转exe方法有三种,分别是cx_freeze,py2exe,PyInstaller.py2exe恐怕是三 ...

  6. 利用Python将多个PDF文件合并

    from PyPDF2 import PdfFileMerger import os files = os.listdir()#列出目录中的所有文件 merger = PdfFileMerger() ...

  7. linux下编译bib、tex生成pdf文件

    实验: 在linux环境下,编译(英文)*.bib和*.tex文件,生成pdf文件. 环境: fedora 20(uname -a : Linux localhost.localdomain 3.19 ...

  8. ThinkPHP3.2.3扩展之生成PDF文件(MPDF)

    目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendo ...

  9. [轉載]史上最强php生成pdf文件,html转pdf文件方法

    之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...

  10. asp.net生成PDF文件 (1)

    asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...

随机推荐

  1. android http中请求访问添加 cookie

    第一种HashMap<String, String> map = new HashMap<String, String>();map.put("cookie" ...

  2. java实现八种排序算法并测试速度

    速度测试: (1) 随机数范围:0-100希尔排序: => Time is 38600基数排序: => Time is 53300快速排序: => Time is 46500堆  排 ...

  3. fetch使用的常见问题及其解决办法

    摘自: https://segmentfault.com/a/1190000008484070 fetch使用的常见问题及其解决办法 javascript wonyun 2月25日发布 |   0 收 ...

  4. Android中使用adb访问SQLite的方法

    (1)打开命令提示符,输入:adb,按回车,如果得到下面一大堆命令说明(如图 1),表示adb的配置是成功的,如果提示"不是内部或外部命令,也不是可运行的程序或批处理文件",那么需 ...

  5. expdp ORA-39070:Unable to open the log file

    Oracle中,当执行expdp或impdp的时候,有时候会出现错误: [oracle@bi-dw ~]$ expdp dp_user/dp_password@dw directory=expdp_d ...

  6. 阿里云logtail采集IDC机房机器需添加AliUids操作

    Configure AliUids for ECS servers under other Alibaba Cloud accounts or on-premises IDCs If Logtail ...

  7. Ubuntu 安装nginx

    https://www.nginx.com/resources/admin-guide/load-balancer/ https://github.com/gplessis/dotdeb-nginx/ ...

  8. 在代码中设置RelativeLayout布局中标签的android:layout_toLeftOf、android:layout_toRightOf等属性

    需要动态改变RelativeLayout里面控件的相对位置,经一个技术群的群友提示,找到了如下的方法,做下记录:   RelativeLayout.Layoutparams params = (Rel ...

  9. Handler消息传递机制浅析

    http://www.runoob.com/w3cnote/android-tutorial-handler-message.html https://blog.csdn.net/lowprofile ...

  10. 【Java】Comparable和Comparator接口的区别

    Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回负数,0,正数来表明已经存在的对象小于,等于,大于输入对象. Java提供了 ...