Python运行出来的结果怎么显示到html上

Python运行出来的结果怎么显示到html上,第1张

我大概看了你所提问的内容,你的意思应该是想把从数据库里所读取到的内容,保存为html格式文件,方便查看。是这样吧?

这里我简单写了这样的代码,代码的思路是:

创建一个html后缀的文件,然后利用文件 *** 作的相关知识写橡码入文件,完成后,可以直接打开file.html查看。

和你的思路能对上,你可以读取数据库内容,然后格式成html规范,写入file.html文件。完成后,就可以打开file.html查看你的结果。

以下是我写的代码,你参考下:(代码相对来说比较简单,这只是给你一个思路,具体你可以依据这个方向进行修改完善)

python3.6环境

# 该代码运行于至少python3.6支持

# 功能:把内容保存为html格式文件

with open('file.html','w') as file: #以w的模式打开file.html文件,不存在就新建

    file.write('<html><body><table border=1><tr><th>a列表</th><th>b列表</th></tr><indent>输出结果:') #使用write写入字符串内容到file.html

    for i in range(10):#执行一个遍历 *** 作

        a=i #i依次赋值给a,i内容为0,1,2,3,4,5,6,7,8,9

        b=i**2 #把i的值依次进行i的2次幂 *** 作

        file.write("<tr><td>"f'{a}'"</td><td>"f'{b}'"</td></tr>") #使用write写入字符串内容到file.html

    file.write('</indent></table></body></html>') #使用write写入字符串内容到file.html

python3环境

# 该代码运行于和销python3

# 功能:把内容保存为html格式文件

with open('file.html','w') as file: #以w的模式打开file.html文件,不存在就新建

    file.write('<html><body><table border=1><tr><th>a列表</th><th>b列表</th></tr><indent>输出结果:') #使用write写入字符串内容到file.html

    for i in range(10):#执行一个遍历 *** 作

        a=i #i依次赋唤如游值给a,i内容为0,1,2,3,4,5,6,7,8,9

        b=i**2 #把i的值依次进行i的2次幂 *** 作

        file.write('<tr><td>{}'.format(a)+'</td><td>{}'.format(b)+'</td></tr>') #使用write写入字符串内容到file.html

    file.write('</indent></table></body></html>') #使用write写入字符串内容到file.html

纯手工,如果对你有帮助望采纳!

pip install pydocx

from pydocx import PyDocX

html = PyDocX.to_html("test.docx")

f = open("test.html", 'w', encoding="utf-8")

f.write(html)

f.close()

通过网页上传word文档,只携歼拦接收docx

<form method="post" enctype="multipart/form-data">

<input type="file" name="file" accept="application/vnd.openxmlformats-officedocument.wordprocessingml.document">

<改码/form>

windows下,将doc转为docx

pip3 install pypiwin32

from win32com import client

word = client.Dispatch("Word.Application")

doc = word.Documents.Open("D:\ \ .doc") //绝对路辩胡径 doc文件

doc.SaveAs("D:\ \ .docx",16) //保存的docx 文件,绝对路径

doc.Close()

word.Quit()

没有,配迅启需要自己解析字符,比如

# coding: utf-8

text = '''今天,晴,多云,23℃/31℃

明天,多云,中雨,25℃/31℃

后天,中雨,小雨,25℃/30℃

周一,小雨,多云,26℃/32℃

周二,多云,多云,27℃/34℃

周三,多云,多云,28℃/36℃

周四,多云,多云,28℃/36℃'''

tables = [[],[],[],[]]

for line in text.splitlines():

    words = line.split(',')

 培如   for i in range(4):

        tables[i].append(words[i])

print '<table>'

for tr in 昌此tables:

    print '<tr>\n<td>'+'</td>\n<td>'.join(tr)+'</td>\n</tr>'

print '</table>'

截图:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12225083.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存