最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Write formatted rtf files to change the font but keep colors and bullet points - Stack Overflow

programmeradmin2浏览0评论

I have a series of rtf files that I want to adjust the formatting of. The files use a variety of fonts, text sizes, and formatting. I want to standardize them to use 12 pt bold Arial font with no italics and 0pt line spacing. I do want to keep the existing colors and bullet points though. The files have to be .rtf because they're used in a program that requires that format.

Most of my searches have pointed me to PyRTF3. However, I have been unable to find any kind of documentation on it. I'm not really sure how to approach it.

I've tried using striprtf, but the problem is that it removes the formatting like colors and bullet points, which I need to keep. I got close by combining it with this solution, but the files have a watermark and again, no colors or bullet points.

from striprtf.striprtf import rtf_to_text
import aspose.words as aw


rtfPath = "path/to/file.rtf"
rtfLines = []
with open(rtfPath, "r") as file:
    for line in file:
        rtfLines.append(rtf_to_text(line))

newdoc = aw.Document()
builder = aw.DocumentBuilder(newdoc)
font = builder.font
font.bold = True
font.name = "Arial"
font.size = 12

paragraphFormat = builder.paragraph_format
paragraphFormat.alignment = aw.ParagraphAlignment.JUSTIFY
paragraphFormat.keep_together = True
for line in rtfLines:
    builder.writeln(line.rstrip("\n"))
newdoc.save(rtfPath)
发布评论

评论列表(0)

  1. 暂无评论