文章目录
== 本系列记录办公时常用的Python文件操作 ==
1.TXT读写操作
1.1 读取文件名至TXT中
=== 读取文件夹名输出至txt文件===
def getfoderList(foderPath):
fList=[fl for fl in os.listdir(foderPath) if os.path.isdir(os.path.join(foderPath,fl))]
return fList
def writeFpToTxt(flist,foderPath):
filepath=os.path.join(foderPath,"文件夹名.txt")
with open(filepath,'w+') as txtf: #不存在则创建
for f in flist:
txtf.write(f+"\n")
txtf.close()
if __name__ == "__main__":
foderPath=r"Y:\BIM大赛"
fl=getfoderList(foderPath)
writeFpToTxt(fl,foderPath)
2.EXCEL读写操作
2.1 采用openpyxl读写填XLSX
import os,time
from openpyxl import Workbook,load_workbook
from openpyxl.styles import PatternFill,colors
=== 读取指定后缀文件名===
def getfileList(foderPath):
flist = [fn.split('-')[0] for fn in os.listdir(foderPath) if(os.path.splitext(fn)[1] in [".doc",".docx",".pdf"])]#举例示意可进行的操作
return flist
===查找文件名,更新汇总台账====
def updateExcel():
foderpath=os.getcwd()#sys.path[0]
wb=load_workbook(foderpath+r"\高企课题台账模板.xlsx")
ws=wb['公司高企课题清单']#wb.sheetnames[0]
l_l=getfileList(foderpath+r"\立项报告自查汇总")# 采用 1.1节 getfileList()方法
i = 3
while i<ws.max_row:
c=str(i)
cfill=PatternFill(fill_type='solid',fgColor='FF0000')
cell_i=ws.cell(i,2).value.split('/')[0]
if cell_i in l_l:#先行后列,索引下标
ws['G'+c].value="是"
else:
ws['G'+c].value=""
ws['G'+c].fill=cfill
i+=1
strTime=str(time.strftime("%Y-%m-%d", time.localtime()))
wb.save(foderpath+r'\高企课题资料实时台账'+strTime+'.xlsx')
3. WORD文件操作
3.1 WORD批量转PDF
======文件夹下所有word转pdf========
def doc_pdf(foderPath):
succ=0
flist=list(filter(lambda x:x.endswith('.doc') or x.endswith('.docx'),os.listdir(foderPath)))
word = Dispatch('Word.Application')
##word = gencache.EnsureDispatch('Word.Application')
try:
for fpath in flist:
doc = word.Documents.Open(foderPath+"\\"+fpath, ReadOnly=1)
pdf = foderPath+"\\"+fpath.split('.doc')[0] + '.pdf'
doc.ExportAsFixedFormat(pdf, constants.wdExportFormatPDF, Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
succ+=1
print(str(succ)+'word file successfully converted to pdf file!')
except Exception as e:
print('Error,exception is:{}'.format(e))
finally:
word.Quit(constants.wdDoNotSaveChanges)
4. PDF文件操作
4.1 PDF文件合并
======文件夹下所有pdf文件用空白页补充为偶数页后再合并========
def mergePdfWithBlank(foderPath,blankpdfPath=os.getcwd()+"\\blank.pdf"):
#flist = filter(lambda x: x.endswith('.pdf'), os.listdir(foderPath)).sort(key=lambda x: int(x[:-4]), reverse=False)
optPageNums=0
succ=0
merger=PdfFileMerger()
flist=glob.glob(foderPath+'\*.pdf')#空白页不能放在文件夹下
try:
blankpdf=PdfFileReader(open(blankpdfPath,'rb'))
for file in flist:
pdf=PdfFileReader(open(file,'rb'))
pageNum=pdf.numPages+1
optPageNums+=pageNum
succ+=1
if pageNum & 1:#是偶数
merger.append(pdf)
else:
merger.append(pdf)
merger.append(blankpdf)
optPageNums += 1
merger.write(os.path.join(foderPath,'合并.pdf'))
print(succ+'pdf file successfully combined to <合并.pdf>pdf file!')
except Exception as e:
print('Error,exception is:{}'.format(e)) # 格式化e
finally:
merger.close()
========创建空白PDF文件========
def newBlankPdf(foderPath):
word = gencache.EnsureDispatch('Word.Application')
newdoc = word.Documents.Add
blankpdfPath = os.path.join(foderPath, '空白.pdf')
newdoc.SaveAs(blankpdfPath, FileFormat=17)
newdoc.Close()
word.Quit()
return blankpdfPath
5. 主方法和脚本使用
======第3、4节主方法命令=======
if __name__ == "__main__":
description = '\n' + '本脚本用来转换word并合并pdf文件,同时奇数页PDF插入空白页,输出的pdf文件(合并.pdf)按输入的pdf文件名生成书签\n使用示例如下:'
description = description + '\n' + 'python Docs2pdf.py -f "0" -p "D:\pdf-files" '
description = description + '\n' + "要合并的pdf文件所在的路径: D:\pdf-files"
parser = ArgumentParser(description=description, formatter_class=RawTextHelpFormatter)
parser.add_argument("-f","--function",dest="func",type=int,default=1,help="选择功能:\n 0 ---文件夹下word合并 \n 1 ---文件夹下word转pdf并合并pdf \n 2 ---文件夹下pdf合并")
parser.add_argument("-p", "--path", dest="path", default=r"C:\Users\30624\Desktop\Test", help="要操作的文件所在目录")
args = parser.parse_args()
if not args.func==1:
args.func=int(input("选择功能:\n 0 ---文件夹下word合并 \n 1 ---文件夹下word转pdf并合并pdf \n 2 ---文件夹下pdf合并"))
args.path=input("要合并的pdf文件所在的路径:如 D:\pdf-files")
if args.func==0:
mergeDoc(args.path)
if args.func==1:
doc_pdf(args.path)
mergePdfWithBlank(args.path)
else:
mergePdfWithBlank(args.path)
=====本脚本使用方法=======
# 方法一 cd /d 进入脚本路径,输入python Docs2pdf.py -f 1 -p "C:\Users\30624\Desktop\Test"
# 方法二 调出cmd,直接将脚本拖进去
# 方法三 进入Python脚本所在的文件夹,shift+右击,选择“在此处打开命令窗口”,按TAB键切换文件,选择目标python脚本