首页 > 解决方案 > Docx-Python 更改文本不透明度

问题描述

我正在替换 docx 文件中的文本,该文件最初的文本不透明度为 25%,但是当我执行脚本时,已更改段落的不透明度恢复为 0%。有没有办法在不透明度中进行编程,类似于字体大小、斜体等?我在 docx-python 文档中没有找到任何内容。

编辑:这是代码的 docx 部分。

styles = d.styles
style = styles.add_style('Main Text', WD_STYLE_TYPE.PARAGRAPH)
mainStyle = d.styles['Main Text']
fontMain = mainStyle.font
fontMain.name = 'Montserrat Light'
fontMain.size = Pt(12)

styles = d.styles
style = styles.add_style('Main Run Text', WD_STYLE_TYPE.CHARACTER)
mainRunStyle = d.styles['Main Run Text']
fontRunMain = mainRunStyle.font
fontRunMain.name = 'Montserrat Light'
fontRunMain.size = Pt(12)

currentDate = datetime.date.today()
currentDateFormatted = currentDate.strftime("%B %d, %Y")
d.paragraphs[10].text = currentDateFormatted

d.paragraphs[12].text = 'Jimmy Newtron' #replace with argument
pName = d.paragraphs[12]
pName.style = d.styles['Main Text']

d.paragraphs[13].text = 'Lead Recruiter' #replace with argument
pTitle = d.paragraphs[13]
pTitle.style = d.styles['Main Text']

d.paragraphs[14].text = 'Mega Corp' #replace with argument
pCompany = d.paragraphs[14]
pCompany.style = d.styles['Main Text']

d.paragraphs[15].text = 'New York City, New York, United States' #replace with argument
pLocation = d.paragraphs[15]
pLocation.style = d.styles['Main Text']

d.paragraphs[17].runs[2].text = 'Jimmy Newtron' #replace with argument
rDear = d.paragraphs[17].runs[2]
rDear.style = d.styles['Main Run Text']

d.paragraphs[18].runs[2].text = 'Mega Corp' #replace with argument
rCompany = d.paragraphs[18].runs[2]
rCompany.style = d.styles['Main Run Text']

d.paragraphs[21].runs[3].text = 'Mega Corp' #replace with argument
rCompanyTwo = d.paragraphs[21].runs[3]
rCompanyTwo = d.styles['Main Run Text']

d.save('c:\\users\\sonorityscape\\desktop\\test.docx')

标签: pythondocx

解决方案


我找到的一个解决方案是将文本保存为运行而不是段落。出于某种原因,它保留了您保存在原始文档中的透明度/不透明度。


推荐阅读