本文主要是介绍kunpeng的aarch64架构cpu、openeuler系统、昇腾服务器适配文档转换功能(doc转docx、ppt转pptx),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、安装flatpak
sudo yum install flatpak
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
二、安装libreoffice
flatpak install flathub org.libreoffice.LibreOffice
三、使用
对于使用 flatpak 安装的 LibreOffice,不需要手动启动或设置任何环境变量。flatpak 提供了一个沙箱化的运行环境,确保应用程序可以正常运行。
flatpak 应用程序的可执行文件通常位于类似
/var/lib/flatpak/app/org.libreoffice.LibreOffice/aarch64/stable/active/export/bin/org.libreoffice.LibreOffice
只要在代码中正确指定了这个完整路径,就可以直接运行和调用 LibreOffice,而无需进行任何其他设置。
四、示例代码-doc
import tqdm
import subprocess
import os
from dotenv import load_dotenv
from docx.table import _Cell, Table
from docx.oxml.table import CT_Tbl
from docx.oxml.text.paragraph import CT_P
from docx.text.paragraph import Paragraph
from docx import Document
import reload_dotenv()
libreoffice_path = "/var/lib/flatpak/app/org.libreoffice.LibreOffice/aarch64/stable/active/export/bin/org.libreoffice.LibreOffice"
def convert_doc_to_docx(doc_file_path):if os.path.basename(doc_file_path).split(".")[1] == "docx":return doc_file_pathelif os.path.basename(doc_file_path).split(".")[1] == "doc":# Define the command to run LibreOffice in headless modecommand = [libreoffice_path,'--headless','--convert-to', 'docx','--outdir', os.path.dirname(doc_file_path),doc_file_path]# Run the commandresult = subprocess.run(command, capture_output=True, text=True)if result.returncode != 0:raise RuntimeError(f"Failed to convert '{doc_file_path}' to DOCX.\nError: {result.stderr}")return doc_file_path.replace('.doc', '.docx')else:return Falsedef doc2text(filepath):filepath = convert_doc_to_docx(filepath)doc = Document(filepath)resp = ""def iter_block_items(parent):from docx.document import Documentif isinstance(parent, Document):parent_elm = parent.element.bodyelif isinstance(parent, _Cell):parent_elm = parent._tcelse:raise ValueError("parse fail")for child in parent_elm.iterchildren():if isinstance(child, CT_P):yield Paragraph(child, parent)elif isinstance(child, CT_Tbl):yield Table(child, parent)for block in iter_block_items(doc):if isinstance(block, Paragraph):resp += block.text.strip() + "\n"elif isinstance(block, Table):for row in block.rows:for cell in row.cells:for paragraph in cell.paragraphs:resp += paragraph.text.strip() + "\n"resp = re.sub(r'\n+', '\n', resp)resp = re.sub(r'(.)\1{4,}', r'\1', resp)return {'document':resp,'metadata':filepath,'format':"docx_text"}if __name__ == '__main__':import jsonfile_path="/opt/rag/data/xxx.doc"loader = doc2text(file_path)output_path = os.path.join(os.path.dirname(file_path), os.path.basename(file_path).split(".")[0] + "_docx"+ ".json")with open(output_path, 'w', encoding='utf-8') as f:json.dump(loader, f, ensure_ascii=False, indent=4)
五、示例代码-ppt
import os
import subprocess
from dotenv import load_dotenv
from unstructured.chunking.title import chunk_by_title
from unstructured.documents.elements import CompositeElement, Table
from unstructured.partition.pptx import partition_pptx
import jsonload_dotenv()
libreoffice_path = "/var/lib/flatpak/app/org.libreoffice.LibreOffice/aarch64/stable/active/export/bin/org.libreoffice.LibreOffice"def remove_duplicates(lst):res = []seen = {}for i in lst:if i not in seen:seen[i] = 1res.append(i)return resdef ppt2text(file_name: str):file_name = convert_ppt_to_pptx(file_name)elements = partition_pptx(filename=file_name,multipage_sections=True,infer_table_structure=True,include_page_breaks=False,)chunks = chunk_by_title(elements=elements,multipage_sections=True,combine_text_under_n_chars=0,new_after_n_chars=None,max_characters=4096,)data = dict()text_list = []for chunk in chunks:if isinstance(chunk, CompositeElement):text = chunk.texttext_list.append(text)elif isinstance(chunk, Table):if text_list:text_list[-1] = text_list[-1] + "\n" + chunk.metadata.text_as_htmlelse:text_list.append(chunk.hunk.metadata.text_as_html)data['document'] = remove_duplicates(text_list)data['metadata'] = file_namedata['format'] = "pptx_text"return datadef convert_ppt_to_pptx(ppt_file_path):if os.path.basename(ppt_file_path).split(".")[1] == "pptx":return ppt_file_pathelif os.path.basename(ppt_file_path).split(".")[1] == "ppt":# Define the command to run LibreOffice in headless modecommand = [libreoffice_path,'--headless','--convert-to', 'pptx','--outdir', os.path.dirname(ppt_file_path),ppt_file_path]# Run the commandresult = subprocess.run(command, capture_output=True, text=True)if result.returncode != 0:raise RuntimeError(f"Failed to convert '{ppt_file_path}' to PPTX.\nError: {result.stderr}")return ppt_file_path.replace('.ppt', '.pptx')else:return Falseif __name__ == "__main__":pptx_file_path = "/opt/data/xxx.ppt"contents = ppt2text(pptx_file_path)# print(contents)output_path = os.path.join(os.path.dirname(pptx_file_path), os.path.basename(pptx_file_path).split(".")[0] + "_ppt" + ".json")with open(output_path, 'w', encoding='utf-8') as f:json.dump(contents, f, ensure_ascii=False, indent=4)
这篇关于kunpeng的aarch64架构cpu、openeuler系统、昇腾服务器适配文档转换功能(doc转docx、ppt转pptx)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!