Pythonデータベースフォーマット出力ドキュメント
2355 ワード
import mysql.connector
# page
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
import os
# python
def export_format_md(page, books):
fileName = ' ' + str(page) + ' .md'
#
fd = open(fileName, 'a')
fd.write(' :
')
fd.write('| | | |
')
fd.write('|:-|:-|:-|
')
for book in books:
fd.write('|' + "{0:04d}".format(book[0]) + '|' + book[2] + '|' + book[1] + '
')
from docx import Document
from docx.shared import Cm
def export_format_md(page, books):
fileName = ' ' + str(page) + ' .docx'
document = 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 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)
python3 xxxx.py 0