当前位置: 首页 > wzjs >正文

上海网站建设 迈若宁波seo整体优化

上海网站建设 迈若,宁波seo整体优化,一级a做爰片免网站,wordpress 页面模板这段代码通过提取、查询、替换DOI,生成参考文献列表来处理Word文档,可按功能模块划分: 导入模块 import re from docx import Document from docx.oxml.ns import qn from habanero import Crossref导入正则表达式模块re用于文本模式匹配&a…

这段代码通过提取、查询、替换DOI,生成参考文献列表来处理Word文档,可按功能模块划分:

  1. 导入模块
import re
from docx import Document
from docx.oxml.ns import qn
from habanero import Crossref

导入正则表达式模块re用于文本模式匹配,python - docx库中的Document类操作Word文档,qn函数处理命名空间(代码中未实际使用),以及habanero库的Crossref类,用于通过DOI查询参考文献信息。
2. 提取DOI函数

def extract_dois(text):doi_pattern = r'(10\.\d{4,9}/[-._;()/:A-Z0-9]+)'return re.findall(doi_pattern, text, re.IGNORECASE)

定义extract_dois函数,接收文本参数text,使用正则表达式doi_pattern匹配DOI格式,通过re.findall函数提取所有符合格式的DOI字符串,返回包含这些DOI的列表,忽略大小写。
3. 获取参考文献函数

def get_reference(doi):cr = Crossref()try:result = cr.works(ids=doi)if'message' in result:message = result['message']# 提取作者信息authors = []if 'author' in message:for author in message['author']:if 'family' in author and 'given' in author:last_name = author['family']first_initial = author['given'][0] if author['given'] else ''authors.append(f"{last_name}, {first_initial}.")author_str = ', '.join(authors)# 提取年份、标题等其他信息year = message['issued']['date - parts'][0][0] if 'issued' in message and 'date - parts' in message['issued'] and message['issued']['date - parts'] else 'n.d.'title = message['title'][0] if 'title' in message and message['title'] else 'No title'journal = message['container - title'][0] if 'container - title' in message and message['container - title'] else 'No journal'volume = message['volume'] if 'volume' in message else 'No volume'issue = message['issue'] if 'issue' in message else 'No issue'pages = message['page'] if 'page' in message else 'No pages'reference = f"{author_str} ({year}). {title}. {journal}, {volume}({issue}), {pages}. doi:{doi}"return referenceelse:return Noneexcept Exception:return None

get_reference函数接收DOI参数doi,创建Crossref实例cr查询该DOI对应的参考文献信息。尝试获取查询结果,若结果中存在message字段,则从中提取作者、年份、标题、期刊、卷号、期号、页码等信息,格式化为APA格式参考文献字符串并返回;若查询失败或出现异常,返回None
4. 主处理函数

def convert_dois_in_word(input_file, output_file):doc = Document(input_file)all_dois = []doi_original_index = {}index = 1# 提取文档中所有DOI并编号for paragraph in doc.paragraphs:dois = extract_dois(paragraph.text)for doi in dois:if doi not in all_dois:all_dois.append(doi)doi_original_index[doi] = indexindex += 1references = []successful_dois = []failed_dois = []# 获取每个DOI的参考文献信息for doi in all_dois:reference = get_reference(doi)if reference:references.append(reference)successful_dois.append(doi)else:failed_dois.append(doi)# 将文档中的DOI替换为上标引用序号for paragraph in doc.paragraphs:for doi in all_dois:if doi in successful_dois:index = successful_dois.index(doi) + 1runs = paragraph.runsfor run in runs:if doi in run.text:parts = run.text.split(doi)run.text = parts[0]new_run = paragraph.add_run(f"[{index}]")new_run.font.superscript = Truerun = paragraph.add_run(parts[1])# 在文档末尾添加参考文献列表doc.add_page_break()doc.add_heading('参考文献', level=1)for i, reference in enumerate(references, start=1):doc.add_paragraph(f"[{i}] {reference}")doc.save(output_file)# 打印转换结果print("成功转换的DOI:")for doi in successful_dois:print(doi)print("\n转换失败的DOI:")for doi in failed_dois:original_index = doi_original_index[doi]print(f"{original_index}. {doi}")

convert_dois_in_word函数接收输入、输出文件路径参数input_fileoutput_file。打开输入Word文档,遍历段落提取所有DOI,为每个唯一DOI编号并存储。尝试获取每个DOI的参考文献信息,区分成功与失败的DOI。再次遍历段落,将成功获取信息的DOI替换为上标引用序号。在文档末尾添加分页符、“参考文献”标题及格式化的参考文献列表,最后保存文档并打印成功和失败转换的DOI信息。
5. 使用示例

input_file = 'input.docx'
output_file = 'output.docx'
convert_dois_in_word(input_file, output_file)

定义输入、输出文件路径,调用convert_dois_in_word函数执行对Word文档DOI的转换和参考文献生成操作。

http://www.dtcms.com/wzjs/263496.html

相关文章:

  • iis网站连接数据库失败今日头条官网首页
  • php企业网站 源码潍坊seo教程
  • 建设网站图片素材学生个人网页制作教程
  • 建设的招标网站网络营销与直播电商就业前景
  • 公司做网站会计凭证怎么做网络营销推广培训机构
  • wordpress 后台 模板seo网站推广免费
  • 网站如何做品牌营销淘宝店铺推广方法
  • 导航网站优化百度指数可以查询到哪些内容
  • 做直播网站的上市公司中文域名查询官网
  • 用php做网站的原理seo工具包
  • java语言网站开发网站推广公司推荐
  • 牡丹菏泽网站建设湖北百度seo
  • 没有网站如何做cpa推广如何做网页
  • 个人营业执照查询系统怎么优化网站关键词排名
  • 一个网站锚文本可以做几个seo查询外链
  • 网站建设关键词怎么制作网站
  • 代理项目推荐seo实战培训费用
  • 网站建设软著以图搜图百度识图网页版
  • 广告公司名字简单大气三个字淘宝seo是什么意思
  • 建设外卖网站规划书水果营销软文
  • 网站建设招标方案模板关键词排名是由什么决定的
  • 淘宝美工与网站开发搜索引擎调价工具哪个好
  • 企业网站源码 企业网站管理系统合肥网络推广
  • 抖音代运营合同模板免费系统优化软件十大排名
  • 自然人做音频网站违法吗泰安百度推广公司
  • 前端工程师主要做什么工作百度seo优化收费标准
  • 珠江摩尔网站建设太原seo培训
  • 网站上的高清图怎么做seort什么意思
  • 05网学霸答案怎么关闭seo综合查询
  • php网站方案产品推广计划怎么写