五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊

使用python解決中英混合參考文獻(xiàn)中et al 和等的問題

2023-06-28 18:15 作者:老張你去哪兒  | 我要投稿

這個(gè)代碼使用zipfile將docx進(jìn)行解壓,然后操作document.xml文件,找到中文中的et al之后替換為“等”,然后再壓縮為docx

import zipfile

import re

import os

import shutil

from lxml import etree


def replace_etal(filepath):

? ? temp_dir = 'temp_dir'

? ? temp_filename = os.path.join(temp_dir, 'word/document.xml')


? ? # Create a temporary directory and extract the docx file into it

? ? with zipfile.ZipFile(filepath, 'r') as docx:

? ? ? ? docx.extractall(temp_dir)


? ? # Parse the XML document

? ? with open(temp_filename, 'r', encoding='utf-8') as f:

? ? ? ? tree = etree.parse(f)


? ? root = tree.getroot()


? ? # Get the default namespace

? ? default_ns = re.match(r'\{.*\}', root.tag).group(0)[1:-1]? # We remove the {}?


? ? # Create a variable to store the text of the previous 't' element

? ? prev_text = ''


? ? # Iterate over every 't' element in the XML

? ? for element in root.findall('.//{{{}}}t'.format(default_ns)):

? ? ? ? print(element.text)

? ? ? ? # If the element text contains 'et al.' and the previous text contains Chinese characters, replace 'et al.' with '等'

? ? ? ? if element.text and 'et al.' in element.text and re.search(r'[\u4e00-\u9fa5]', prev_text):

? ? ? ? ? ? element.text = element.text.replace('et al.', '等.')


? ? ? ? # Update the previous text

? ? ? ? if element.text:

? ? ? ? ? ? prev_text = element.text

? ? ? ? else:

? ? ? ? ? ? prev_text = ''


? ? # Write the modified XML back to the temporary file

? ? with open(temp_filename, 'wb') as f:

? ? ? ? f.write(etree.tostring(root))


? ? # Create a new zip file with all contents of the temporary directory

? ? with zipfile.ZipFile( filepath, 'w') as docx:

? ? ? ? for folderName, subfolders, filenames in os.walk(temp_dir):

? ? ? ? ? ? for filename in filenames:

? ? ? ? ? ? ? ? # create complete filepath of file in directory

? ? ? ? ? ? ? ? filePath = os.path.join(folderName, filename)

? ? ? ? ? ? ? ? # Add file to zip

? ? ? ? ? ? ? ? docx.write(filePath, arcname=filePath.replace(temp_dir, ''))


? ? # Delete the temporary directory

? ? shutil.rmtree(temp_dir)


? ? return? filepath # 返回修改后的文件名


def openword(odocx):

? ? # 打開文檔

? ? app_path = "\"C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE\"" # Word應(yīng)用程序路徑,根據(jù)實(shí)際安裝路徑修改

? ? os.system(f'{app_path} {odocx}')


odocx = replace_etal('測試文檔.docx')

openword(odocx)


使用python解決中英混合參考文獻(xiàn)中et al 和等的問題的評(píng)論 (共 條)

分享到微博請遵守國家法律
翼城县| 习水县| 蚌埠市| 潼关县| 广州市| 信丰县| 芜湖县| 宿松县| 独山县| 巧家县| 安化县| 兴海县| 上杭县| 睢宁县| 周至县| 搜索| 乌审旗| 望城县| 元阳县| 三台县| 赤水市| 景谷| 锡林郭勒盟| 河曲县| 凤阳县| 额济纳旗| 化州市| 无锡市| 昭通市| 吉首市| 梁河县| 蒙阴县| 依兰县| 怀远县| 龙胜| 安岳县| 民和| 随州市| 承德市| 广州市| 洪洞县|