這篇文章主要介紹“python-docx如何使用”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“python-docx如何使用”文章能幫助大家解決問題。
公司專注于為企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設(shè)、微信公眾號開發(fā)、商城網(wǎng)站建設(shè),小程序開發(fā),軟件按需網(wǎng)站建設(shè)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗,我們會仔細了解各客戶的需求而做出多方面的分析、設(shè)計、整合,為客戶設(shè)計出具風格及創(chuàng)意性的商業(yè)解決方案,成都創(chuàng)新互聯(lián)更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。
python-docx的簡單使用
''' 設(shè)置表格所有單元格的四個邊為0.5磅,黑色,實線 可以使用返回值,也可以不使用 ''' def 設(shè)置表格網(wǎng)格線為黑色實線(table_object:object): kwargs = { "top":{"sz": 4, "val": "single", "color": "#000000"}, "bottom":{"sz": 4, "val": "single", "color": "#000000"}, "left":{"sz": 4, "val": "single", "color": "#000000"}, "right":{"sz": 4, "val": "single", "color": "#000000"}, "insideV":{"sz": 4, "val": "single", "color": "#000000"}, "insideH":{"sz": 4, "val": "single", "color": "#000000"} } borders = docx.oxml.OxmlElement('w:tblBorders') for tag in ('bottom', 'top', 'left', 'right', 'insideV', 'insideH'): edge_data = kwargs.get(tag) if edge_data: any_border = docx.oxml.OxmlElement(f'w:{tag}') for key in ["sz", "val", "color", "space", "shadow"]: if key in edge_data: any_border.set(docx.oxml.ns.qn(f'w:{key}'), str(edge_data[key])) borders.append(any_border) table_object._tbl.tblPr.append(borders) return table_object ''' 設(shè)置標題樣式 ''' def 設(shè)置標題樣式為黑色宋體(heading_object:object): heading_object.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.LEFT #左對齊 for run in heading_object.runs: run.font.name=u'宋體' #設(shè)置為宋體 #run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')#設(shè)置為宋體,和上邊的一起使用 run.font.color.rgb = docx.shared.RGBColor(0,0,0)#設(shè)置顏色為黑色 return heading_object ''' 創(chuàng)建docx文檔,將翻譯結(jié)果和原文寫入文檔中 ''' def word(): my_word_doc = docx.Document() #打開一個空白文檔 # for style in my_word_doc.styles: # print(style) heading = my_word_doc.add_heading(翻譯結(jié)果["NAME"],level=2) #指定樣式標題2 設(shè)置標題樣式為黑色宋體(heading) heading = my_word_doc.add_heading("描述",level=3) #指定樣式標題3 設(shè)置標題樣式為黑色宋體(heading) for line in 翻譯結(jié)果["SYNOPSIS"].split("\n"): my_word_doc.add_paragraph(line) for line in 翻譯結(jié)果["DESCRIPTION"].split("\n"): my_word_doc.add_paragraph(line) heading = my_word_doc.add_heading("參數(shù)",level=3) #指定樣式標題3 設(shè)置標題樣式為黑色宋體(heading) #table = my_word_doc.add_table(rows=len(翻譯結(jié)果["PARAMETERS"]), cols=3) #指定樣式標題3;在末尾添加一個表 table = my_word_doc.add_table(rows=len(翻譯結(jié)果["PARAMETERS"]), cols=2) #指定樣式標題3;在末尾添加一個表 #table.style = my_word_doc.styles['Medium Grid 1'] 設(shè)置表格網(wǎng)格線為黑色實線(table) index=0 for key,value in 翻譯結(jié)果["PARAMETERS"].items(): for line in key.split("\n"): cell = table.cell(index,0) cell.text += line for line in value.split("\n"): table.cell(index,1).text += line #table.cell(index,1).text = 幫助文件解析結(jié)果["PARAMETERS"][key] cell_paragraphs = table.cell(index,0).paragraphs for i in cell_paragraphs: i.alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT #左對齊 cell_paragraphs = table.cell(index,1).paragraphs for i in cell_paragraphs: i.alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT #左對齊 # table.cell(index,2).text = value # cell_paragraphs = table.cell(index,2).paragraphs # for i in cell_paragraphs: # i.alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT #左對齊 index += 1 heading = my_word_doc.add_heading("示例",level=3) #指定樣式標題3 設(shè)置標題樣式為黑色宋體(heading) for key,value in 翻譯結(jié)果["Example"].items(): heading = my_word_doc.add_heading(key[0:-1],level=4) #指定樣式標題4 設(shè)置標題樣式為黑色宋體(heading) for line in value.split("\n"): my_word_doc.add_paragraph(line) my_word_doc.save(r"C:\Users\gyj\Downloads\temp.docx")
實際上我是拿來轉(zhuǎn)換PowerShell cmdlet命令的幫助txt文件為word文檔的。其中帶了翻譯。簡單記錄下。
import docx #pip install python-docx import re import json import requests import time import hashlib from urllib.parse import urlencode import random import copy #字典形式 幫助文件解析結(jié)果={"NAME":"","SYNOPSIS":"","DESCRIPTION":"","PARAMETERS":{},"Example":{}}; 翻譯結(jié)果={"NAME":"","SYNOPSIS":"","DESCRIPTION":"","PARAMETERS":{},"Example":{}}; ''' ''' def 解析PowerShell命令的幫助文本(文件路徑:str): with open(文件路徑,mode='r',encoding='utf-8') as fd_help: line = fd_help.readline(); while line: if(line.strip() == "NAME"): line = fd_help.readline() line = line.strip() 幫助文件解析結(jié)果['NAME'] = line elif(line.strip() == "SYNOPSIS"): line = fd_help.readline() while line[0:4]==" ": 幫助文件解析結(jié)果['SYNOPSIS'] += line.lstrip() line = fd_help.readline() elif(line.strip() == "DESCRIPTION"): line = fd_help.readline() while line[0:4]==" ": 幫助文件解析結(jié)果['DESCRIPTION'] += line.lstrip() line = fd_help.readline() elif(line.strip() == "PARAMETERS"): line = fd_help.readline() while line[0:4]==" ": 參數(shù)名 = line.lstrip() 幫助文件解析結(jié)果['PARAMETERS'][參數(shù)名] = "" line = fd_help.readline() while line[0:8]==" ": 幫助文件解析結(jié)果['PARAMETERS'][參數(shù)名] += line.lstrip() line = fd_help.readline() if(len(re.findall(r'^\s*-*\s*Example',line))): break elif(len(re.findall(r'^\s*-*\s*Example',line))): temp=re.sub(r"^\s*-*\s*([\w,\.: ]+) *-*",r"\1",line) temp.rstrip() 幫助文件解析結(jié)果["Example"][temp]=""; line = fd_help.readline() while line: if(len(re.findall(r'^\s*-*\s*Example',line))): temp=re.sub(r"^\s*-*\s*([\w,\.: ]+) *-*",r"\1",line) temp.rstrip() 幫助文件解析結(jié)果["Example"][temp]=""; line = fd_help.readline() while not (len(re.findall(r'^\s*-*\s*Example',line))) and line: 幫助文件解析結(jié)果["Example"][temp]+=line.lstrip() line = fd_help.readline() else: line = fd_help.readline() #檢查key=""的情況,并刪除這樣的key if("" in 幫助文件解析結(jié)果): del 幫助文件解析結(jié)果[""] if("" in 幫助文件解析結(jié)果["PARAMETERS"]): del 幫助文件解析結(jié)果["PARAMETERS"][""] if("" in 幫助文件解析結(jié)果["Example"]): del 幫助文件解析結(jié)果["Example"][""] # 本文件是通過請求有道翻譯,去獲取翻譯結(jié)果 ''' i: 你好 from: AUTO to: AUTO smartresult: dict client: fanyideskweb salt: 16643765479061 //毫秒級別的時間戳后面加上個0-9之間的隨機數(shù),js代碼:r + parseInt(10 * Math.random(), 10);這里的r表示時間戳字符串 sign: 1d69ce8f7c6258243e573e31e29e0012 //簽名,下面找到了 lts: 1664376547906 //毫秒級別的時間戳 bv: 42c8b36dd7d61c619e7b1dc11e44d870 //同設(shè)備相同,使用md5加密的(方法是:md5(User-Agent)==>md5("5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53") doctype: json version: 2.1 keyfrom: fanyi.web action: FY_BY_REALTlME /* 解密出來了:sign的計算如下 define("newweb/common/service", ["./utils", "./md5", "./jquery-1.7"], function(e, t) { var n = e("./jquery-1.7"); e("./utils"); e("./md5"); var r = function(e) { var t = n.md5(navigator.appVersion) , r = "" + (new Date).getTime() , i = r + parseInt(10 * Math.random(), 10); return { ts: r, bv: t, salt: i, sign: n.md5("fanyideskweb" + e + i + "Ygy_4c=r#e#4EX^NUGUc5") //在這里,e是要翻譯的內(nèi)容,i是毫秒級別的時間戳后面加上個0-9之間的隨機數(shù);后面這串字符串估計是服務(wù)器那邊隨機生成的,應(yīng)該會變化。每次抓取的時候,可以查看下js代碼 } }; 總體來說,data數(shù)據(jù)由函數(shù)“generateSaltSign”計算出來 */ ''' ''' 獲取翻譯結(jié)果 The_translated_string:被翻譯的字符串 由于翻譯是以行為單位,所以一行一個結(jié)果,函數(shù)將解析The_translated_string參數(shù),并以字符串形式返回所有翻譯結(jié)果 ''' def youdao_translate(The_translated_string:str): if(The_translated_string == ""): return {"":""} url = r'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' User_Agent = "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37" header = { "Accept": "application/json, text/javascript, */*; q=0.01", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6", "Connection": "keep-alive", #"Content-Length": "307", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Cookie":"OUTFOX_SEARCH_USER_ID=1135160796@10.108.162.134; OUTFOX_SEARCH_USER_ID_NCOO=775555146.507473; JSESSIONID=aaaQ2GYK5N-ozb24rKNcy; SESSION_FROM_COOKIE=unknown; DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; JSESSIONID=abcPzon0RcZqc7GltuAgy; ___rl__test__cookies=1665366515354", "Host": "fanyi.youdao.com", "Origin": "https://fanyi.youdao.com", "Referer": "https://fanyi.youdao.com/", "sec-ch-ua": """\"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"\"""", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "Windows", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "same-origin", "User-Agent": User_Agent, "X-Requested-With": "XMLHttpRequest" } #The_translated_string=input("輸入你要翻譯的中文信息:\n") # 被翻譯的字符串 timestamp = str(round(time.time()*1000)) # 毫秒級別的時間戳 salt = timestamp + str(random.randint(0, 9)) # 毫秒級別的時間戳后面加上個0-9之間的隨機數(shù) sign_str = "fanyideskweb" + The_translated_string + salt + "Ygy_4c=r#e#4EX^NUGUc5" # 構(gòu)造簽名字符串 # 簽名,算法:sign: n.md5("fanyideskweb" + e + i + "Ygy_4c=r#e#4EX^NUGUc5") //在這里,后面這串字符串估計是服務(wù)器那邊隨機生成的,應(yīng)該會變化。每次抓取的時候,可以查看下js代碼 sign = hashlib.md5(str.encode(sign_str)).hexdigest() # 同設(shè)備相同,使用md5加密的(方法是:md5(User-Agent)==>md5("5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53") bv = hashlib.md5(str.encode(User_Agent)).hexdigest() cookies = { "OUTFOX_SEARCH_USER_ID": "1135160796@10.108.162.134", "OUTFOX_SEARCH_USER_ID_NCOO": "775555146.507473", "JSESSIONID": "aaaQ2GYK5N-ozb24rKNcy", "SESSION_FROM_COOKIE": "unknown", "DICT_UGC": "be3af0da19b5c5e6aa4e17bd8d90b28a|", "JSESSIONID": "abcPzon0RcZqc7GltuAgy", "___rl__test__cookies": "1665366515354" } data = { "i": The_translated_string, "from": "AUTO", "to": "AUTO", # 一般來說,是從中文翻譯為英文 # "from": "zh-CHS", # "to": "en", "smartresult": "dict", "client": "fanyideskweb", "salt": salt, "sign": sign, "lts": timestamp, "bv": bv, "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web", "action": "FY_BY_CLICKBUTTION" } data = urlencode(data); result = requests.post(url, data=data, cookies=cookies,headers=header) json_result = json.loads(result.text); #lines = The_translated_string.splitlines(); #按照行邊界拆分 if(not json_result["errorCode"]): ret_list=""; for i in json_result["translateResult"]: #如果源字符串就是存在段落的,則這里就會根據(jù)其來分結(jié)果 for j in i: #翻譯服務(wù)器認為該分段的,這里就會再次存在子項 ret_list+=j['tgt'] ret_list+="\n" #ret_list[json_result["translateResult"][i][0]["src"]]=json_result["translateResult"][i][0]["tgt"] return ret_list; else: return "errorCode = "+str(json_result["errorCode"]); #示例 ''' str_ = "你好\n世界\n我來了\n哈哈" print(youdao_translate(str_)) -----out----- {'你好': 'hello', '世界': 'The world', '我來了': "I'm coming", '哈哈': 'Ha ha'} ''' ''' 將變量“幫助文件解析結(jié)果”中的value翻譯成中文 ''' def 執(zhí)行翻譯(): 翻譯結(jié)果 = copy.deepcopy(幫助文件解析結(jié)果) #這里執(zhí)行了深拷貝了,所以它不再是外面的那個“翻譯結(jié)果”變量了 翻譯結(jié)果["SYNOPSIS"] = youdao_translate(翻譯結(jié)果["SYNOPSIS"]) 翻譯結(jié)果["DESCRIPTION"] = youdao_translate(翻譯結(jié)果["DESCRIPTION"]) for key,value in 翻譯結(jié)果["PARAMETERS"].items(): if(value == ""): continue 翻譯結(jié)果["PARAMETERS"][key] = youdao_translate(value) for key,value in 翻譯結(jié)果["Example"].items(): if(value == ""): continue 翻譯結(jié)果["Example"][key] = youdao_translate(value) temp_dict = {} for key,value in 翻譯結(jié)果["Example"].items(): if(key == ""): continue temp_dict[youdao_translate(key)] = value 翻譯結(jié)果["Example"] = temp_dict return 翻譯結(jié)果 ''' 設(shè)置表格所有單元格的四個邊為0.5磅,黑色,實線 可以使用返回值,也可以不使用 ''' def 設(shè)置表格網(wǎng)格線為黑色實線(table_object:object): kwargs = { "top":{"sz": 4, "val": "single", "color": "#000000"}, "bottom":{"sz": 4, "val": "single", "color": "#000000"}, "left":{"sz": 4, "val": "single", "color": "#000000"}, "right":{"sz": 4, "val": "single", "color": "#000000"}, "insideV":{"sz": 4, "val": "single", "color": "#000000"}, "insideH":{"sz": 4, "val": "single", "color": "#000000"} } borders = docx.oxml.OxmlElement('w:tblBorders') for tag in ('bottom', 'top', 'left', 'right', 'insideV', 'insideH'): edge_data = kwargs.get(tag) if edge_data: any_border = docx.oxml.OxmlElement(f'w:{tag}') for key in ["sz", "val", "color", "space", "shadow"]: if key in edge_data: any_border.set(docx.oxml.ns.qn(f'w:{key}'), str(edge_data[key])) borders.append(any_border) table_object._tbl.tblPr.append(borders) return table_object ''' 設(shè)置標題樣式 ''' def 設(shè)置標題樣式為黑色宋體(heading_object:object): heading_object.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.LEFT #左對齊 for run in heading_object.runs: run.font.name=u'宋體' #設(shè)置為宋體 #run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')#設(shè)置為宋體,和上邊的一起使用 run.font.color.rgb = docx.shared.RGBColor(0,0,0)#設(shè)置顏色為黑色 return heading_object ''' 創(chuàng)建docx文檔,將翻譯結(jié)果和原文寫入文檔中 ''' def word(): my_word_doc = docx.Document() #打開一個空白文檔 # for style in my_word_doc.styles: # print(style) heading = my_word_doc.add_heading(翻譯結(jié)果["NAME"],level=2) #指定樣式標題2 設(shè)置標題樣式為黑色宋體(heading) heading = my_word_doc.add_heading("描述",level=3) #指定樣式標題3 設(shè)置標題樣式為黑色宋體(heading) for line in 翻譯結(jié)果["SYNOPSIS"].split("\n"): my_word_doc.add_paragraph(line) for line in 翻譯結(jié)果["DESCRIPTION"].split("\n"): my_word_doc.add_paragraph(line) heading = my_word_doc.add_heading("參數(shù)",level=3) #指定樣式標題3 設(shè)置標題樣式為黑色宋體(heading) #table = my_word_doc.add_table(rows=len(翻譯結(jié)果["PARAMETERS"]), cols=3) #指定樣式標題3;在末尾添加一個表 table = my_word_doc.add_table(rows=len(翻譯結(jié)果["PARAMETERS"]), cols=2) #指定樣式標題3;在末尾添加一個表 #table.style = my_word_doc.styles['Medium Grid 1'] 設(shè)置表格網(wǎng)格線為黑色實線(table) index=0 for key,value in 翻譯結(jié)果["PARAMETERS"].items(): for line in key.split("\n"): cell = table.cell(index,0) cell.text += line for line in value.split("\n"): table.cell(index,1).text += line #table.cell(index,1).text = 幫助文件解析結(jié)果["PARAMETERS"][key] cell_paragraphs = table.cell(index,0).paragraphs for i in cell_paragraphs: i.alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT #左對齊 cell_paragraphs = table.cell(index,1).paragraphs for i in cell_paragraphs: i.alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT #左對齊 # table.cell(index,2).text = value # cell_paragraphs = table.cell(index,2).paragraphs # for i in cell_paragraphs: # i.alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT #左對齊 index += 1 heading = my_word_doc.add_heading("示例",level=3) #指定樣式標題3 設(shè)置標題樣式為黑色宋體(heading) for key,value in 翻譯結(jié)果["Example"].items(): heading = my_word_doc.add_heading(key[0:-1],level=4) #指定樣式標題4 設(shè)置標題樣式為黑色宋體(heading) for line in value.split("\n"): my_word_doc.add_paragraph(line) my_word_doc.save(r"C:\Users\xxx\Downloads\temp.docx") 解析PowerShell命令的幫助文本(r"C:\Users\xxx\Downloads\Get-CimClass.txt") #有道翻譯的新版翻譯結(jié)果解析實在是太變態(tài)了,我搞不了,是在不行就把格式弄好,使用文檔翻譯翻譯吧。 #翻譯結(jié)果 = 執(zhí)行翻譯() 翻譯結(jié)果=幫助文件解析結(jié)果 word()
關(guān)于“python-docx如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
文章題目:python-docx如何使用
網(wǎng)頁地址:http://www.rwnh.cn/article32/pcohsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、建站公司、網(wǎng)站收錄、全網(wǎng)營銷推廣、網(wǎng)站維護、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)