python 打印功能测试程序

`
from CDHTMLTable import HTMLTable
from PySide2.QtCore import QRect, QPoint, QSize, Qt
from PySide2.QtGui import QImage, QIcon, QPixmap, QPainter, QTextDocument
from PySide2.QtWidgets import QApplication, QMainWindow, QLabel, QSizePolicy, QAction, QWidget, QTableWidget, QPushButton, QVBoxLayout, QTableWidgetItem,QMessageBox, QDesktopWidget, QStyleFactory
from PySide2.QtPrintSupport import QPrinter, QPrintDialog, QPrintPreviewDialog, QPrintPreviewWidget
import sys
import numpy as np
from option_mssql import mssql_query

def PositionSearch():
sql = '''
select holddate 日期, productname 品种, upper(productgroupID) + DELIVERYMONTH 合约, OPENPRICE 开盘价, HIGHESTPRICE 最高价, LOWESTPRICE 最低价, CLOSEPRICE 收盘价,
settlementprice 结算价, ZD1_CHG 收盘价变化, ZD2_CHG 结算价变化, VOLUME 成交手数, TURNOVER 成交金额, TASVOLUME TAS手数, OPENINTEREST 持仓量
from (select *, ROW_NUMBER() OVER (PARTITION BY holddate,PRODUCTGROUPID ORDER BY VOLUME DESC) dateNumber from F_KX ) T
where holddate >= '2023-01-01' and dateNumber=1 and VOLUME<>0 order by productgroupID, holddate
'''

# 持仓汇总 查询 开始
df = mssql_query(sql, 3)
# df = df.iloc[:,1:]
df.insert(0, '序号', range(1, df.shape[0]+1, 1))
# print(df.head(5000))
return df

def printHtml():
# 标题
table = HTMLTable(caption=('结算价情况表'))
df = PositionSearch()
df = df.replace(np.nan, None) #将值NaN,替换成 None

headers = tuple(df.columns) # 表头内容
tuples = [tuple(x) for x in df.values]
# 表头行
table.append_header_rows((headers,))
# 数据行
table.append_data_rows(tuples)
# 标题样式
table.caption.set_style({
'padding': '2px',
'font-size': '220px',
'text-align': 'center',
})
# 表格样式,即<table>标签样式
table.set_style({
'text-align': 'center',
'border-collapse': 'collapse',
'word-break': 'keep-all',
'white-space': 'nowrap',
'vertical-align': 'middle',
'margin': 'auto',
})
# # 统一设置所有单元格样式,<td>或<th>
table.set_cell_style({
'width': "15%",
'border-color': '#000',
'border-width': '1px',
'border-style': 'solid',
'padding': '3px',
'font-size': '90px',
'text-align': 'center',
})
# # 表头样式
table.set_header_row_style({
'color': '#fff',
'background-color': '#48a6fb',
'font-size': '80px',
'margin-top': '2px',
})
body = table.to_html()
# print(body)
# html的charset='UTF-8'必须加上,否则中午会乱码
html = "<!DOCTYPE html><html><head><meta charset='UTF-8'></head><body>{0}</body></html>".format(body)
# print(html)
# 生成图片
textDocument = QTextDocument()
textDocument.setHtml(html)
textDocument.print_(printer)

def printMain():
# /* 打印预览 */
preview = QPrintPreviewDialog(printer, widget)
preview.setWindowState(Qt.WindowMaximized)
preview.paintRequested.connect(printHtml)
preview.exec()

if name == 'main':
app = QApplication(sys.argv)
printer = QPrinter(QPrinter.HighResolution)
printer.setPageSize(QPrinter.A4)
printer.setOrientation(QPrinter.Landscape) # (QPrinter::Landscape常用)(Orientation(纵向:Portrait 横向:Landscape))
printer.setPageMargins(0.1, 0.1, 0.1, 0.1, QPrinter.Millimeter)

widget = QWidget()
button=QPushButton('打印')
button.clicked.connect(printMain)
layout = QVBoxLayout(widget)
layout.addWidget(button)
widget.setWindowTitle('Pyhton打印测试程序')
widget.setWindowIcon(QIcon("./images/rlylogo.ico"))
widget.show()
sys.exit(app.exec_())

`

posted @   冀未然  阅读(21)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示