python 操作word 修改页眉与页脚
Python 操作 修改 word 页脚 页眉
2023-09-11 14:20:48 时间
# -*- coding: utf-8 -*- # @Time : 2022年05月01日 19时59分 # @File : doctemp.py # @notice : from docxtpl import DocxTemplate data_dic = { 't1':'燕子', 't2':'杨柳', 't3':'桃花', 't4':'针尖', 't5':'头涔涔', 't6':'泪潸潸', 't7':'茫茫然', 't8':'伶伶俐俐', '报告日期':'2008-11-22', '报告编号':'A1154477', } # doc = DocxTemplate("mb1.docx") #加载模板文件 # doc.render(data_dic) #填充数据 # doc.save('模版1.docx') #保存目标文件 import docx # from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH #导入库:设置对象居中、对齐等 from docx.shared import Cm doc = docx.Document("系统生成报告.docx") #加载模板文件 # for item in doc.sections: # print(item.header) # print(item.start_type) #changing the page margins修改页边距 sections = doc.sections for section in sections: section.top_margin = Cm(3) section.bottom_margin = Cm(2) section.left_margin = Cm(2.54) section.right_margin = Cm(2.54) doc.settings.odd_and_even_pages_header_footer = True # 启动页眉页脚奇偶页不同 偶数页眉 = doc.sections[0].even_page_header # 对偶数页进行设置,奇数页直接对节进行页眉页脚设置即可。 偶数页眉.paragraphs[0].text = "这是一个偶数页眉" # 偶数页眉.paragraphs[1].text = "这是一个11偶数页眉" print('页',list(doc.sections[0])) doc.sections[0].different_first_page_header_footer = True # 启动页眉页脚首页不同 首页页眉 = doc.sections[0].first_page_header 首页页眉.paragraphs[0].text = "这是首页的页眉" print('页数',len(doc.sections)) header=doc.sections[0].header paragraph = header.paragraphs[0] paragraph.text = "Title of my document" print(doc.sections[0]) print(doc.settings) doc.save('22.docx')
相关文章
- 简明 Python 教程学习笔记_7_文件操作(os、shutil、pathlib )
- Python集合(set)类型的操作
- Python 操作 Kafka --- kafka-python
- Python脚本扫描给定网段的MAC地址表(scapy或 python-nmap)
- Python 统计进程池正在执行的进程个数(进程池进程个数)
- python操作txt文件中数据教程[2]-python提取txt文件
- Google Earth Engine——从Python中的经纬度坐标获取Modis正弦图块网格位置
- 0基础Python入门学习路线图应该是怎样的?
- python之进程和线程的对比
- python简单爬虫操作(requests、etree)
- 【python实战】拒绝黄牛!我要用python原价冲周董演唱会~Python自动秒杀抢票脚本
- 19 python - 列表的常见操作
- python os模块文件目录操作详解
- Python + Jmeter 实现自动化性能压测
- 【Python】操作txt文件
- 《Python密码学编程》——2.4 特色程序
- 《Python数据可视化编程实战》——1.7 安装requests模块
- python 控制鼠标操作
- python操作mysql数据库系列-操作MySql数据库(四)
- Python之文件读取、写入和异常处理操作举例
- python文档自动翻译
- Python学习---django下的cookie操作 180201
- python 试试pandas 读取excel功能
- Python fabric实现远程操作和部署
- Python操作MongoDB基本使用
- Python、Django、Mysql详细搭建开发,model操作数据库以及执行原生SQL
- Python删除文件夹中旧文件
- Python字符串操作
- python构建web界面实现MySQL数据库的操作