您好,登錄后才能下訂單哦!
這篇文章主要介紹“python-docx如何使用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“python-docx如何使用”文章能幫助大家解決問題。
python-docx的簡單使用
''' 設置表格所有單元格的四個邊為0.5磅,黑色,實線 可以使用返回值,也可以不使用 ''' def 設置表格網格線為黑色實線(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 ''' 設置標題樣式 ''' def 設置標題樣式為黑色宋體(heading_object:object): heading_object.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.LEFT #左對齊 for run in heading_object.runs: run.font.name=u'宋體' #設置為宋體 #run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')#設置為宋體,和上邊的一起使用 run.font.color.rgb = docx.shared.RGBColor(0,0,0)#設置顏色為黑色 return heading_object ''' 創建docx文檔,將翻譯結果和原文寫入文檔中 ''' def word(): my_word_doc = docx.Document() #打開一個空白文檔 # for style in my_word_doc.styles: # print(style) heading = my_word_doc.add_heading(翻譯結果["NAME"],level=2) #指定樣式標題2 設置標題樣式為黑色宋體(heading) heading = my_word_doc.add_heading("描述",level=3) #指定樣式標題3 設置標題樣式為黑色宋體(heading) for line in 翻譯結果["SYNOPSIS"].split("\n"): my_word_doc.add_paragraph(line) for line in 翻譯結果["DESCRIPTION"].split("\n"): my_word_doc.add_paragraph(line) heading = my_word_doc.add_heading("參數",level=3) #指定樣式標題3 設置標題樣式為黑色宋體(heading) #table = my_word_doc.add_table(rows=len(翻譯結果["PARAMETERS"]), cols=3) #指定樣式標題3;在末尾添加一個表 table = my_word_doc.add_table(rows=len(翻譯結果["PARAMETERS"]), cols=2) #指定樣式標題3;在末尾添加一個表 #table.style = my_word_doc.styles['Medium Grid 1'] 設置表格網格線為黑色實線(table) index=0 for key,value in 翻譯結果["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 = 幫助文件解析結果["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 設置標題樣式為黑色宋體(heading) for key,value in 翻譯結果["Example"].items(): heading = my_word_doc.add_heading(key[0:-1],level=4) #指定樣式標題4 設置標題樣式為黑色宋體(heading) for line in value.split("\n"): my_word_doc.add_paragraph(line) my_word_doc.save(r"C:\Users\gyj\Downloads\temp.docx")
實際上我是拿來轉換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 #字典形式 幫助文件解析結果={"NAME":"","SYNOPSIS":"","DESCRIPTION":"","PARAMETERS":{},"Example":{}}; 翻譯結果={"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() 幫助文件解析結果['NAME'] = line elif(line.strip() == "SYNOPSIS"): line = fd_help.readline() while line[0:4]==" ": 幫助文件解析結果['SYNOPSIS'] += line.lstrip() line = fd_help.readline() elif(line.strip() == "DESCRIPTION"): line = fd_help.readline() while line[0:4]==" ": 幫助文件解析結果['DESCRIPTION'] += line.lstrip() line = fd_help.readline() elif(line.strip() == "PARAMETERS"): line = fd_help.readline() while line[0:4]==" ": 參數名 = line.lstrip() 幫助文件解析結果['PARAMETERS'][參數名] = "" line = fd_help.readline() while line[0:8]==" ": 幫助文件解析結果['PARAMETERS'][參數名] += 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() 幫助文件解析結果["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() 幫助文件解析結果["Example"][temp]=""; line = fd_help.readline() while not (len(re.findall(r'^\s*-*\s*Example',line))) and line: 幫助文件解析結果["Example"][temp]+=line.lstrip() line = fd_help.readline() else: line = fd_help.readline() #檢查key=""的情況,并刪除這樣的key if("" in 幫助文件解析結果): del 幫助文件解析結果[""] if("" in 幫助文件解析結果["PARAMETERS"]): del 幫助文件解析結果["PARAMETERS"][""] if("" in 幫助文件解析結果["Example"]): del 幫助文件解析結果["Example"][""] # 本文件是通過請求有道翻譯,去獲取翻譯結果 ''' i: 你好 from: AUTO to: AUTO smartresult: dict client: fanyideskweb salt: 16643765479061 //毫秒級別的時間戳后面加上個0-9之間的隨機數,js代碼:r + parseInt(10 * Math.random(), 10);這里的r表示時間戳字符串 sign: 1d69ce8f7c6258243e573e31e29e0012 //簽名,下面找到了 lts: 1664376547906 //毫秒級別的時間戳 bv: 42c8b36dd7d61c619e7b1dc11e44d870 //同設備相同,使用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是要翻譯的內容,i是毫秒級別的時間戳后面加上個0-9之間的隨機數;后面這串字符串估計是服務器那邊隨機生成的,應該會變化。每次抓取的時候,可以查看下js代碼 } }; 總體來說,data數據由函數“generateSaltSign”計算出來 */ ''' ''' 獲取翻譯結果 The_translated_string:被翻譯的字符串 由于翻譯是以行為單位,所以一行一個結果,函數將解析The_translated_string參數,并以字符串形式返回所有翻譯結果 ''' 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之間的隨機數 sign_str = "fanyideskweb" + The_translated_string + salt + "Ygy_4c=r#e#4EX^NUGUc5" # 構造簽名字符串 # 簽名,算法:sign: n.md5("fanyideskweb" + e + i + "Ygy_4c=r#e#4EX^NUGUc5") //在這里,后面這串字符串估計是服務器那邊隨機生成的,應該會變化。每次抓取的時候,可以查看下js代碼 sign = hashlib.md5(str.encode(sign_str)).hexdigest() # 同設備相同,使用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"]: #如果源字符串就是存在段落的,則這里就會根據其來分結果 for j in i: #翻譯服務器認為該分段的,這里就會再次存在子項 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'} ''' ''' 將變量“幫助文件解析結果”中的value翻譯成中文 ''' def 執行翻譯(): 翻譯結果 = copy.deepcopy(幫助文件解析結果) #這里執行了深拷貝了,所以它不再是外面的那個“翻譯結果”變量了 翻譯結果["SYNOPSIS"] = youdao_translate(翻譯結果["SYNOPSIS"]) 翻譯結果["DESCRIPTION"] = youdao_translate(翻譯結果["DESCRIPTION"]) for key,value in 翻譯結果["PARAMETERS"].items(): if(value == ""): continue 翻譯結果["PARAMETERS"][key] = youdao_translate(value) for key,value in 翻譯結果["Example"].items(): if(value == ""): continue 翻譯結果["Example"][key] = youdao_translate(value) temp_dict = {} for key,value in 翻譯結果["Example"].items(): if(key == ""): continue temp_dict[youdao_translate(key)] = value 翻譯結果["Example"] = temp_dict return 翻譯結果 ''' 設置表格所有單元格的四個邊為0.5磅,黑色,實線 可以使用返回值,也可以不使用 ''' def 設置表格網格線為黑色實線(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 ''' 設置標題樣式 ''' def 設置標題樣式為黑色宋體(heading_object:object): heading_object.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.LEFT #左對齊 for run in heading_object.runs: run.font.name=u'宋體' #設置為宋體 #run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')#設置為宋體,和上邊的一起使用 run.font.color.rgb = docx.shared.RGBColor(0,0,0)#設置顏色為黑色 return heading_object ''' 創建docx文檔,將翻譯結果和原文寫入文檔中 ''' def word(): my_word_doc = docx.Document() #打開一個空白文檔 # for style in my_word_doc.styles: # print(style) heading = my_word_doc.add_heading(翻譯結果["NAME"],level=2) #指定樣式標題2 設置標題樣式為黑色宋體(heading) heading = my_word_doc.add_heading("描述",level=3) #指定樣式標題3 設置標題樣式為黑色宋體(heading) for line in 翻譯結果["SYNOPSIS"].split("\n"): my_word_doc.add_paragraph(line) for line in 翻譯結果["DESCRIPTION"].split("\n"): my_word_doc.add_paragraph(line) heading = my_word_doc.add_heading("參數",level=3) #指定樣式標題3 設置標題樣式為黑色宋體(heading) #table = my_word_doc.add_table(rows=len(翻譯結果["PARAMETERS"]), cols=3) #指定樣式標題3;在末尾添加一個表 table = my_word_doc.add_table(rows=len(翻譯結果["PARAMETERS"]), cols=2) #指定樣式標題3;在末尾添加一個表 #table.style = my_word_doc.styles['Medium Grid 1'] 設置表格網格線為黑色實線(table) index=0 for key,value in 翻譯結果["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 = 幫助文件解析結果["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 設置標題樣式為黑色宋體(heading) for key,value in 翻譯結果["Example"].items(): heading = my_word_doc.add_heading(key[0:-1],level=4) #指定樣式標題4 設置標題樣式為黑色宋體(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") #有道翻譯的新版翻譯結果解析實在是太變態了,我搞不了,是在不行就把格式弄好,使用文檔翻譯翻譯吧。 #翻譯結果 = 執行翻譯() 翻譯結果=幫助文件解析結果 word()
關于“python-docx如何使用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。