python docx的超链接网址和链接文本操作
作者:追梦小狂魔 时间:2021-06-05 22:10:55
我就废话不多说了,大家还是直接看代码吧~
from docx import Document
from docx import RT
import re
d=Document("./liu2.docx")
for p in d.paragraphs:
rels = d.part.rels
for rel in rels:
if rels[rel].reltype == RT.HYPERLINK:
print("\n 超链接文本为", rels[rel], " 超链接网址为: ", rels[rel]._target)
补充:Python输出“test.docx“文档正文中的所有红色的文字、输出文档中所有的超链接地址和文本
一、题目:
1、查阅资料了解.docx文档结构,然后编写程序,输出"test.docx"文档正文中的所有红色的文字。
2、查阅资料了解.docx文档结构,然后查阅资料,编写程序,输出"测试.docx"文档中所有的超链接地址和文本。
3、已知文件“超市营业额1.xlsx”中记录了某超市2019年3月1日至5日各员工在不同时段、不同柜台的销售额。部分数据如图,要求编写程序,读取该文件的数据,并统计每个员工的销售总额、每个时段的销售总额、每个柜台的销售总额。
超市营业额1.xlsx文件图
二、代码展示:
# -*- coding: utf-8 -*-
""""
@Author : Jackma
@Time : 2020/11/7 21:26
@File : 2020_11_7.py
@Software: PyCharm
@URL : www.jackmark.top
@Version :
"""
# 1、查阅资料了解.docx文档结构,然后编写程序,输出"test.docx"文档正文中的所有红色的文字。
# 2、查阅资料了解.docx文档结构,然后查阅资料,编写程序,输出"测试.docx"文档中所有的超链接地址和文本。
# 3、已知文件“超市营业额1.xlsx”中记录了某超市2019年3月1日至5日各员工在不同时段、不同柜台的销售额。
# 部分数据如图,要求编写程序,读取该文件的数据,并统计每个员工的销售总额、每个时段的销售总额、每个
# 柜台的销售总额。
# 4、查阅资料,编写程序操作Excel文件。已知当前文件夹中的文件“每个人的爱好.xlsx”的内容如图中A到H列所
# 示,要求追加一列,并如图中方框所示进行汇总。
from docx import Document
from docx.shared import RGBColor
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from openpyxl import load_workbook
# 1
def find_bold_red():
'''
输出文档中的所有红色的、加粗的文字
:return:
'''
# 定义两个列表
boldText = [] # 存储加粗的文字
redText = [] # 存储红色字体的文字
name1 = input('输入你要查询的文件名(without .docx):')
# doc1 = Document('test.docx') # 打开文档
doc1 = Document(name1 + '.docx') # 打开文档
for p in doc1.paragraphs: # 遍历里面的每个段落
for r in p.runs: # 找每段中所有的run, run指连续的相同格式的字体
if r.bold: # 找到加粗字体
boldText.append(r.text) # 把run的文本放到boldText文本中
if r.font.color.rgb == RGBColor(255,0,0): # rgb(255,0,0)代表红色,找到红色字体
redText.append(r.text)
result = {'red text': redText,
'bold text': boldText,
'both': set(redText) & set(boldText) # 集合的交集
}
# 输出结果
for title in result.keys():
print(title.center(30, '=')) # 长度为30,center指居中,效果如下
# ===========red text============
for text in result[title]:
print(text)
find_bold_red()
# 2
# def find_Hyperlink():
# '''
# 只适用于WPS创建的文档
# 输出"test.docx"文档中所有的超链接地址和文本
# :return:
# '''
# doc2 = Document('test.docx')
# for p in doc2.paragraphs:
# for index, run in enumerate(p.runs):
# if run.style.name == 'Hyperlink':
# print(run.text, end =':')
# for child in p.runs[index-2].element.getchildren():
# text = child.text
# if text and text.stratswith('HYPERLINK'):
# print(text[12:-2])
#
# find_Hyperlink()
def find_Hyperlink():
'''
输出"test.docx"文档中所有的超链接地址和文本
:return:
'''
docx_file=input(" 输入你要查询的文件名(without .docx): ")
document = Document(docx_file + ".docx")
rels = document.part.rels
for rel in rels:
if rels[rel].reltype == RT.HYPERLINK:
# print("\n 超链接文本为", rels[rel], " 超链接网址为: ", rels[rel]._target)
print(" 超链接网址为: ", rels[rel]._target)
find_Hyperlink()
# 3
def money():
'''
统计每个员工的销售总额、每个时段的销售总额、每个柜台的销售总额。
:return:
'''
# 3个字典分别存储按员工、按时段、按柜台的销售总额
persons = dict()
periods = dict()
goods = dict()
ws = load_workbook('超市营业额1.xlsx').worksheets[0]
for index, row in enumerate(ws.rows):
# 跳过第一行的表头
if index == 0:
continue
# 获取每行的相关信息
_, name, _, time, num, good = map(lambda cell: cell.value, row)
# 根据每行的值更新三个字典
persons[name] = persons.get(name, 0) + num
periods[time] = periods.get(time, 0) + num
goods[good] = goods.get(good, 0) + num
print(persons)
print(periods)
print(goods)
money()
三、结果展示:
首先是测试文档test.docx内容
图1 test.docx文件图
程序1、
输出"test.docx"文档正文中的所有红色的文字。
程序2、
输出"test.docx"文档中所有的超链接地址和文本。
程序3、
统计每个员工的销售总额、每个时段的销售总额、每个柜台的销售总额。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_15557299/article/details/112094458
标签:python,docx,链接,文本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
对python中使用requests模块参数编码的不同处理方法
2022-11-02 01:30:29
![](https://img.aspxhome.com/file/2023/8/103688_0s.jpg)
详解python中的三种命令行模块(sys.argv,argparse,click)
2021-04-24 12:57:10
完美处理python与anaconda环境变量的冲突问题
2023-01-11 07:37:31
![](https://img.aspxhome.com/file/2023/1/97051_0s.png)
python中子类与父类的关系基础知识点
2022-11-26 23:45:12
python神经网络Keras GhostNet模型的实现
2022-05-16 17:19:42
![](https://img.aspxhome.com/file/2023/9/93679_0s.png)
Python Process多进程实现过程
2021-04-30 02:21:45
![](https://img.aspxhome.com/file/2023/4/85384_0s.png)
python能在浏览器能运行吗
2023-05-11 14:32:07
Selenium chrome配置代理Python版的方法
2022-02-24 14:26:08
Python利用matplotlib.pyplot.boxplot()绘制箱型图实例代码
2022-11-18 05:08:26
![](https://img.aspxhome.com/file/2023/3/80403_0s.png)
对numpy中轴与维度的理解
2022-03-20 11:58:33
![](https://img.aspxhome.com/file/2023/7/120257_0s.jpg)
django静态文件加载的方法
2022-12-26 13:57:56
深入分析Python中Lambda函数的用法
2023-07-01 16:03:21
Python pandas找出、删除重复的数据实例
2023-07-05 11:24:46
python实现批量获取指定文件夹下的所有文件的厂商信息
2021-12-14 20:42:27
asp如何检测HTTP表头信息?
2010-06-28 18:20:00
浅谈视觉设计的准确性
2007-09-18 17:59:00
![](https://img.aspxhome.com/file/uploadpic/20079/18/20079181814327.gif)
Python编程实现简单的微博自动点赞
2021-05-14 06:43:12
![](https://img.aspxhome.com/file/2023/4/128204_0s.jpg)
详解python3 + Scrapy爬虫学习之创建项目
2022-07-22 18:02:05
![](https://img.aspxhome.com/file/2023/2/109592_0s.png)
《写给大家看的设计书》阅读笔记之对齐原则
2009-07-09 16:32:00
![](https://img.aspxhome.com/file/UploadPic/20097/9/dq-25s.jpg)
从错误中学习改正Go语言五个坏习惯提高编程技巧
2023-10-12 20:06:33