[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
from docx import Document from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Cm,Pt from docx.oxml.ns import qn document =
Document('2.docx') #读取文档的第一个表格数据 tables = document.tables[0]
#在第该表格3行1列的单元格内输入“能几日又中秋?as12A” run =
tables.cell(2,0).paragraphs[0].add_run(u'能几日又中秋?as12A') #输入时默认华文琥珀字体
run.font.name = '华文琥珀' #输入字体大小默认30号 run.font.size = Pt(30) r = run._element
#将输入语句中的中文部分字体变为华文行楷 r.rPr.rFonts.set(qn('w:eastAsia'), '华文行楷') #居中
tables.cell(1,0).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER #保存
document.save('3.docx')
要点:
1、在同一个 单元格内连续使用
table.cell.paragraphs[0].add_run('XXX'),则默认为在单元格中附加文字,而非在该单元格内重建。
2、如果没有这两句,则无法改变单元格内的中文字体:
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), '黑体')
3、run.font.name = '黑体'
上述语句只能修改ascii 代码所包含的字符, 中文不可显示。
综合演示:
代码:
from docx import Document from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Cm,Pt from docx.oxml.ns import qn document =
Document('2.docx') tables = document.tables[0] run =
tables.cell(1,0).paragraphs[0].add_run(u'二十年重过南楼hellow,233') run.font.name =
'仿宋' run.font.size = Pt(18) r = run._element r.rPr.rFonts.set(qn('w:eastAsia'),
'仿宋') tables.cell(1,0).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run = tables.cell(1,0).paragraphs[0].add_run(u'柳下系船犹未稳as12A') run.font.name =
'黑体' run.font.size = Pt(35) r = run._element r.rPr.rFonts.set(qn('w:eastAsia'),
'黑体') run = tables.cell(2,0).paragraphs[0].add_run(u'能几日又中秋?as12A')
run.font.name = '华文琥珀' run.font.size = Pt(30) r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), '华文行楷') document.save('3.docx')
运行后结果: