多時候,你想給一個按鈕,在網頁上通過一個實際的打印機打印出網頁的內容。
JavaScript可使用window對象的print函數就可以實現這樣的功能。
當執行JavaScript的print函數window.print()將會打印當前頁面。可以使用onclick事件如下直接調用此函數:
<head>
<script type="text/javascript">
<!--
//-->
</script>
</head>
<body>
<form>
<input type="button" value="Print" onclick="window.print()" />
</form>
</body>1234567891011復制代碼類型:[javascript]
這將產生以下按鈕,打印此頁。
這符合打印出來的頁面,但這個不是一個推薦的方式。打印機友好的頁面實際上只是一個文本,沒有圖像,圖形或廣告頁面。
可以使用以下頁式打印機友好方式:
使頁面的副本,并離開了不需要的文本和圖形,然后從原始鏈接到該打印機友好的頁面。
如果你不想讓頁面的額外副本,那么可以使用像適當的注釋標記打印文本 <!-- PRINT STARTS HERE -->..... <!-- PRINT ENDS HERE --> 然后你可以使用PERL或其他腳本在后臺清除打印文本和顯示進行最后的打印。網站使用同樣的方法給打印設備對我們網站的訪客。
如果沒有人在提供上述設備,那么你可以使用瀏覽器的標準工具欄讓網頁打印出來。按照鏈接如下:
File --> Print --> Click OK button.
開課吧廣場-人才學習交流平臺
、測試代碼,保存為【test.html】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="write.js"></script>
</head>
<body>
<div id="ID1"></div>
<script>
writeformat(()=>{
writeln(1, 2, 3, 4);
writeln(1, [2, 3], 4);
writeln({a:1,b:2});
writeln(1, [2, 3], [4, { a:1, b: { c:1, d:2 }},{a:1,b:2} ]);
writeln();
});
writebr(1, [2, 3], [4, { a:1, b: { c:1, d:2 }},{a:1,b:2} ]);
document.getElementById("ID1").innerHTML = getWriteHtml(1, [2, 3], [4, { a:1, b: { c:1, d:2 }},{a:1,b:2} ]);
writeformat(()=>{
write(getWriteHtmlPretty(1, [2, 3], [4, { a:1, b: { c:1, d:2 }},{a:1,b:2},5 ],6));
});
</script>
</body>
</html>
效果如下:
2、源碼(保存為 write.js)
不久,我寫了一篇文章回顧 Python 中 print 的發展歷史 ,提到了兩條發展線索:
本文依然跟 print 相關,想介紹的是標準庫中的 pprint 模塊。
pprint 是“pretty printer”的簡寫,“pretty”的含義是“漂亮的、美觀的”,還有表示“相當地”的程度語氣,因此它的含義便是:(相當)美觀的打印。
這是個相當簡單卻有用的模塊,主要用于打印復雜的數據結構對象,例如多層嵌套的列表、元組和字典等。
先看看 print() 打印的一個例子:
mylist = ["Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated."] print(mylist) # 結果如下: ['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']
這是一個簡單的例子,全部打印在一行里。
想象一下,如果對象中的元素是多層嵌套的內容(例如復雜的 Json 數據),或者有超多的元素(例如在列表中存了很多 URL 鏈接),再打印出來會是怎樣?
那肯定是一團糟的,不好閱讀。
使用 pprint 模塊的 pprint() 替代 print(),可以解決如下痛點:
1、簡單使用
語法:pprint(object, stream=None, indent=1, width=80, depth=None, *,compact=False)
默認的行寬度參數為 80,當打印的字符(character)小于 80 時,pprint() 基本上等同于內置函數 print(),當字符超出時,它會作美化,進行格式化輸出:
import pprint # 打印上例的 mylist pprint.pprint(mylist) # 打印的元素是換行的(因為超出80字符): ['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']
2、設置縮進為 4 個空格(默認為1)
pprint.pprint(mylist, indent=4) [ 'Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']
3、設置打印的行寬
mydict = {'students': [{'name':'Tom', 'age': 18},{'name':'Jerry', 'age': 19}]} pprint.pprint(mydict) # 未超長: {'students': [{'age': 18, 'name': 'Tom'}, {'age': 19, 'name': 'Jerry'}]} pprint.pprint(mydict, width=20) # 超長1: {'students': [{'age': 18, 'name': 'Tom'}, {'age': 19, 'name': 'Jerry'}]} pprint.pprint(mydict, width=70) # 超長2: {'students': [{'age': 18, 'name': 'Tom'}, {'age': 19, 'name': 'Jerry'}]}
4、設置打印的層級(默認全打印)
newlist = [1, [2, [3, [4, [5]]]]] pprint.pprint(newlist, depth=3) # 超出的層級會用...表示 [1, [2, [3, [...]]]]
5、優化循環結構的打印
當列表或其它數據結構中出現循環引用時,要完整打印出所有內容是不可能的。
所以 print 作了簡化處理,就像上例一樣,只打印外層的殼,而不打印內層循環的東西。
這種處理方式是簡化了,但沒有指出是誰導致了循環,還容易看漏。
pprint() 方法作了改進,遇到無限循環結構時,會表示成<Recursion on typename with id=number> 的格式。
還有個 saferepr() 方法,也是這樣優化,而且返回的是個字符串:
newlist = [1, 2] newlist.insert(0, newlist) # 列表元素指向列表自身,造成循環引用 # 直接 print 的結果是:[[...], 1, 2] pprint.pprint(newlist) # [<Recursion on list with id=1741283656456>, 1, 2] pprint.saferepr(newlist) # '[<Recursion on list with id=1741283656456>, 1, 2]'
6、判斷是否出現循環結構
有兩個方法可以判斷一個對象中是否出現無限循環:
pprint.isrecursive(newlist) # True pprint.isreadable(newlist) # False
isreadable() 除了能像 isrecursive() 一樣判斷循環,還能判斷該格式化內容是否可被 eval() 重構。
以上就是 pprint 模塊的快捷入門介紹,除此之外,還有 pformat() 方法、PrettyPrinter 類,以及某些參數的使用等內容,我覺得沒有大用,就不多說了。
如若感興趣,你可查閱:
最后,還有兩個小小的點:
1、用 pprint() 替換 print() 的技巧
在不考慮 print() 函數本身的參數的情況下,可以在引入 pprint 模塊后,寫上 “print = pprint.pprint”,令 print() 起到改頭換面的效果:
import pprint print = pprint.pprint mylist = ["Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated."] print(mylist) # 可對比本文開頭的例子 ['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']
2、國人開發的 beeprint
國內某位 pan 同學在 Github 開源了個beeprint,明顯是對標 pprint 的。
項目地址:https://github.com/panyanyany/beeprint
它優化了字典對象的打印,對于從其它語言轉過來的同學而言(例如 Java),這是個福音:
它還優化了長文本的打印,支持自定義對象的打印,看起來不錯。
但是,其它功能不夠齊全,而且作者停止維護兩年了,荒廢已久……
總體而言,pprint 算是 print() 的輕量級替代,簡單實用,極其方便(畢竟是標準庫),文檔豐富而有保障。
所以,若想要打印美觀易讀的數據,這個 pprint 標準庫,不妨一試哦。
作者簡介:豌豆花下貓,生于廣東畢業于武大,現為蘇漂程序員,有一些極客思維,也有一些人文情懷,有一些溫度,還有一些態度。公眾號:「Python貓」(python_cat)
*請認真填寫需求信息,我們會在24小時內與您取得聯系。