如何用Python把html转为PDF(纯页面内容、纯漂浮页内容、完美没水印和标记)

__完成所有步骤大概需要15分钟__

  度娘搜一搜其实很多在线转换网站,但我们都清楚,这些网站的特点是:质量差、转换量低、有水印、不支持漂浮页、下载过程繁琐……,总结就是:要充钱,下面放一个免费、高质量、无水印、无限转换量、支持漂浮页、一键导出、一劳永逸的教程

01 - 适用场合

  教程适用于html web to PDF以及html file to PDF,还适合于漂浮页的PDF导出,可以直接把某个在线网页进行转换,也可以把本地网页文件进行转换,最终都会转换成PDF

02 - 环境要求

  Windows、Python 2.x 以上(如果不会安装Python的,直接下载一个Anaconda,一键安装后,打开Spyder就可以进行Python编程)
Alt

03 - 操作步骤

  编程API来源于一个网站pdfcrowd,它提供免费的在线转换,但是在线转换有标记以及水印等问题,所以我们使用这个网页提供的API进行编程转换,这样就不会有标记和水印
Alt

3.1 - 注册账号

  进入pdfcrowd,点击右上方的Sign up,填写用户名、密码和邮箱进行注册,然后到邮箱中确定注册,随后便自动返回页面登陆成功
Alt

3.2 - 安装pdfcrowd包

  pdfcrowd是该网页提供的API,win+R打开命令行,输入:pip install pdfcrowd,随后安装完成
Alt

3.3 - 获取API Key

  登陆成功后网页右上角点击自己的用户名,进入账户管理页面,在左侧点击Change API Key获得Key,这个Key需要在编程中使用
Alt

3.4 - 编程运行

  注册时的用户名替换到your_name中,上一步得到的key替换到your_key中,网页地址 / 本地html文件替换到your_url / your_path中,运行程序,在程序同一个目录下得到example.pdf

html web to PDF(网页转PDF)

import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.HtmlToPdfClient('your_name', 'your_key')

    # run the conversion and write the result to a file
    client.convertUrlToFile('your_url', 'example.pdf')
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise

html file to PDF(文件转PDF)

import pdfcrowd
import sys

try:
    # create the API client instance
    client = pdfcrowd.HtmlToPdfClient('your_name', 'your_key')

    # run the conversion and write the result to a file
    client.convertFileToFile('your_path', 'example.pdf')
except pdfcrowd.Error as why:
    # report the error
    sys.stderr.write('Pdfcrowd Error: {}\n'.format(why))

    # handle the exception here or rethrow and handle it at a higher level
    raise

3.5 - 运行结果

  以小白的FreeRTOS主页为例,转换为PDF,效果如下:
  转换的结果不是图片,而是与网页相符的文件+链接+图片,矢量格式,无限放大,无水印
Alt
  另外还可以转换网页中的漂浮页,比如某些公司官网的电子offer,一般都是漂浮页,如果直接打印就会把漂浮页后面的内容也打印,此时可以先转换为PDF,稍加处理后再打印

04 - 总结

  Python其实自带了一个html转PDF的模块pdfkit,本质上是wkhtmltopdf网页API的封装,与小白的操作异曲同工,但是用之前需要下载wkhtmltopdf这个软件,然后还需要设置它的路径,过程更加繁琐一点,而pdfkit的转换效果与pdfcrowd差不多,小白更喜欢简单易操作的pdfcrowd

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好,可以使用Python中的PyPDF2和ReportLab库来进行Word转换为PDF格式,PDF文件的合并、拆分、页面旋转和增加水印。 对于Word转为PDF文档的格式转化,可以使用ReportLab库中的platypus包和pydocx包将Word文档转换为PDF文件。可以按照以下步骤进行操作: 1. 安装ReportLab库。可以使用pip命令进行安装,命令如下: `pip install reportlab` 2. 安装pydocx库。可以使用pip命令进行安装,命令如下: `pip install pydocx` 3. 打开Word文档,读取文档内容并用platypus包中的SimpleDocTemplate类创建PDF文档。将读取的内容添加到PDF文档中,并保存为PDF文件。以下是示例代码: ``` from docx import Document from reportlab.lib.pagesizes import A4, portrait from reportlab.platypus import SimpleDocTemplate, Paragraph from reportlab.lib.styles import getSampleStyleSheet def word_to_pdf(word_path, pdf_path): document = Document(word_path) doc = SimpleDocTemplate(pdf_path, pagesize=portrait(A4)) styles = getSampleStyleSheet() paragraph_list = [] for para in document.paragraphs: text = para.text p = Paragraph(text, styles["Normal"]) paragraph_list.append(p) doc.build(paragraph_list) word_to_pdf("word_document.docx", "pdf_document.pdf") ``` 对于PDF文件的合并、拆分,可以使用PyPDF2库中的PdfFileMerger和PdfFileReader类进行操作。以下是示例代码: ``` from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter def merge_pdfs(file_paths, output_path): merger = PdfFileMerger() for path in file_paths: merger.append(path) merger.write(output_path) merger.close() def split_pdfs(input_path, output_path): input_pdf = PdfFileReader(open(input_path, "rb")) for i in range(input_pdf.numPages): output_pdf = PdfFileWriter() output_pdf.addPage(input_pdf.getPage(i)) with open(output_path.format(i), "wb") as outputStream: output_pdf.write(outputStream) merge_pdfs(["pdf_document1.pdf", "pdf_document2.pdf"], "merged_document.pdf") split_pdfs("pdf_document.pdf", "pdf_page_{}.pdf") ``` 对于PDF页面旋转,可以使用PyPDF2库中的PdfFileReader和PdfFileWriter类进行操作。以下是示例代码: ``` from PyPDF2 import PdfFileReader, PdfFileWriter def rotate_pdf(input_path, output_path, rotation_angle): input_pdf = PdfFileReader(open(input_path, "rb")) output_pdf = PdfFileWriter() for i in range(input_pdf.numPages): page = input_pdf.getPage(i) page.rotateClockwise(rotation_angle) output_pdf.addPage(page) with open(output_path, "wb") as outputStream: output_pdf.write(outputStream) rotate_pdf("pdf_document.pdf", "rotated_document.pdf", 90) ``` 对于PDF页面增加水印,可以使用PyPDF2库中的PdfFileReader和PdfFileWriter类进行操作。以下是示例代码: ``` from PyPDF2 import PdfFileReader, PdfFileWriter from reportlab.lib.units import mm from reportlab.pdfgen import canvas def add_watermark(input_path, output_path, watermark_text): c = canvas.Canvas("watermark.pdf", pagesize=A4) c.setFont('Helvetica', 80) c.setFillGray(0.5) c.saveState() c.rotate(45) c.drawString(0, 0, watermark_text) c.restoreState() c.save() input_pdf = PdfFileReader(open(input_path, "rb")) output_pdf = PdfFileWriter() watermark_pdf = PdfFileReader(open("watermark.pdf", "rb")) for i in range(input_pdf.numPages): page = input_pdf.getPage(i) page.mergePage(watermark_pdf.getPage(0)) output_pdf.addPage(page) with open(output_path, "wb") as outputStream: output_pdf.write(outputStream) add_watermark("pdf_document.pdf", "watermark_document.pdf", "Confidential") ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值