python自动化办公task3

from docx import Document
 
# 源文件目录
self.word_path = './output.docx'
 
# 打开文档,构建一个文档对象
self.doc = Document(self.word_path)

通过章节对象的 left_margin、top_margin、right_margin、bottom_margin 属性值可以获取当前章节的左边距、上边距、右边距、下边距

def get_page_margin(p):
    """
    获取某个页面的页边距(EMU)
    :param p:
    :return:
    """
    # 分别对应:左边距、上边距、右边距、下边距
    left, top, right, bottom = p.left_margin, p.top_margin, p.right_margin, p.bottom_margin
    return left, top, right, bottom
 
# 2、页边距信息
first_p = msg_ps[0]
left, top, right, bottom = get_page_margin(first_p)
print('左边距:', left, ",上边距:", top, ",右边距:", right, ",下边距:", bottom)

页眉边距:header_distance

页脚边距:footer_distance

def get_header_footer_distance(p):
    """
    获取页眉、页脚边距
    :param p:
    :return:
    """
    # 分别对应页眉边距、页脚边距
    header_distance, footer_distance = p.header_distance, p.footer_distance
    return header_distance, footer_distance
 
# 3、页眉页脚边距
header_distance, footer_distance = get_header_footer_distance(first_p)
print('页眉边距:', header_distance, ",页脚边距:", footer_distance)

页面宽度:page_width

页面高度:page_height

def get_page_size(p):
    """
    获取页面宽度、高度
    :param p:
    :return:
    """
    # 分别对应页面宽度、高度
    page_width, page_height = p.page_width, p.page_height
    return page_width, page_height
 
# 4、页面宽度、高度
page_width, page_height = get_page_size(first_p)
print('页面宽度:', page_width, ",页面高度:", page_height)

页面方向分为:横向和纵向

使用章节对象的 orientation 属性去获取一个章节的页面方向

def get_page_orientation(p):
    """
    获取页面方向
    :param p:
    :return:
    """
    return p.orientation
 
# 5、页面方向
# 类型:class 'docx.enum.base.EnumValue
# 包含:PORTRAIT (0)、LANDSCAPE (1)
page_orientation = get_page_orientation(first_p)
print("页面方向:", page_orientation)
from docx.enum.p import WD_ORIENT
 
# 设置页面方向(横向、竖向)
# 设置为横向
first_p.orientation = WD_ORIENT.LANDSCAPE
# 设置为竖向
# first_p.orientation = WD_ORIENT.PORTRAIT
self.doc.save(self.word_path)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值