2017-05-11 7 views
0

Excelファイルからデータを取り出してWordファイルを作成するプログラムをPythonで作成しようとしています。 たとえば、次のExcelファイルの名前をProblems.xlsxとします。enter image description herePythonを使ったボーダーの追加に関する質問docxワード文書へのパッケージ

このExcelファイルに記載されている13の問題のすべてについて、(Book Id) - (Problem Id)という名前のファイルを作成します。各ファイルは次のようになります。

Ignore the file name, which is Docume 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") 

上記のコードは、正しい名前のファイルを作成しますが、二つの重要な問題があります。

  1. はフォントがまだのTimes New Romanの代わりにCalibriままで、フォントサイズが11ではなく12です
  2. 私は今のところ破線を追加しましたが、私は本当に赤い丸で囲まれたボタンを通常クリックする下の境界線を持っていたいと思います。

答えて

0

試行錯誤の末、次のコードで問題が解決しました。

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") 
関連する問題