我试图用Python编写一个程序,它从Excel文件中获取数据,并使用它创建Word文件。例如,考虑以下名为Problems.xlsx的Excel文件
我希望为这个Excel文件中的所有13个问题创建一个名为:(图书Id) -(章节Id)-(问题Id)的文件。每个文件应该如下所示:
nt1.docx.应该是52289-9.2-59PS.docx。但是请注意,第一行包含一个底部边框、左边的文件名和公司名称(这是常量的)和今天的日期。
,我在向文档中添加边框和更改字体大小和名称方面遇到了困难。,这是我迄今为止编写的代码。
from openpyxl import load_workbook
from docx import Document
from time import strftime
from docx.shared import Pt
#Accessing data from excel file.
ws=load_workbook(filename="Problems.xlsx")
sheet=ws.get_sheet_by_name('Sheet2')
for i in range(2,15):
fileName=""
for j in range(1,4):
if j!=3:
fileName=fileName+str(sheet.cell(row=i,column=j).value)+"-"
else:
fileName=fileName+str(sheet.cell(row=i,column=j).value)
#Now we have the file names, so let's make a file
document=Document()
run=document.add_paragraph().add_run()
font=run.font
font.name="Times New Roman"
font.size=12
date=strftime("%d/%m/%Y")
document.add_paragraph(fileName+" AID: 1112|"+date)
document.add_paragraph("--------------------------------------------------------------------------------------------------------------------")
#Saving the document with the fileName
document.save(fileName+".docx")
上面的代码创建了具有正确名称的文件,但是有两个关键问题:
发布于 2017-05-11 09:19:45
在尝试和错误之后,下面的代码工作并修复了我在问题中提到的两个问题。
from openpyxl import load_workbook
from docx import Document
from time import strftime
from docx.shared import Pt
#Accessing data from excel file.
ws=load_workbook(filename="Problems.xlsx")
sheet=ws.get_sheet_by_name('Sheet2')
for i in range(2,15):
fileName=""
for j in range(1,4):
if j!=3:
fileName=fileName+str(sheet.cell(row=i,column=j).value)+"-"
else:
fileName=fileName+str(sheet.cell(row=i,column=j).value)
#Now we have the file names, so let's make a file
document=Document()
style=document.styles['Normal']
font=style.font
font.name="Times New Roman"
font.size=Pt(12)
date=strftime("%d/%m/%Y")
font.underline=True
document.add_paragraph(fileName+" AID: 1112|"+date,style='Normal')
#Saving the document with the fileName
document.save(fileName+".docx")
https://stackoverflow.com/questions/43909889
复制相似问题