python中如何设置字体样式以及页边距?

首先定义一个类格式,每个不同的id代表了不同的引用样式。

# fdocx 类定义
class fdocx:
    def __init__(self):
        # 定义不同内容对应的字体和段落格式
        self.formats = [
            {'id': 1, 'font': '黑体', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 391160, 'style': 'Normal'},
            {'id': 2, 'font': '楷体_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 391160, 'style': 'Normal'},
            {'id': 3, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 391160, 'style': 'Normal'},
            {'id': 4, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},
            {'id': 5, 'font': '方正小标宋_GBK', 'pt': 22, 'alignment': WD_PARAGRAPH_ALIGNMENT.CENTER, 'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},
            {'id': 6, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 391160, 'style': 'Normal'},   #左对齐963930  :这个号码指的是对齐的样式,是空格几个
            {'id': 7, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.RIGHT, 'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},   #右对齐
            {'id': 8, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.CENTER,'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},
            {'id': 9, 'font': 'Times New Roman', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.CENTER,'color': RGBColor(0, 0, 0), 'first_line_indent': 0, 'style': 'Normal'},
            {'id': 10, 'font': '仿宋_GB2312', 'pt': 16, 'alignment': WD_PARAGRAPH_ALIGNMENT.LEFT, 'color': RGBColor(0, 0, 0),'first_line_indent': self.chars_to_twips(2), 'style': 'Normal'},
            #二级标题(一)
        ]

    @staticmethod
    def chars_to_twips(char_count):
        # 估算字符到Twips的转换。这里假设每个字符大约相当于0.5英寸(即720Twips)。
        # 这个值可能需要根据实际使用的字体和字号进行调整。
        return char_count * 720

    # 对段落和运行格式化的函数
    def formatit(self, paragraph, format_id):
        jsondata = self.getformatjson(format_id)
        # 设置段落格式
        paragraph.style = jsondata['style']
        paragraph.paragraph_format.alignment = jsondata['alignment']
        paragraph.paragraph_format.first_line_indent = jsondata['first_line_indent']
        # 设置字体格式
        for run in paragraph.runs:
            run.font.name = jsondata['font']
            run.font.size = Pt(jsondata['pt'])
            r = run._element.rPr.rFonts
            r.set(qn("w:eastAsia"), jsondata['font'])
            run.font.color.rgb = jsondata['color']




    # 获取格式化数据的函数
    def getformatjson(self, format_id):
        for w in self.formats:
            if w['id'] == format_id:
                return w
        return {}

当定义完了类之后,就是引用

           # 创建Word文档
            doc = docx.Document()



            sections = doc.sections
            section = sections[0]
            section.left_margin = 1 * 972000   ## 设置页边距, 1 * 914400   设置1英寸页边距也就是2.54cm,1 * 1332000 =3.7cm。972000=2.7cm;900000= 2.5;1080000 =3cm
            section.right_margin = 1 * 900000
            section.top_margin = 1 * 1332000
            section.bottom_margin = 1 * 1080000

            # 设置默认字体和字号
            doc.styles['Normal'].font.name = '方正小标宋简体'
            doc.styles['Normal'].font.size = Pt(16)
            total_manyilv_rounded = round(total_manyilv, 1)


            # 添加标题
            title = doc.add_heading("这里是标题内容", level=0)
            fdoc = fdocx()
            fdoc.formatit(title, 5)  # 假设标题使用格式5

            # 添加累计情况
            累计情况 = doc.add_heading("一、累计情况", level=1)
            fdoc.formatit(累计情况, 1)  # 假设累计情况使用格式3
            # 累计情况段落下方添加新内容
            content1 = f"你要输入的内容。"
            new_paragraph = doc.add_paragraph(content1)
            fdoc.formatit(new_paragraph, 6)  # 应用格式ID为6的样式
            # 在new_content下方再添加一段内容
            content2 = f"你要输入的内容。"
            additional_paragraph = doc.add_paragraph(content2)
            fdoc.formatit(additional_paragraph, 6)  # 应用格式ID为7的样式,如果需要不同的格式



            output_path3 = output_directory + f"/这里是你的路径"
            doc.save(output_path3 + ".docx")
            self.textBrowser.append(f"内容")

以上是生成一个word并设置其页边距, 设置页边距, 1 * 914400 设置1英寸页边距也就是2.54cm,1 * 1332000 =3.7cm。972000=2.7cm;900000= 2.5;1080000 =3cm。大家可以根据要求换算。
下面链接是当时参考的,也很详细,可以搭配看https://blog.csdn.net/smdbs2000/article/details/134554518?spm=1001.2014.3001.5506

Python Dash应用,如果你想要在外联样式表(external stylesheet)设置面的打印样式,特别是在处理跨打印上,你可以使用CSS的`@page`规则。这个规则允许你控制每个面的布局、边距、方向等属性。 例如,为了设置打印,你可以在CSS文件添加类似这样的代码: ```css /* CSS 文件 */ @page { size: landscape; /* 设置纸张方向为横向 */ margin: 0mm; /* 调整边距,防止内容超出边框 */ /* 可以设置每一的开始位置,如每从顶部开始 */ /* page-break-after: always; */ /* 或者使用媒体查询针对不同的设备大小调整 */ @media print { /* 添加其他打印特定的样式 */ font-size: smaller; /* 缩放字体以适应打印 */ color: black; /* 如果需要的话改变默认的黑白模式 */ } } body { /* 其他通用样式... */ } ``` 然后,在你的Dash应用,通过HTML标签引用这个外部CSS文件: ```python import dash from dash import html app = dash.Dash(__name__) server = app.server # 加载外部CSS文件 app.css.append_css({ 'external_url': 'path/to/your/styles.css' }) app.layout = html.Body( ..., id='content', # 这里可以给id='content'添加额外的CSS类以应用上面的@page规则 ) if __name__ == '__main__': app.run_server(debug=True) ``` 请注意,对于某些复杂的打印需求,可能还需要配合JavaScript的print事件或者使用更专业的库,比如`html2canvas`或`Dash-renderer`的`to_html_string()`函数结合`pdfkit`等工具进行转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值