raspberry pi 5 用 x11 時,強制螢幕休眠
底下程式執行,螢幕就休眠,一動鍵盤或滑鼠,就會恢復螢幕畫面。
這是用 python 來執行。
import os
os.system("xset dpms force off") # 關閉螢幕(休眠)
事實上,最重要的是這一行指令,在 terminal 底下一執行就可以
xset dpms force off
用 js 抓出所有可用的多媒體裝置
●test.htm (要放在有 https 的地方)
<!doctype html>
<html lang="zh-Hant-TW">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=classDevice-width, initial-scale=1.0">
<title>裝置媒體裝置列舉</title>
<style>
body
{
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
button
{
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover
{
background-color: #45a049;
}
.classDevice
{
padding: 10px;
margin: 5px 0;
background-color: #f5f5f5;
border-radius: 4px;
}
.classDeviceList
{
margin-top: 20px;
}
.classDeviceCategory
{
margin-bottom: 30px;
}
</style>
</head>
<body>
<h1>媒體裝置列舉</h1>
<p>此頁面將列出所有可用的攝影鏡頭和麥克風裝置。</p>
<button id="btnRefresh" onclick="funcEnumerateDevices();">重新整理裝置列表</button>
<div class="classDeviceList">
<div class="classDeviceCategory">
<h2>攝影鏡頭</h2>
<div id="divVideoInputDevices"></div>
</div>
<div class="classDeviceCategory">
<h2>麥克風</h2>
<div id="divAudioInputDevices"></div>
</div>
</div>
<script>
async function funcEnumerateDevices()
{
try
{
// 請求使用者權限(這會觸發瀏覽器的權限對話框)
await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
// 列舉所有媒體裝置
const oDevices=await navigator.mediaDevices.enumerateDevices();
// 過濾並顯示攝影鏡頭
const oVideoInputDevices=oDevices.filter(oDevice => oDevice.kind==='videoinput');
funcDisplayDevices(oVideoInputDevices, 'divVideoInputDevices');
// 過濾並顯示麥克風
const oAudioInputDevices=oDevices.filter(oDevice => oDevice.kind==='audioinput');
funcDisplayDevices(oAudioInputDevices, 'divAudioInputDevices');
}
catch(oError)
{
console.error('列舉裝置時發生錯誤:', oError);
alert('列舉裝置時發生錯誤: ' + oError.message);
}
}
function funcDisplayDevices(oDevices, strElementId)
{
const oContainer=document.getElementById(strElementId);
oContainer.innerHTML='';
if(oDevices.length === 0)
{
oContainer.innerHTML='<div class="classDevice">未找到裝置</div>';
return;
}
oDevices.forEach(
oDevice =>
{
const oDeviceElement=document.createElement('div');
oDeviceElement.className='classDevice';
const strDeviceId=oDevice.deviceId || '無ID';
const strDeviceLabel=oDevice.label || '未命名裝置';
oDeviceElement.innerHTML
='<strong>名稱:</strong> '+strDeviceLabel+'<br>'
+'<strong>ID:</strong> '+strDeviceId+'<br>'
+'<strong>類型:</strong> '+oDevice.kind+'';
oContainer.appendChild(oDeviceElement);
}
);
}
</script>
</body>
</html>
用 python 解析 qrcode
# sudo apt install python3-pyzbar
# sudo apt install python3-pillow
from pyzbar.pyzbar import decode
from PIL import Image
oImg=Image.open('13.jpg')
oResults=decode(oImg)
for oResult in oResults:
print("QR Code data:", oResult.data.decode("utf-8"))
用 python 來產生 odt 檔案
# pip install odfpy
from odf.opendocument import OpenDocumentText
from odf.table import Table, TableRow, TableCell, TableColumn
from odf.style import Style, PageLayout, PageLayoutProperties, MasterPage
from odf.style import Footer, FooterStyle, ParagraphProperties
from odf.style import TableProperties, TableColumnProperties, TableCellProperties
from odf.style import TextProperties, FontFace
from odf.text import P, Span, PageNumber, PageCount
oDoc=OpenDocumentText()
# 字型
oFontFace=FontFace(name="標楷體", fontfamily="標楷體")
oDoc.fontfacedecls.addElement(oFontFace)
# 版面
oPageLayout=PageLayout(name="pagelayout20250424154200")
oPageLayoutProperties=PageLayoutProperties(
margin="1cm", # 留白 1cm
pagewidth="29.7cm", # A4 橫向
pageheight="21cm", # A4 橫向
printorientation="landscape", # 橫向
writingmode="tb-rl" # 直書
)
oPageLayout.addElement(oPageLayoutProperties)
oDoc.automaticstyles.addElement(oPageLayout)
# 頁尾
oFooterStyle=Style(name="footerstyle20250424140000", family="paragraph")
oFooterStyle.addElement(ParagraphProperties(textalign="center"))
oFooterStyle.addElement(TextProperties(fontnameasian="標楷體",fontsizeasian="12pt",fontname="Consolas",fontsize="12pt"))
oDoc.styles.addElement(oFooterStyle)
oFooter=Footer()
oFotterParagraph=P(stylename=oFooterStyle)
oFotterParagraph.addText("2025-04-24 大乘起信論勝異方便")
oFotterParagraph.addText(" ")
oFotterParagraph.addElement(PageNumber(selectpage="current"))
oFotterParagraph.addText(" / ")
oFotterParagraph.addElement(PageCount())
oFooter.addElement(oFotterParagraph)
# 版面+頁尾
oMasterPage=MasterPage(name="Standard", pagelayoutname=oPageLayout)
oMasterPage.addElement(oFooter)
oDoc.masterstyles.addElement(oMasterPage)
# 表格與欄位樣式
oTableStyle=Style(name="tablestyle20250424154200", family="table")
oTableStyle.addElement(TableProperties(width="18cm", align="left"))
oColumnStyle = Style(name="columnstyle20250424154300", family="table-column")
oColumnStyle.addElement(TableColumnProperties(columnwidth="18cm"))
oCellStyleAZero=Style(name="cellstyle20250424163630", family="table-cell")
oCellStyleAZero.addElement(TableCellProperties(backgroundcolor="#D6D5F0", border="0.002cm solid #000000",padding="0.1cm"))
oCellStyleA=Style(name="cellstyle20250424163600", family="table-cell")
oCellStyleA.addElement(TableCellProperties(backgroundcolor="#D6D5F0", borderleft="0.002cm solid #000000",borderright="0.002cm solid #000000",borderbottom="0.002cm solid #000000",padding="0.1cm"))
oCellStyleB=Style(name="cellstyle20250424220700", family="table-cell")
oCellStyleB.addElement(TableCellProperties(backgroundcolor="#ffffff", borderleft="0.002cm solid #000000",borderright="0.002cm solid #000000",borderbottom="0.002cm solid #000000",padding="0.1cm"))
oCellStyleC=Style(name="cellstyle20250424220730", family="table-cell")
oCellStyleC.addElement(TableCellProperties(backgroundcolor="#CDEFF7", borderleft="0.002cm solid #000000",borderright="0.002cm solid #000000",borderbottom="0.002cm solid #000000",padding="0.1cm"))
oCellStyleD=Style(name="cellstyle20250424220800", family="table-cell")
oCellStyleD.addElement(TableCellProperties(backgroundcolor="#ffffff", borderleft="0.002cm solid #000000",borderright="0.002cm solid #000000",borderbottom="0.002cm solid #000000",padding="0.1cm"))
oTextStyleA=Style(name="textstyle20250424165600", family="paragraph")
oTextStyleA.addElement(TextProperties(fontnameasian="標楷體",fontsizeasian="12pt",fontweightasian="bold",fontname="標楷體",fontsize="12pt",fontweight="bold"))
oTextStyleB=Style(name="textstyle20250425085700", family="paragraph")
oTextStyleB.addElement(TextProperties(fontnameasian="標楷體",fontsizeasian="24pt",fontweightasian="bold",fontname="標楷體",fontsize="24pt",fontweight="bold"))
oTextStyleC=Style(name="textstyle20250425085900", family="paragraph")
oTextStyleC.addElement(TextProperties(fontnameasian="標楷體",fontsizeasian="12pt",fontweightasian="bold",fontname="標楷體",fontsize="12pt",fontweight="bold"))
oTextStyleD1=Style(name="textstyle20250425090800", family="paragraph")
oTextStyleD1.addElement(TextProperties(fontnameasian="標楷體",fontsizeasian="16pt",fontname="標楷體",fontsize="16pt"))
oTextStyleD1.addElement(ParagraphProperties(backgroundcolor="#dddddd"))
oTextStyleD2=Style(name="textstyle20250425090830", family="paragraph")
oTextStyleD2.addElement(TextProperties(fontnameasian="標楷體",fontsizeasian="12pt",fontname="標楷體",fontsize="12pt"))
oTextStyleD2.addElement(ParagraphProperties(backgroundcolor="#ffffff"))
oDoc.automaticstyles.addElement(oTableStyle)
oDoc.automaticstyles.addElement(oColumnStyle)
oDoc.automaticstyles.addElement(oCellStyleAZero)
oDoc.automaticstyles.addElement(oCellStyleA)
oDoc.automaticstyles.addElement(oCellStyleB)
oDoc.automaticstyles.addElement(oCellStyleC)
oDoc.automaticstyles.addElement(oCellStyleD)
oDoc.automaticstyles.addElement(oTextStyleA)
oDoc.automaticstyles.addElement(oTextStyleB)
oDoc.automaticstyles.addElement(oTextStyleC)
oDoc.automaticstyles.addElement(oTextStyleD1)
oDoc.automaticstyles.addElement(oTextStyleD2)
# 先加入一行空白行
oDoc.text.addElement(P(text=""))
# 表格
oTable = Table(name="table20250424141000", stylename=oTableStyle)
oTable.addElement(TableColumn(stylename=oColumnStyle))
oDoc.text.addElement(oTable)
# 第一格
oRow=TableRow()
oCell=TableCell(stylename=oCellStyleAZero)
oCell.addElement(P(stylename=oTextStyleA, text="大乘起信論勝異方便"))
oRow.addElement(oCell)
oTable.addElement(oRow)
# 第二格
oRow=TableRow()
oCell=TableCell(stylename=oCellStyleB)
oCell.addElement(P(stylename=oTextStyleB, text="[開場白]"))
oRow.addElement(oCell)
oTable.addElement(oRow)
# 第三格
oRow=TableRow()
oCell=TableCell(stylename=oCellStyleC)
oCell.addElement(P(stylename=oTextStyleC, text="講稿與辭典"))
oRow.addElement(oCell)
oTable.addElement(oRow)
# 第四格
oRow=TableRow()
oCell=TableCell(stylename=oCellStyleD)
oCell.addElement(P(stylename=oTextStyleD1, text="請看裂網疏「又識論云。此信心所。自性澄清。亦能淨餘心心所等。如水清珠。能清濁水。」我們先看這一段。"))
oCell.addElement(P(stylename=oTextStyleD2, text="【又】FROM【教育部重編國語辭典修訂本】\r\n[副]1.表示重複或反覆。如:「一天又一天」、「看了又看」。唐.白居易〈賦得古原草送別〉詩:「野火燒不盡,春風吹又生。」"))
oRow.addElement(oCell)
oTable.addElement(oRow)
# 最後加入一行空白行
oDoc.text.addElement(P(text=""))
# 儲存檔案
oDoc.save("20250424_02.odt")
用 python 來產生 word 檔案
# pip install python-docx
from docx import Document
from docx.shared import Pt
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.shared import Cm
from docx.enum.section import WD_ORIENT
from docx.enum.text import WD_ALIGN_PARAGRAPH
def set_table_border(table):
tbl = table._tbl
tblPr = tbl.tblPr
# 建立 <w:tblBorders> 元素
tblBorders = OxmlElement('w:tblBorders')
for border_name in ('top', 'left', 'bottom', 'right', 'insideH', 'insideV'):
border = OxmlElement(f'w:{border_name}')
border.set(qn('w:val'), 'single') # 實線 single
border.set(qn('w:sz'), '1') # 線條寬度,單位為 1/8 pt,1pt=>8,目前用1,等於就是1/8pt
border.set(qn('w:space'), '0') # 線與內容之間距離
border.set(qn('w:color'), '000000') # 黑色
tblBorders.append(border)
# 加入到 <w:tblPr> 中
tblPr.append(tblBorders)
def cm_to_twips(cm):
return int(cm * 567) # 1 cm ≈ 567 twips
def set_table_width(table, width_cm):
table.autofit = False
tbl = table._tbl
tblPr = tbl.tblPr
width_twips = cm_to_twips(width_cm)
tblW = tblPr.xpath('./w:tblW')
if tblW:
tblW[0].set(qn('w:w'), str(width_twips))
tblW[0].set(qn('w:type'), 'dxa')
else:
tblW = OxmlElement('w:tblW')
tblW.set(qn('w:w'), str(width_twips))
tblW.set(qn('w:type'), 'dxa')
tblPr.append(tblW)
def set_table_alignment(table, alignment='center'):
"""
alignment: 'left', 'center', or 'right'
"""
tbl = table._tbl
tblPr = tbl.tblPr
jc = OxmlElement('w:jc')
jc.set(qn('w:val'), alignment)
tblPr.append(jc)
# 插入欄位函式(頁碼、總頁數用)
def insert_field(run, field_code):
fldChar1 = OxmlElement('w:fldChar')
fldChar1.set(qn('w:fldCharType'), 'begin')
instrText = OxmlElement('w:instrText')
instrText.set(qn('xml:space'), 'preserve')
instrText.text = field_code
fldChar2 = OxmlElement('w:fldChar')
fldChar2.set(qn('w:fldCharType'), 'separate')
fldChar3 = OxmlElement('w:fldChar')
fldChar3.set(qn('w:fldCharType'), 'end')
r_element = run._r
r_element.append(fldChar1)
r_element.append(instrText)
r_element.append(fldChar2)
r_element.append(fldChar3)
def set_cell_padding(cell, top=0, left=0, bottom=0, right=0):
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
# 建立 <w:tcMar>(儲存格內距)元素
tcMar = OxmlElement('w:tcMar')
for name, value in (('top', top), ('left', left), ('bottom', bottom), ('right', right)):
mar = OxmlElement(f'w:{name}')
mar.set(qn('w:w'), str(value)) # TWIPS 單位:1 pt = 20,1 cm ≈ 567
mar.set(qn('w:type'), 'dxa')
tcMar.append(mar)
tcPr.append(tcMar)
def set_table_cell_margins(table, margin=0):
"""
設定整張表格的儲存格內距為指定值(預設 0)
"""
tbl = table._tbl
tblPr = tbl.tblPr
tblCellMar = OxmlElement('w:tblCellMar')
for side in ('top', 'start', 'bottom', 'end'):
elem = OxmlElement(f'w:{side}')
elem.set(qn('w:w'), str(margin))
elem.set(qn('w:type'), 'dxa') # dxa = 1/20 pt
tblCellMar.append(elem)
tblPr.append(tblCellMar)
def set_paragraph_background(paragraph, color_hex):
"""
設定段落背景色,例如 color_hex = "FFFF00"(黃色)
"""
p = paragraph._p # 取得底層 <w:p> 元素
pPr = p.get_or_add_pPr() # 取得段落屬性 <w:pPr>
shd = OxmlElement('w:shd') # 建立 <w:shd> 元素
shd.set(qn('w:val'), 'clear') # 填滿方式,clear 表示實心
shd.set(qn('w:color'), 'auto') # 字體顏色(不設定)
shd.set(qn('w:fill'), color_hex) # 背景填滿色
pPr.append(shd) # 加到段落屬性中
def set_cell_background(cell, color_hex):
"""
設定儲存格背景色,例如 color_hex = "FFFF00"(黃色)
"""
tc = cell._tc # 取得底層 <w:tc> 元素
tcPr = tc.get_or_add_tcPr() # 取得 <w:tcPr> 儲存格屬性元素
shd = OxmlElement('w:shd') # 建立 <w:shd> 元素
shd.set(qn('w:val'), 'clear') # 填滿方式
shd.set(qn('w:color'), 'auto') # 不指定字體顏色
shd.set(qn('w:fill'), color_hex) # 背景填滿色
tcPr.append(shd) # 加到儲存格屬性中
# https://python-docx.readthedocs.io/en/latest/
oDoc=Document()
oSection=oDoc.sections[0]
oSection.orientation = WD_ORIENT.PORTRAIT
oSection.page_width = Cm(21)
oSection.page_height = Cm(29.7)
oSection.top_margin = Cm(1)
oSection.bottom_margin = Cm(1)
oSection.left_margin = Cm(1)
oSection.right_margin = Cm(1)
oFooter=oSection.footer
oP=oFooter.paragraphs[0]
oP.alignment = WD_ALIGN_PARAGRAPH.CENTER
oP.text="2025-04-24 大乘起信論勝異方便 "
oRun=oP.add_run()
insert_field(oRun, 'PAGE')
oP.add_run(" / ")
oRun=oP.add_run()
insert_field(oRun, 'NUMPAGES')
oP=oDoc.add_paragraph('')
oP.paragraph_format.space_before = Pt(0)
oP.paragraph_format.space_after = Pt(0)
oTable=oDoc.add_table(rows=0, cols=1)
oTable.autofit=False
oTable.allow_autofit=False
set_table_alignment(oTable, 'center')
set_table_width(oTable, 18)
set_table_border(oTable)
oP=oDoc.add_paragraph('')
oP.paragraph_format.space_before = Pt(0)
oP.paragraph_format.space_after = Pt(0)
# 第一個儲存格
if 1==1:
oCell=oTable.add_row().cells[0]
oCell.width=Cm(18)
set_cell_padding(oCell)
set_cell_background(oCell, 'e7e1fa')
# oP=oCell.paragraphs[0]
# oP=oCell.add_paragraph()
oP=oCell.paragraphs[0]
oP.paragraph_format.space_before = Pt(0)
oP.paragraph_format.space_after = Pt(0)
oRun=oP.add_run('大乘起信論勝異方便')
oRun.font.name='標楷體'
oRun._element.rPr.rFonts.set(qn('w:eastAsia'), '標楷體')
oRun.font.size=Pt(12)
oRun.font.bold=True
# 第二個儲存格
if 1==1:
oCell=oTable.add_row().cells[0]
oCell.width=Cm(18)
set_cell_padding(oCell)
set_cell_background(oCell, 'ffffff')
# oP=oCell.paragraphs[0]
# oP=oCell.add_paragraph()
oP=oCell.paragraphs[0]
oP.paragraph_format.space_before = Pt(0)
oP.paragraph_format.space_after = Pt(0)
oRun=oP.add_run('[開場白]')
oRun.font.name='標楷體'
oRun._element.rPr.rFonts.set(qn('w:eastAsia'), '標楷體')
oRun.font.size=Pt(24)
oRun.font.bold=True
# 第三個儲存格
if 1==1:
oCell=oTable.add_row().cells[0]
oCell.width=Cm(18)
set_cell_padding(oCell)
set_cell_background(oCell, 'cbeef7')
# oP=oCell.paragraphs[0]
# oP=oCell.add_paragraph()
oP=oCell.paragraphs[0]
oP.paragraph_format.space_before = Pt(0)
oP.paragraph_format.space_after = Pt(0)
oRun=oP.add_run('講稿與辭典')
oRun.font.name='標楷體'
oRun._element.rPr.rFonts.set(qn('w:eastAsia'), '標楷體')
oRun.font.size=Pt(12)
oRun.font.bold=True
# 第四個儲存格
if 1==1:
oCell=oTable.add_row().cells[0]
oCell.width=Cm(18)
set_cell_padding(oCell)
set_cell_background(oCell, 'ffffff')
# oP=oCell.paragraphs[0]
# oP=oCell.add_paragraph()
oP=oCell.paragraphs[0]
set_paragraph_background(oP, 'dddddd')
oP.paragraph_format.space_before = Pt(0)
oP.paragraph_format.space_after = Pt(0)
oRun=oP.add_run("諸位老師學長早安,\r\n阿彌陀佛。")
oRun.font.name='標楷體'
oRun._element.rPr.rFonts.set(qn('w:eastAsia'), '標楷體')
oRun.font.size=Pt(16)
oRun.font.bold=False
# oP=oCell.paragraphs[0]
# oP=oCell.add_paragraph()
oP=oCell.add_paragraph()
oP.paragraph_format.space_before = Pt(0)
oP.paragraph_format.space_after = Pt(0)
oRun=oP.add_run("這裡是辭典文字......")
oRun.font.name='標楷體'
oRun._element.rPr.rFonts.set(qn('w:eastAsia'), '標楷體')
oRun.font.size=Pt(12)
oRun.font.bold=False
######set_table_cell_margins(oTable, 0)
#nmomtf
oDoc.save('20250424.docx')