Docx-Python更改文本不透明度

时间:2018-07-16 05:04:37

标签: python docx

我要替换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')

1 个答案:

答案 0 :(得分:0)

我找到的解决方案是将文本保存为运行而不是段落。由于某些原因,它可以保持您保存在原始文档中的透明度/不透明度。