備忘錄_20160105(定位) 修改 回首頁

程式 2025-04-25 15:53:15 1745567595 100
用 python 來產生 odt 檔案

用 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")