安装
pip install python-docx
这个不止是可以在mac上安装,还可以在linux上安装,并不需要你安装word,我主要的目的是在整理各个模型结果的时候,再展示给别人看的时候,可以把各个模型的结果自动写入word文件中,就不用自己跑完结果自己去每个文件夹找文件再复制粘贴或者截图到word中,这样其实太无聊了,我想着如果能让程序跑完结果后,把所有结果自动写入word中,我最终简单排下版就可以展示结果了,这样就很nice, 然后我找到了这个库
此外还有python操作ppt的库,应该也好用
pip install python-pptx
这个python-pptx感觉还是挺费劲的
测试案例1
from docx import Document
from docx.shared import Inches
import subprocess
document = Document()
document.add_heading('Document Title', 0)## 加入document title
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')
document.add_paragraph(
'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
'first item in ordered list', style='List Number'
)
document.add_picture('monty-truth.png', width=Inches(1.25))
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty)
row_cells[1].text = id
row_cells[2].text = desc
document.add_page_break()
save_word_filename="demo1.docx"
document.save(save_word_filename)
output = subprocess.check_output(['libreoffice', '--convert-to', 'pdf' ,save_word_filename])
print(output)
# import os
# import_file_name = save_word_filename
# output_file_path = "/DATA2/zhangjingxiao/yxk/"
# os.system("libreoffice --headless --convert-to pdf %s --outdir %s" % (import_file_name, output_file_path))
最终的结果如下
注意这里的libreoffice我是请管理员安装的,一开始安装的还是有问题,无法正常的将word转换成pdf,后面我找了一下原因
https://stackoverflow.com/questions/65877214/libreoffice-convert-to-pdf-produces-file-could-not-be-loaded-error-even-if-file
最终的解决办法
学习代码
参考网站
https://zhuanlan.zhihu.com/p/429395139
https://blog.csdn.net/guoxuying/article/details/114014758
最终我实现的案例
from docx import Document
from docx.shared import Inches
from docx.shared import Cm,Pt
import subprocess
import numpy as np
import pandas as pd
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.enum.text import WD_COLOR_INDEX
#################################################################################
#################################################################################
#################################################################################
#################################################################################
method_list=["AAA","BBB","CCC","DDDD","EEEE","FFFF","GGGG"]
dataset_list=["aaaa","bbbb"]
#################################################################################
#################################################################################
#################################################################################
#################################################################################
evaluation_index=["ARI","NMI","ASW(label)","ASW(label/batch)","batchKL"]
document = Document()
document.add_heading('Batch Effect Evaluation', 0)
p = document.add_heading("Compared Methods:",1)
for method in method_list:
document.add_paragraph(
method, style='List Number'
)
para=document.add_paragraph(
'bbknn', style='List Number'
)
para.add_run(
'''(Not Implemented)'''
).font.highlight_color = WD_COLOR_INDEX.YELLOW
para=document.add_paragraph(
'liger', style='List Number'
)
para.add_run(
'''(Not Implemented)'''
).font.highlight_color = WD_COLOR_INDEX.YELLOW
p = document.add_heading("Selected Evaluation Indexs",1)
for eva in evaluation_index:
document.add_paragraph(
eva, style='List Bullet'
)
document.add_paragraph(
'iLISI(to be added)', style='List Bullet'
)
document.add_paragraph(
'cLISI(to be added)', style='List Bullet'
)
p = document.add_heading("Selected DataSets",1)
for dataset in dataset_list:
document.add_paragraph(
dataset, style='List Bullet'
)
document.add_page_break()
for dataset in dataset_list:
# run=document.add_heading(dataset+' DataSet', level=1)
# run.font.size = Pt(30)
header = document.add_paragraph(style='Heading 1')
header.style.font.name = 'Times New Roman'
header.style.font.size = Pt(20)
header.add_run(dataset+' DataSet')
#################################################################
method="Raw"
df=pd.read_csv("./evaluation/"+dataset+"/"+method+"/"+dataset+"_information.csv")
t = document.add_table(df.shape[0]+1, df.shape[1])
# t.allow_autofit = True
# t.columns[0].width = Cm(4.5)
#t.style = 'Colorful Grid Accent 1'
t.style = 'Colorful List'
# add the header rows.
# for i in range(df.shape[0]):
# t.cell(i,0).text = df.index[i]
for j in range(df.shape[-1]):
t.cell(0,j).text = df.columns[j]
#t.cell(0,j).paragraphs[0].runs[0].font.size = Pt(5)
# add the rest of the data frame
for i in range(df.shape[0]):
for j in range(df.shape[-1]):
if(j==0):
t.cell(i+1,j).text = str(df.values[i,j])
#t.cell(i+1,j).paragraphs[0].runs[0].font.size = Pt(5)
t.cell(i+1,j).paragraphs[0].alignment=WD_PARAGRAPH_ALIGNMENT.CENTER
t.cell(i+1,j).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER # 水平居中
else:
t.cell(i+1,j).text = str(np.round(df.values[i,j],3))
t.cell(i+1,j).paragraphs[0].alignment=WD_PARAGRAPH_ALIGNMENT.CENTER
t.cell(i+1,j).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER # 水平居中
document.add_heading('Visualization Result(scanpy UMAP)', level=2)
document.add_heading(method, level=3)
document.add_picture("./evaluation/"+dataset+"/"+method+"/"+dataset+"_"+method+"_scanpy_vis.png",Inches(7))
############################################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
for method in method_list:
document.add_heading(method, level=3)
document.add_picture("./evaluation/"+dataset+"/"+method+"/"+dataset+"_"+method+"_scanpy_vis.png",Inches(7))
document.add_page_break()
document.add_heading("Evalution Summary Table({})".format(dataset), level=2)
document.add_paragraph('')
df=pd.read_csv("./evaluation/"+dataset+"/"+dataset+"_evaluation.csv")
df["bbknn"]=df["scVI"]
df["liger"]=df["scVI"]
# add a table to the end and create a reference variable
# extra row is so we can add the header row
t = document.add_table(df.shape[0]+1, df.shape[1])
# t.allow_autofit = True
# t.columns[0].width = Cm(4.5)
#t.style = 'Colorful Grid Accent 1'
t.style = 'Colorful List'
# add the header rows.
# for i in range(df.shape[0]):
# t.cell(i,0).text = df.index[i]
for j in range(df.shape[-1]):
t.cell(0,j).text = df.columns[j]
t.cell(0,j).paragraphs[0].runs[0].font.size = Pt(5)
# add the rest of the data frame
for i in range(df.shape[0]):
for j in range(df.shape[-1]):
if(j==0):
t.cell(i+1,j).text = str(df.values[i,j])
t.cell(i+1,j).paragraphs[0].runs[0].font.size = Pt(5)
t.cell(i+1,j).paragraphs[0].alignment=WD_PARAGRAPH_ALIGNMENT.CENTER
t.cell(i+1,j).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER # 水平居中
else:
t.cell(i+1,j).text = str(np.round(df.values[i,j],3))
t.cell(i+1,j).paragraphs[0].alignment=WD_PARAGRAPH_ALIGNMENT.CENTER
t.cell(i+1,j).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER # 水平居中
#
document.add_picture("./evaluation/"+dataset+"/"+dataset+"_ARI_NMI.png",Inches(4))
document.add_picture("./evaluation/"+dataset+"/"+dataset+"_ASW.png",Inches(4))
document.add_picture("./evaluation/"+dataset+"/"+dataset+"_BatchKL.png",Inches(4))
document.add_page_break()
save_word_filename="展示结果.docx"
document.save(save_word_filename)
output = subprocess.check_output(['libreoffice', '--convert-to', 'pdf' ,save_word_filename])
print(output)