from docx import Document
doc = Document()
tabs = [
[“姓名”, “學號”, “成績”],
[“儲蓄保險王”, 101, 99],
[“正義真話俠”, 102, 98],
[“SavingKing”, 103, 95],
[“@wvr5039s”, 104, 90]
]
table = doc.add_table(rows=5, cols=3)
# ========================================================================
# for r in range(5):
# cells = table.rows[r].cells
# for c in range(3):
# cells[c].text = str(tabs[r][c])
# ========================================================================
for r in range(5):
for c in range(3):
table.cell(r,c).text = str(tabs[r][c])
doc.save(“savingking.docx”)
#原程式無誤,少最後一個row
#是因為使用wordPad開啟的關係
#用微軟的Word或免費的LibreOffice開,
#就不會少最後一個row
輸出檔案:
少了最後一個row
改用LibreOffice開啟即正常:
新增一行:
table.add_row()
#少最後一個row
#是wordPad的關係,程式碼無關
輸出:
最後一個row有跑出來
只是多了四個空白row
多這一行:
table.add_row()
且不屬於for迴圈之下
表格即正常:
from docx import Document
from docx.shared import Cm #或 Inches
doc = Document()
doc.add_heading(“SavingKing’s Title”,0) #數字越大,字越小
p = doc.add_paragraph(“A plain paragraph having some “)
p.add_run(“bold”).bold = True
p.add_run(” and some “) #字串前後都有一個空格
p.add_run(“italic”).italic = True
# Run物件:以相同樣式連續鄰接的文字。
#當文字的樣式有變化時,就需要一個新的Run物件。
doc.add_picture(\
r”C:\Python\RF Stable\Fig\Phase Stable.png”,width = Cm(10) )
doc.save(“savingking_2.docx”)
繼前面已經成功插入表格
如何在表格中插入圖檔?
from docx import Document
from docx.shared import Cm #Inch #Pt
doc = Document()
tabs = [
[“姓名”, “學號”, “成績”],
[“儲蓄保險王”, 101, 99],
[“正義真話俠”, 102, 98],
[“SavingKing”, 103, 95],
[“@wvr5039s”, 104, 90]
]
table = doc.add_table(rows=5, cols=3)
for r in range(0, 5):
# table.add_row()
for c in range(0, 3):
#print(“(%d,%d)” %(r,c),end=” “)
table.cell(r, c).text = str(tabs[r][c])
table.add_row()
figpath = r”C:\22056 EPP IL\Fig\Air vs DUT.png”
run = table.cell(5,0).paragraphs[0].add_run()
#.add_run(“想要寫的文字”)
pic = run.add_picture(figpath)
pic.height = Cm(4)
pic.width = Cm(3)
doc.save(r”C:\Python\DocxCsv\SavingKing.docx”)
SavingKing.docx
調整欄寬:
from docx import Document
from docx.shared import Cm #Inch #Pt
doc = Document()
table = doc.add_table(rows=3, cols=2)
col0 = table.columns[0]
col1 = table.columns[1]
col0.width = Cm(2)
col1.width = Cm(13)
run0 = table.cell(0,0).paragraphs[0].add_run(“item 1”)
figpath = r”C:\22056 EPP IL\Fig\Air vs DUT.png”
run = table.cell(0,1).paragraphs[0].add_run(“item 1:\n”)
pic = run.add_picture(figpath)
pic.height = Cm(12)
pic.width = Cm(12)
doc.save(r”C:\Python\DocxCsv\SavingKing.docx”)
SavingKing.docx:
統整:
| 物件 | 方法 | 備註 |
| doc = Document() | .add_heading(“Title”,0) | 數字越大,字越小 |
| | .add_paragraph(“A plain paragraph.”) | 加入段落 |
| | .add_picture( ”path” ,width = Cm( 10)) | 加入圖片 |
| | .save(”path”) | 存檔 |
| pg = doc.add_paragraph() | pg.add_run(”bold text”) = True | 粗體字 |
| | pg.add_run(”plain text”) | |
| | pg.add_run( ”italic text” ) = True | 斜體字 |
| table=doc.add_table(rows=5,cols=3) | table.cell(r,c).text = str | |
| | table.add_row() | |
| 以上已可插入表格,段落(粗斜體),圖片 | 如何在儲存格中插入圖片? | |
| run = table.cell(5,0).paragraph[0].add_run() | | |
| pic = run.add_picture(”path”) | .height = Cm(8) | |
| | .width = Cm(12) | |
doc = Document()
def addPicsInTable(pathList, title, doc):
“””pathList長度6,就表示有6張圖,
建立一個7列的table(首列放標題title)
“””
pg = doc.add_paragraph()
pg.add_run(title).bold = True
table = doc.add_table(rows= len(pathList)+1, cols=1)
col0 = table.columns[0]
col0.width = Cm(13)
table.cell(0,0).paragraphs[0].add_run(title).bold = True
for r in range(0, len(pathList) ):
path = pathList[r]
#if (r+1)%2 ==0 : doc.add_paragraph()
str1 = “item “+ str(r+1) +”:\n”
table.cell(r+1,0).paragraphs[0].add_run(str1).bold = True
run = table.cell(r+1,0).paragraphs[0].add_run()
pic = run.add_picture(path)
pic.height = Cm(8)
pic.width = Cm(12)
#if (r+1)%2 ==0 : doc.add_page_break()
#插入換頁符號,沒辦法斷開表格
doc.add_paragraph(“\n next”) #插入新的段落,不會覆蓋前面的資料
return None