国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python docx的超鏈接網(wǎng)址和鏈接文本操作

瀏覽:4日期:2022-06-26 10:20:07

我就廢話不多說了,大家還是直接看代碼吧~

from docx import Documentfrom docx import RTimport red=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], ' 超鏈接網(wǎng)址為: ', rels[rel]._target)

補(bǔ)充:Python輸出“test.docx“文檔正文中的所有紅色的文字、輸出文檔中所有的超鏈接地址和文本

一、題目:

1、查閱資料了解.docx文檔結(jié)構(gòu),然后編寫程序,輸出'test.docx'文檔正文中的所有紅色的文字。

2、查閱資料了解.docx文檔結(jié)構(gòu),然后查閱資料,編寫程序,輸出'測(cè)試.docx'文檔中所有的超鏈接地址和文本。

3、已知文件“超市營(yíng)業(yè)額1.xlsx”中記錄了某超市2019年3月1日至5日各員工在不同時(shí)段、不同柜臺(tái)的銷售額。部分?jǐn)?shù)據(jù)如圖,要求編寫程序,讀取該文件的數(shù)據(jù),并統(tǒng)計(jì)每個(gè)員工的銷售總額、每個(gè)時(shí)段的銷售總額、每個(gè)柜臺(tái)的銷售總額。

python docx的超鏈接網(wǎng)址和鏈接文本操作

超市營(yíng)業(yè)額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文檔結(jié)構(gòu),然后編寫程序,輸出'test.docx'文檔正文中的所有紅色的文字。# 2、查閱資料了解.docx文檔結(jié)構(gòu),然后查閱資料,編寫程序,輸出'測(cè)試.docx'文檔中所有的超鏈接地址和文本。# 3、已知文件“超市營(yíng)業(yè)額1.xlsx”中記錄了某超市2019年3月1日至5日各員工在不同時(shí)段、不同柜臺(tái)的銷售額。# 部分?jǐn)?shù)據(jù)如圖,要求編寫程序,讀取該文件的數(shù)據(jù),并統(tǒng)計(jì)每個(gè)員工的銷售總額、每個(gè)時(shí)段的銷售總額、每個(gè)# 柜臺(tái)的銷售總額。# 4、查閱資料,編寫程序操作Excel文件。已知當(dāng)前文件夾中的文件“每個(gè)人的愛好.xlsx”的內(nèi)容如圖中A到H列所# 示,要求追加一列,并如圖中方框所示進(jìn)行匯總。 from docx import Documentfrom docx.shared import RGBColor from docx.opc.constants import RELATIONSHIP_TYPE as RTfrom openpyxl import load_workbook # 1def find_bold_red(): ’’’ 輸出文檔中的所有紅色的、加粗的文字 :return: ’’’ # 定義兩個(gè)列表 boldText = [] # 存儲(chǔ)加粗的文字 redText = [] # 存儲(chǔ)紅色字體的文字 name1 = input(’輸入你要查詢的文件名(without .docx):’) # doc1 = Document(’test.docx’) # 打開文檔 doc1 = Document(name1 + ’.docx’) # 打開文檔 for p in doc1.paragraphs: # 遍歷里面的每個(gè)段落 for r in p.runs: # 找每段中所有的run, run指連續(xù)的相同格式的字體 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) # 集合的交集 } # 輸出結(jié)果 for title in result.keys(): print(title.center(30, ’=’)) # 長(zhǎng)度為30,center指居中,效果如下 # ===========red text============ for text in result[title]: print(text) find_bold_red() # 2# def find_Hyperlink():# ’’’# 只適用于WPS創(chuàng)建的文檔# 輸出'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], ' 超鏈接網(wǎng)址為: ', rels[rel]._target) print(' 超鏈接網(wǎng)址為: ', rels[rel]._target) find_Hyperlink() # 3def money(): ’’’ 統(tǒng)計(jì)每個(gè)員工的銷售總額、每個(gè)時(shí)段的銷售總額、每個(gè)柜臺(tái)的銷售總額。 :return: ’’’ # 3個(gè)字典分別存儲(chǔ)按員工、按時(shí)段、按柜臺(tái)的銷售總額 persons = dict() periods = dict() goods = dict() ws = load_workbook(’超市營(yíng)業(yè)額1.xlsx’).worksheets[0] for index, row in enumerate(ws.rows): # 跳過第一行的表頭 if index == 0: continue # 獲取每行的相關(guān)信息 _, name, _, time, num, good = map(lambda cell: cell.value, row) # 根據(jù)每行的值更新三個(gè)字典 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()三、結(jié)果展示:

首先是測(cè)試文檔test.docx內(nèi)容

python docx的超鏈接網(wǎng)址和鏈接文本操作

圖1 test.docx文件圖

程序1、

輸出'test.docx'文檔正文中的所有紅色的文字。

python docx的超鏈接網(wǎng)址和鏈接文本操作

程序2、

輸出'test.docx'文檔中所有的超鏈接地址和文本。

python docx的超鏈接網(wǎng)址和鏈接文本操作

程序3、

統(tǒng)計(jì)每個(gè)員工的銷售總額、每個(gè)時(shí)段的銷售總額、每個(gè)柜臺(tái)的銷售總額。

python docx的超鏈接網(wǎng)址和鏈接文本操作

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 万安县| 嘉峪关市| 东明县| 文安县| 陆丰市| 潜山县| 根河市| 开封市| 高要市| 瑞昌市| 延津县| 雷波县| 东山县| 东安县| 上高县| 介休市| 桦甸市| 鹿泉市| 康保县| 蚌埠市| 二连浩特市| 德钦县| 沅江市| 定兴县| 金乡县| 舟山市| 高密市| 姜堰市| 麦盖提县| 萨嘎县| 建阳市| 沅陵县| 开阳县| 昌江| 张家港市| 宁晋县| 浮梁县| 林芝县| 桂东县| 巢湖市| 公主岭市|