av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Python數(shù)據(jù)庫格式化輸出文檔的思路與方法

瀏覽:2日期:2022-06-25 16:13:39
問題

如果文案格式是統(tǒng)一的,是否可以通過Python格式化輸出doc/md的文檔?

能用代碼搞定的,盡力不手工

思路

首先,數(shù)據(jù)已經(jīng)錄入庫,需要python能讀取數(shù)據(jù)庫,可使用mysql-connector

其次,格式化輸出的文檔,肯定需要文件讀寫操作,需使用os

接著,考慮到各大平臺多數(shù)支持markdown格式,優(yōu)先輸出md格式文檔。若輸出doc,需使用docx

補(bǔ)充,python一鍵執(zhí)行,分頁數(shù)據(jù)操作,接收外部參數(shù),需使用sys

編碼

分頁獲取數(shù)據(jù)庫內(nèi)容

import mysql.connector# 數(shù)據(jù)庫中page頁數(shù)據(jù)def fetch_data_from_db(page): cmd = ’select * from xxx order by id limit ’ + str(page * 50) + ’, ’ + str(50) conn = mysql.connector.connect(user=’xxx’, password=’xxx’, database=’xxx’) cursor = conn.cursor() cursor.execute(cmd) values = cursor.fetchall() conn.commit() cursor.close() conn.close() return values

格式化輸出md文檔,md中添加表格樣式

import mysql.connector# 數(shù)據(jù)庫中page頁數(shù)據(jù)def fetch_data_from_db(page): cmd = ’select * from xxx order by id limit ’ + str(page * 50) + ’, ’ + str(50) conn = mysql.connector.connect(user=’xxx’, password=’xxx’, database=’xxx’) cursor = conn.cursor() cursor.execute(cmd) values = cursor.fetchall() conn.commit() cursor.close() conn.close() return values

格式話輸出doc文檔

from docx import Documentfrom docx.shared import Cmdef export_format_md(page, books): fileName = ’善齋書屋第’ + str(page) + ’期.docx’ document = Document() table = document.add_table(rows = 51, cols = 3) # 設(shè)置行列數(shù) table.cell(0, 0).text = '索引' table.cell(0, 1).text = '作者' table.cell(0, 2).text = '書名' for index, book in enumerate(books): table.cell(index+1, 0).text = '{0:05d}'.format(book[0]) table.cell(index+1, 1).text = book[2] table.cell(index+1, 2).text = book[1] document.save(fileName)

外部傳參獲取

if __name__ == ’__main__’: args = sys.argv if len(args) == 2: # 獲取分頁 page = args[1] books = fetch_data_from_db(page) export_format_md(page, books)

一鍵執(zhí)行

python3 xxxx.py 0總結(jié)

到此這篇關(guān)于Python數(shù)據(jù)庫格式化輸出文檔的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)庫格式化輸出內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 精品国产一区二区在线观看 | 午夜免费福利视频 | 一区二区三区视频 | 国产精品久久久精品 | 99午夜| 9l视频自拍九色9l视频成人 | 色偷偷网站 | 天天躁日日躁bbbbb | 国产黄色在线 | 二区三区在线观看 | 欧美黑人一区二区三区 | 免费看一级毛片 | 久久久久国产一区二区三区 | 久久久黄色片 | 国产三级午夜理伦三级 | 精品免费国产一区二区三区四区 | 狼人色 | 毛片在线观看网站 | 欧美综合一区 | 91麻豆精品一区二区三区 | 玖玖在线观看 | 国产在线网站 | 91一区二区 | 91禁蘑菇在线看 | 国产第二页 | 成人深夜| 一级特黄妇女高潮 | 婷婷综合激情 | 日韩一区二区在线观看视频 | 国产三级在线观看视频 | 国产91免费视频 | 国产91av在线| 伊人久久av | 视频一区二区在线 | 中文字幕在线观看一区二区三区 | 自拍偷拍第五页 | 亚洲天堂影院 | 精品欧美在线 | 日韩综合久久 | 久久精品欧美一区二区三区不卡 | 日韩精品综合 |