python开发文档管理系统,python如何开发web项目

大家好,给大家分享一下python开发文档管理系统,很多人还不知道这一点。下面详细解释一下。现在让我们来看看!


Python生成HTML文档


1. webbrowser

webbrowser可以读html文件,一般html文件生成可以直接用python读写文件方式写入

import webbrowser
 
#命名生成的html
GEN_HTML = "test.html" 
 
#打开文件,准备写入
f = open(GEN_HTML,'w')
 
#准备相关变量
str1 = 'my name is :'
str2 = '--MichaelAn--'
 
# 写入HTML界面中
message = """
<html>
<head></head>
<body>
<p>%s</p>
<p>%s</p>
</body>
</html>
"""%(str1,str2)
 
#写入文件
f.write(message) 
 
#关闭文件
f.close()
 
#运行完自动在网页中显示
webbrowser.open(GEN_HTML,new = 1) 
'''
webbrowser.open(url, new=0, autoraise=True) 
Display url using the default browser. 
If new is 0, the url is opened in the same browser window if possible.
If new is 1, a new browser window is opened if possible.
If new is 2, a new browser page (“tab”) is opened if possible.
If auto raise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).
'''

2. Jinja2

jinja2是一个处理html的模板引擎。使用jinja2对html模板进行渲染python基础知识点精心整理。(对{{ }}里面的变量)

import jinja2

env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath=''))
template = env.get_template('Template.html')

summary = u"这是一些文字"

# 输出数据表格的代码
table =  df.to_html()

# 输出图表的代码
fig = px.box(data_frame=df, x='one', y='two')
chart = plotly.offline.plot(fig, include_plotlyjs=True, output_type='div')

# 导出图片并保存
# 语法和format类似,‘=’前面是html模板的{{ }}中的变量名,后面是要导入的内容,我喜欢用一样的名字,比较容易识别
html = template.render(summary=summary, table=table, chart=chart)
with open('Report.html', 'w', encoding='utf-8') as f:
        f.write(html)


Python生成PDF文档


1. FPDF

FPDF 是一个 可用于创建 PDF 格式文件的纯 PHP 类,也就说不需要借助 PDFlib 库。FPDF首部的F代表自由,自由软件的自由。
Python中所用到的FPDF是基于PHP版本的FPDF。

from fpdf import FPDF

class PDF(FPDF):
    def header(self):    #添加标题
        # Logo
        self.image('logo_fb.jpg', 10, 8, 33)  #插入图片,X,Y表示图片左上角的XY坐标,33表示图片的大小
        # Arial bold 15
        self.set_font('Arial', 'B', 15)
        # Move to the right
        self.cell(80)
        # Title
        self.cell(30, 10, 'Title', 1, 0, 'C')
        # Line break
        self.ln(20)

    # 添加脚注
    def footer(self):
        # Position at 1.5 cm from bottom
        self.set_y(-15)
        # Arial italic 8
        self.set_font('Arial', 'I', 8)
        # Page number
        self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')

# Instantiation of inherited class
pdf = PDF()
pdf.alias_nb_pages()
pdf.add_font('SIMYOU','','SIMYOU.ttf',True)    #FPDF库自带的字体非常少,如果使用其他字体,需要先add,同时注意要把字体文件放到同一目录下,切记
pdf.set_font('SIMYOU',size=10)    #设置字体,字体,加粗,字号  必须设置,且第二个参数有3个,B加粗,I斜体,U下划线,但不是所有的字体都可以设置
pdf.set_text_color(120,120,120)    #设置颜色,采用RGB方式

pdf.add_page()    #增加一页,这里必须输入,否则无法写入

pdf.ln() #插入新行
for i in range(1, 41):
    pdf.cell(0, 10, 'Printing line number ' + str(i), 0, 1) #插入文本框,0表示文档整个横向长度,10是高度
pdf.multi_cell(0,5,"测试"*100) #如果字数过多,建议使用插入多行

pdf.output('tutorial_02.pdf', 'F')

2. PyPDF2

  • 1,将单个 PDF 拆分为多个 PDF 文件 ;
  • 2,将多个 PDF 合并为一个 PDF 文件 ;
  • 3,将 PDF 中某页进行旋转 ;
  • 4,对 PDF 添加水印 ;
  • 5,对 PDF 加密 ;
  • 6,对 PDF 进行解密;
  • 6,获取 PDF 基本信息,例如作者、标题、页数等;


Python压缩PDF文档


原理:PDF切分为图片,根据压缩率zoom压缩图片后保存本地;图片合成PDF。压缩率越大pdf越大。

安装依赖包:pip install PyMuPDF==1.18.14

import fitz
import os


def covert2pic(zoom):
    if os.path.exists('.pdf'):       # 临时文件,需为空
         os.removedirs('.pdf')
    os.mkdir('.pdf')
    for pg in range(totaling):
        page = doc[pg]
        zoom = int(zoom)            #值越大,分辨率越高,文件越清晰
        rotate = int(0)
        print(page)
        trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).preRotate(rotate)
        pm = page.getPixmap(matrix=trans, alpha=False)
      
        lurl='.pdf/%s.jpg' % str(pg+1)
        pm.writePNG(lurl)
    doc.close()

def pic2pdf(obj):
    doc = fitz.open()
    for pg in range(totaling):
        img = '.pdf/%s.jpg' % str(pg+1)
        imgdoc = fitz.open(img)                 # 打开图片
        pdfbytes = imgdoc.convertToPDF()        # 使用图片创建单页的 PDF
        os.remove(img)  
        imgpdf = fitz.open("pdf", pdfbytes)
        doc.insertPDF(imgpdf)                   # 将当前页插入文档
    if os.path.exists(obj):         # 若文件存在先删除
        os.remove(obj)
    doc.save(obj)                   # 保存pdf文件
    doc.close()


def pdfz(sor, obj, zoom):    
    covert2pic(zoom)
    pic2pdf(obj)
    
if __name__  == "__main__":

    sor = "source.pdf"              # 需要压缩的PDF文件
    obj = "new" + sor
    doc = fitz.open(sor) 
    totaling = doc.pageCount
    
    zoom = 200                     # 清晰度调节,缩放比率
    pdfz(sor, obj, zoom)
    os.removedirs('.pdf')

开源pdf文档编辑工具

https://github.com/ClementGre/PDF4Teachers

https://github.com/JakubMelka/PDF4QT

References

Python 自动化办公 —— PyPDF2 库的基本使用 - 知乎

用Python制作一份HTML报告 - 知乎

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值