問題1:python-docx指定頁面設置成橫向?
python-docx包的add_section()可以添加一個新的節,section.orientation、section.page_width、section.page_height屬性可以改變節紙張的方向。關於這些屬性和方法的詳細用法以及頁面設置的內容可以參考文章python-docx頁面設置,python-docx節的添加、定位和分節符的設置。
那么,我們采用第一部分的步驟嘗試使用python實現改變部分頁面的紙張方向,代碼如下:
from docx import Document from docx.enum.section import WD_ORIENTATION, WD_SECTION_START # 導入節方向和分解符類型
document = Document() # 新建docx文檔
document.add_paragraph() # 添加一個空白段落
section = document.add_section(start_type=WD_SECTION_START.CONTINUOUS) # 添加橫向頁的連續節
section.orientation = WD_ORIENTATION.LANDSCAPE # 設置橫向
page_h, page_w = section.page_width, section.page_height section.page_width = page_w # 設置橫向紙的寬度
section.page_height = page_h # 設置橫向紙的高度
document.add_paragraph() # 添加第二個空白段落
section = document.add_section(start_type=WD_SECTION_START.CONTINUOUS) # 添加連續的節
section.orientation = WD_ORIENTATION.PORTRAIT # 設置縱向
page_h, page_w = section.page_width, section.page_height # 讀取插入節的高和寬
section.page_width = page_w # 設置縱向紙的寬度
section.page_height = page_h # 設置縱向紙的高度
document.save('test.docx')
引用:https://baijiahao.baidu.com/s?id=1665028380582245274
問題2:python給word文檔添加標題
mport docx doc=docx.Document() #整數 0 表示標題是 Title 樣式,這用於文檔的頂部。整數 1 到 45是不同的標題層次,是主要的標題, 45是最低層的子標題
doc.add_heading('標題0',0) doc.add_heading('標題1',1) doc.add_heading('標題2',2) doc.add_heading('標題3',3) doc.add_heading('標題4',4) doc.add_heading('標題5',5) doc.save('example3.docx')
引用:https://www.cnblogs.com/shunguo/p/11399305.html
問題3:word插入日期,格式為年月日,且居中顯示
# doc = Document() #新建word文件
import time today=time.strftime("%Y-%m-%d",time.localtime()) date_para=doc.add_paragraph() # date_run=date_para.add_run(today) #2020-08-13 # print(today[0:5])
date_run=date_para.add_run("%s年%s月%s日"%(today[0:4],today[6:7],today[8:11])) date_run.font.name=u"宋體" date_run.font.size=Pt(16) date_para.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER #日期居中
doc.add_page_break() #插入分頁符
問題4:word插入表格,表格里面插入圖片,圖片下面顯示照片名稱且居中顯示
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT #用作設置段落對齊 WD_ALIGN_PARAGRAPH
from docx.shared import Pt #磅數
from docx.oxml.ns import qn #中文格式
import time,os,sys,datetime
import docx #導入docx庫
from docx.shared import Inches #圖片尺寸
from docx.enum.table import WD_TABLE_ALIGNMENT #表格內容居中
# doc = Document("./照片管理模板.docx") #打開word文件
doc = Document() #新建word文件
doc.add_heading('標題0',0) # myDoucment.add_heading(fn.strip('-0.jpg'), level=1) # 插入圖片名稱,作為一級標題生成目錄
table=doc.add_table(rows=4,cols=3, style="Normal Table") # table=doc.add_table(rows=4,cols=3, style="Table Grid") # table.cell(0,0).add.picture("1.JPG") #設置表格內容字體和大小 # table.style.font.name=u"楷體" # table.style.font.size = 20 ######################## #讀取照片信息
# table.cell(1,0).text='第1張照片'
run = table.cell(1, 0).paragraphs[0].add_run("第1張照片") run.font.name = u'黑體' run.font.size = Pt(12) table.cell(1,1).text='第2張照片' table.cell(1,2).text='第3張照片' table.cell(3,0).text='第4張照片' table.cell(3,1).text='第5張照片' table.cell(3,2).text='第6張照片'
w = float(8 / 2.54) # cm轉為英寸
h = float(6 / 2.54) # cm轉為英寸
run = doc.tables[0].cell(0, 0).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(0, 1).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(0, 2).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(2, 0).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(2, 1).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(2, 2).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) #設置表格內容居中
for r in range(4):#循環將每一行,每一列都設置為居中
for c in range(3):
table.cell(r, c).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
#插入分頁符 doc.add_page_break()