如何使用 Python 将目录中的所有图像添加到 word 文档
How can I add all the images in a directory to a word document using Python
我一直在尝试使此代码正常工作,以便我可以将数百张图片添加到 Microsoft Word 文档中,但无法完全正常工作。我认为问题在于正确定义图像的位置。
import win32com.client as win32
import os
#creating a word application object
wordApp = win32.gencache.EnsureDispatch('Word.Application') #create a word application object
wordApp.Visible = True # hide the word application
doc = wordApp.Documents.Add() # create a new application
#Formating the document
doc.PageSetup.RightMargin = 20
doc.PageSetup.LeftMargin = 20
doc.PageSetup.Orientation = win32.constants.wdOrientLandscape
# a4 paper size: 595x842
doc.PageSetup.PageWidth = 595
doc.PageSetup.PageHeight = 842
header_range= doc.Sections(1).Headers(win32.constants.wdHeaderFooterPrimary).Range
header_range.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
header_range.Font.Bold = True
header_range.Font.Size = 20
header_range.Text = "Header Of The Document"
# Inserting Tables
total_column = 2
total_row = 5
rng = doc.Range(0,0)
rng.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
table = doc.Tables.Add(rng,total_row, total_column)
table.Borders.Enable = False
if total_column > 1:
table.Columns.DistributeWidth()
#Collecting images in the same directory and inserting them into the document
frame_max_width= 167 # the maximum width of a picture
frame_max_height= 125 # the maximum height of a picture
filenames = os.listdir("some_directory") #Do I need this? I think it might be the issue...
for index, filename in enumerate(filenames): # loop through all the files and folders for adding pictures
if os.path.isfile(os.path.join(os.path.abspath("."), filename)): # check whether the current object is a file or not
if filename[len(filename)-3: len(filename)].upper() == 'JPG': # check whether the current object is a JPG file
#calculating the position of each image to be put into the correct table cell
cell_column= index % total_column + 1
cell_row = index / total_column + 1
print 'cell_column=%s,cell_row=%s' % (cell_column,cell_row)
#we are formatting the style of each cell
cell_range= table.Cell(cell_row, cell_column).Range
cell_range.ParagraphFormat.LineSpacingRule = win32.constants.wdLineSpaceSingle
cell_range.ParagraphFormat.SpaceBefore = 0
cell_range.ParagraphFormat.SpaceAfter = 3
#this is where we are going to insert the images
current_pic = cell_range.InlineShapes.AddPicture(os.path.join(os.path.abspath("."), filename))
width, height = (frame_max_height*width/height, frame_max_height)
#changing the size of each image to fit the table cell
current_pic.Height= height
current_pic.Width= width
#putting a name underneath each image which can be handy
table.Cell(cell_row, cell_column).Range.InsertAfter("\n"+filename)
这段代码让我创建了我的文档并插入了一张图片,但随后我收到以下错误:回溯(最近一次调用最后一次):
文件 "pic_dump.py",第 56 行,位于
宽度, 高度 = (frame_max_height*width/height, frame_max_height)
NameError: 名称 'width' 未定义。
感谢收到的任何帮助。
问题出在这一行。 width, height = (frame_max_height*width/height, frame_max_height)
.
您正在设置 width
= frame_max_height*width/height
,但 python 不可能设置为 frame_max_height
* width
次告诉 Python width
是什么。您需要先将 width
设置为某个值才能解决此错误。
好的,非常感谢 Me 先生,我现在可以按照自己的意愿工作了。工作代码如下:
import win32com.client as win32
import os
#creating a word application object
wordApp = win32.gencache.EnsureDispatch('Word.Application') #create a word application object
wordApp.Visible = True # hide the word application
doc = wordApp.Documents.Add() # create a new application
#Formating the document
doc.PageSetup.RightMargin = 20
doc.PageSetup.LeftMargin = 20
doc.PageSetup.Orientation = win32.constants.wdOrientLandscape
# a4 paper size: 595x842
doc.PageSetup.PageWidth = 595
doc.PageSetup.PageHeight = 842
# Inserting Tables
my_dir="C:\Users\Public\Pictures\Sample Pictures"
filenames = os.listdir(my_dir)
piccount=0
file_count = 0
for i in filenames:
if i[len(i)-3: len(i)].upper() == 'JPG': # check whether the current object is a JPG file
piccount = piccount + 1
#file_count= file_count + 1
print piccount, " images will be inserted"
#print filenames
total_column = 2
total_row = int(piccount/total_column)+2
rng = doc.Range(0,0)
rng.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
table = doc.Tables.Add(rng,total_row, total_column)
table.Borders.Enable = False
if total_column > 1:
table.Columns.DistributeWidth()
#Collecting images in the same directory and inserting them into the document
frame_max_width= 167 # the maximum width of a picture
frame_max_height= 125 # the maximum height of a picture
piccount = 1
for index, filename in enumerate(filenames): # loop through all the files and folders for adding pictures
#if os.path.isfile(os.path.join(os.path.abspath("."), filename)):
if os.path.isfile(os.path.join(os.path.abspath(my_dir), filename)): # check whether the current object is a file or not
if filename[len(filename)-3: len(filename)].upper() == 'JPG': # check whether the current object is a JPG file
piccount = piccount + 1
print filename, len(filename), filename[len(filename)-3: len(filename)].upper()
cell_column = (piccount % total_column + 1) #calculating the position of each image to be put into the correct table cell
cell_row = (piccount/total_column + 1)
print 'cell_column=%s,cell_row=%s' % (cell_column,cell_row)
#we are formatting the style of each cell
cell_range= table.Cell(cell_row, cell_column).Range
cell_range.ParagraphFormat.LineSpacingRule = win32.constants.wdLineSpaceSingle
cell_range.ParagraphFormat.SpaceBefore = 0
cell_range.ParagraphFormat.SpaceAfter = 3
#this is where we are going to insert the images
current_pic = cell_range.InlineShapes.AddPicture(os.path.join(os.path.abspath(my_dir), filename))
width, height = (frame_max_height*frame_max_width/frame_max_height, frame_max_height)
#changing the size of each image to fit the table cell
current_pic.Height= height
current_pic.Width= width
#putting a name underneath each image which can be handy
table.Cell(cell_row, cell_column).Range.InsertAfter("\n"+filename)
else: continue
我一直在尝试使此代码正常工作,以便我可以将数百张图片添加到 Microsoft Word 文档中,但无法完全正常工作。我认为问题在于正确定义图像的位置。
import win32com.client as win32
import os
#creating a word application object
wordApp = win32.gencache.EnsureDispatch('Word.Application') #create a word application object
wordApp.Visible = True # hide the word application
doc = wordApp.Documents.Add() # create a new application
#Formating the document
doc.PageSetup.RightMargin = 20
doc.PageSetup.LeftMargin = 20
doc.PageSetup.Orientation = win32.constants.wdOrientLandscape
# a4 paper size: 595x842
doc.PageSetup.PageWidth = 595
doc.PageSetup.PageHeight = 842
header_range= doc.Sections(1).Headers(win32.constants.wdHeaderFooterPrimary).Range
header_range.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
header_range.Font.Bold = True
header_range.Font.Size = 20
header_range.Text = "Header Of The Document"
# Inserting Tables
total_column = 2
total_row = 5
rng = doc.Range(0,0)
rng.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
table = doc.Tables.Add(rng,total_row, total_column)
table.Borders.Enable = False
if total_column > 1:
table.Columns.DistributeWidth()
#Collecting images in the same directory and inserting them into the document
frame_max_width= 167 # the maximum width of a picture
frame_max_height= 125 # the maximum height of a picture
filenames = os.listdir("some_directory") #Do I need this? I think it might be the issue...
for index, filename in enumerate(filenames): # loop through all the files and folders for adding pictures
if os.path.isfile(os.path.join(os.path.abspath("."), filename)): # check whether the current object is a file or not
if filename[len(filename)-3: len(filename)].upper() == 'JPG': # check whether the current object is a JPG file
#calculating the position of each image to be put into the correct table cell
cell_column= index % total_column + 1
cell_row = index / total_column + 1
print 'cell_column=%s,cell_row=%s' % (cell_column,cell_row)
#we are formatting the style of each cell
cell_range= table.Cell(cell_row, cell_column).Range
cell_range.ParagraphFormat.LineSpacingRule = win32.constants.wdLineSpaceSingle
cell_range.ParagraphFormat.SpaceBefore = 0
cell_range.ParagraphFormat.SpaceAfter = 3
#this is where we are going to insert the images
current_pic = cell_range.InlineShapes.AddPicture(os.path.join(os.path.abspath("."), filename))
width, height = (frame_max_height*width/height, frame_max_height)
#changing the size of each image to fit the table cell
current_pic.Height= height
current_pic.Width= width
#putting a name underneath each image which can be handy
table.Cell(cell_row, cell_column).Range.InsertAfter("\n"+filename)
这段代码让我创建了我的文档并插入了一张图片,但随后我收到以下错误:回溯(最近一次调用最后一次): 文件 "pic_dump.py",第 56 行,位于 宽度, 高度 = (frame_max_height*width/height, frame_max_height) NameError: 名称 'width' 未定义。
感谢收到的任何帮助。
问题出在这一行。 width, height = (frame_max_height*width/height, frame_max_height)
.
您正在设置 width
= frame_max_height*width/height
,但 python 不可能设置为 frame_max_height
* width
次告诉 Python width
是什么。您需要先将 width
设置为某个值才能解决此错误。
好的,非常感谢 Me 先生,我现在可以按照自己的意愿工作了。工作代码如下:
import win32com.client as win32
import os
#creating a word application object
wordApp = win32.gencache.EnsureDispatch('Word.Application') #create a word application object
wordApp.Visible = True # hide the word application
doc = wordApp.Documents.Add() # create a new application
#Formating the document
doc.PageSetup.RightMargin = 20
doc.PageSetup.LeftMargin = 20
doc.PageSetup.Orientation = win32.constants.wdOrientLandscape
# a4 paper size: 595x842
doc.PageSetup.PageWidth = 595
doc.PageSetup.PageHeight = 842
# Inserting Tables
my_dir="C:\Users\Public\Pictures\Sample Pictures"
filenames = os.listdir(my_dir)
piccount=0
file_count = 0
for i in filenames:
if i[len(i)-3: len(i)].upper() == 'JPG': # check whether the current object is a JPG file
piccount = piccount + 1
#file_count= file_count + 1
print piccount, " images will be inserted"
#print filenames
total_column = 2
total_row = int(piccount/total_column)+2
rng = doc.Range(0,0)
rng.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
table = doc.Tables.Add(rng,total_row, total_column)
table.Borders.Enable = False
if total_column > 1:
table.Columns.DistributeWidth()
#Collecting images in the same directory and inserting them into the document
frame_max_width= 167 # the maximum width of a picture
frame_max_height= 125 # the maximum height of a picture
piccount = 1
for index, filename in enumerate(filenames): # loop through all the files and folders for adding pictures
#if os.path.isfile(os.path.join(os.path.abspath("."), filename)):
if os.path.isfile(os.path.join(os.path.abspath(my_dir), filename)): # check whether the current object is a file or not
if filename[len(filename)-3: len(filename)].upper() == 'JPG': # check whether the current object is a JPG file
piccount = piccount + 1
print filename, len(filename), filename[len(filename)-3: len(filename)].upper()
cell_column = (piccount % total_column + 1) #calculating the position of each image to be put into the correct table cell
cell_row = (piccount/total_column + 1)
print 'cell_column=%s,cell_row=%s' % (cell_column,cell_row)
#we are formatting the style of each cell
cell_range= table.Cell(cell_row, cell_column).Range
cell_range.ParagraphFormat.LineSpacingRule = win32.constants.wdLineSpaceSingle
cell_range.ParagraphFormat.SpaceBefore = 0
cell_range.ParagraphFormat.SpaceAfter = 3
#this is where we are going to insert the images
current_pic = cell_range.InlineShapes.AddPicture(os.path.join(os.path.abspath(my_dir), filename))
width, height = (frame_max_height*frame_max_width/frame_max_height, frame_max_height)
#changing the size of each image to fit the table cell
current_pic.Height= height
current_pic.Width= width
#putting a name underneath each image which can be handy
table.Cell(cell_row, cell_column).Range.InsertAfter("\n"+filename)
else: continue