使用Python怎么在数据库中格式化输出文档

  介绍

使用Python怎么在数据库中格式化输出文档?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

思路

首先,数据已经录入库,需要Python能读取数据库,可使用mysql-connector

其次,格式化输出的文档,肯定需要文件读写操作,需使用os

接着,考虑到各大平台多数支持减价格式,优先输出md格式文档。若输出医生,需使用多克斯

补充,Python一键执行,分页数据操作,接收外部参数,需使用sys

编码

分页获取数据库内容

import  mysql.connector      #,数据库中页面页数据   def  fetch_data_from_db(页面):=,cmd  & # 39; select  *,得到xxx  order  by  id  limit  & # 39;, +, str (page  *, 50), +, & # 39;, & # 39;, +, str (50)=,,conn  mysql.connector.connect(用户=& # 39;xxx # 39;,,密码=& # 39;xxx # 39;,,数据库=& # 39;xxx # 39;)=,,cursor  conn.cursor ()   ,cursor.execute (cmd)=,,values  cursor.fetchall ()   ,conn.commit ()   ,cursor.close ()   ,conn.close (),   ,return 值

格式化输出医学文档,md中添加表格样式

import  mysql.connector      #,数据库中页面页数据   def  fetch_data_from_db(页面):=,cmd  & # 39; select  *,得到xxx  order  by  id  limit  & # 39;, +, str (page  *, 50), +, & # 39;, & # 39;, +, str (50)=,,conn  mysql.connector.connect(用户=& # 39;xxx # 39;,,密码=& # 39;xxx # 39;,,数据库=& # 39;xxx # 39;)=,,cursor  conn.cursor ()   ,cursor.execute (cmd)=,,values  cursor.fetchall ()   ,conn.commit ()   ,cursor.close ()   ,conn.close (),   ,return 值

格式话输出医生文档

得到docx  import 文档   得到docx.shared  import 厘米      def  export_format_md(页面,,书):=,fileName  & # 39;善斋书屋第& # 39;,+,str(页面),+,& # 39;期。docx # 39;=,document 文档()=,,table  document.add_table (rows =, 51岁,cols =, 3), #,设置行列数   ,table.cell (0, 0) .text =,“索引“;   ,table.cell (0, 1) .text =,“作者“   ,table.cell (0, 2) .text =,“书名“   ,for 指数,book 拷贝列举(书):   table.cell才能(指数+ 1,0).text =,“{0:05d}“.format(书[0])   table.cell才能(指数+ 1,,1).text [2]=,书   table.cell才能(指数+ 1,,2).text =,书[1]   之前,document.save(文件名)

外部传参获取

if  __name__ ==, & # 39; __main__ # 39;:=,args  sys.argv   ,if  len (args),==, 2:   #,才能获取分页   page 才能=,arg游戏[1],   时间=books 才能;fetch_data_from_db(页面)   export_format_md才能(页面,,书)

一键执行

python3  xxxx.py  0

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

使用Python怎么在数据库中格式化输出文档