将一个word文档按一页或多页拆分成多个文档

工作中,有时候碰到需要将一个比较大的word按照指定的页数分割成若干个小的word文档,下面提供分割的方法供参考:

一、按照单页拆分

1、在Word里面打开那个需要分割的文档(假设它的文件名叫做“test.doc”);

2、键入ALT+F11打开VBA编辑器,选择菜单“插入-模块”,或者键入ALT+F8打开宏命令窗口,或者点文档内上面的“工具”→“宏”→“宏”→找到这个宏→“运行”即可;

3、粘贴下面的代码:

Option Explicit

Sub SplitPagesAsDocuments()

Dim oSrcDoc As Document, oNewDoc As Document

Dim strSrcName As String, strNewName As String

Dim oRange As Range

Dim nIndex As Integer

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")

Set oSrcDoc = ActiveDocument

Set oRange = oSrcDoc.Content

oRange.Collapse wdCollapseStart

oRange.Select

For nIndex = 1 To ActiveDocument.Content.Information(wdNumberOfPagesInDocument)

oSrcDoc.Bookmarks("\page").Range.Copy

oSrcDoc.Windows(1).Activate

Application.Browser.Target = wdBrowsePage

Application.Browser.Next

strSrcName = oSrcDoc.FullName

strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
fso.GetBaseName(strSrcName) & "_" & nIndex & "." & fso.GetExtensionName(strSrcName))
Set oNewDoc = Documents.Add

Selection.Paste

oNewDoc.SaveAs strNewName

oNewDoc.Close False

Next

Set oNewDoc = Nothing

Set oRange = Nothing

Set oSrcDoc = Nothing

Set fso = Nothing

MsgBox "结束!"

End Sub

4、键入F5运行,看到“完成!”结束。

5、检查当前文档所在路径下是否生成若干名为“原始文档_n.doc”(n代表其对应原始文档中的第几页)的文档,检查它们的内容是否就对应于原始文档每个页面的内容。

 

二、按指定页动态拆分

 上述方法是按单页拆分的,如果想按照指定页数拆分,可使用下面的代码,其它步骤和上述按单页拆分方案相同。

代码如下:

Option Explicit

Sub DynamicSplitPagesAsDocuments()

Dim oSrcDoc As Document, oNewDoc As Document
Dim strSrcName As String, strNewName As String
Dim oRange As Range
Dim nIndex As Integer, nSubIndex As Integer, nTotalPages As Integer, nBound As Integer
Dim fso As Object

Const nSteps = 3   //这里可以指定需要拆分的页数,如这里表示按照每3页拆分成一个小文档

Set fso = CreateObject("Scripting.FileSystemObject")
Set oSrcDoc = ActiveDocument
Set oRange = oSrcDoc.Content

nTotalPages = ActiveDocument.Content.Information(wdNumberOfPagesInDocument)
oRange.Collapse wdCollapseStart
oRange.Select
For nIndex = 1 To nTotalPages Step nSteps
Set oNewDoc = Documents.Add
If nIndex + nSteps > nTotalPages Then
nBound = nTotalPages
Else
nBound = nIndex + nSteps - 1
End If
For nSubIndex = nIndex To nBound
oSrcDoc.Activate
oSrcDoc.Bookmarks("\page").Range.Copy
oSrcDoc.Windows(1).Activate
Application.Browser.Target = wdBrowsePage
Application.Browser.Next

oNewDoc.Activate
oNewDoc.Windows(1).Selection.Paste
Next nSubIndex
strSrcName = oSrcDoc.FullName
strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
fso.GetBaseName(strSrcName) & "_" & (nIndex \ nSteps) & "." & fso.GetExtensionName(strSrcName))
oNewDoc.SaveAs strNewName
oNewDoc.Close False
Next nIndex
Set oNewDoc = Nothing
Set oRange = Nothing
Set oSrcDoc = Nothing
Set fso = Nothing
MsgBox "结束!"

End Sub

 

注意:

如文档中有分节符分解后的文档会出现空白页,如要分解后不出现空白页,需要把文档中的分节符全部删除。

分节符若全部替换,要注意替换后文档可能会出现排版混乱,这则需要自己手动排版了。

分节符是指为表示节的结尾插入的标记。分节符包含节的格式设置元素,如页边距、页面的方向、页眉和页脚,以及页码的顺序。分节符用一条横贯屏幕的虚双线表示。

 

删除分节符的方法:

试着按住shift键,然后点击鼠标左键,然后将显示出来的分节符删掉即可。

 

分节符和分页符的区别:

分页符只是分页,前后还是同一节;
分节符是分节,可以同一页中不同节,也可以分节的同时下一页。
两者用法的最大区别在于页眉页脚与页面设置,比如:
1、文档编排中,某几页需要横排,或者需要不同的纸张、页边距等,那么将这几页单独设为一节,与前后内容不同节;
2、文档编排中,首页、目录等的页眉页脚、页码与正文部分需要不同,那么将首页、目录等作为单独的节;
3、如果前后内容的页面编排方式与页眉页脚都一样,只是需要新的一页开始新的一章,那么一般用分页符即可,当然用分节符(下一页)也行。

 

转载于:https://www.cnblogs.com/cookiewu/p/9845472.html

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您回答这个问题。这里提供一些可以使用的Python库来实现这些功能: - PyPDF2:用于合并、拆分和旋转PDF文件。使用该库可以将多个PDF文件合并为一个文件,或者将单个PDF文件拆分多个独立的文件。此外,还可以旋转PDF页面,使其横向或纵向排列。 - pdfminer:用于从PDF文件中提取文本内容。该库可以解析PDF文件并提取其中的文本内容,包括标准字符、空格、换行符等。 - reportlab:用于添加水印和创建PDF文件。该库可以创建新的PDF文件,并在其中添加文本、图片或自定义图形。使用该库可以很容易地添加水印,例如公司标志或文档所有者的名称。 以下是一个示例代码,演示如何使用上述库实现Word文档与PDF文档的格式转换、PDF文件的合并、拆分、PDF页面旋转以及PDF页面增加水印: ``` python # 导入所需的库 import os import re from io import BytesIO from reportlab.pdfgen import canvas from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.converter import TextConverter, PDFPageAggregator from pdfminer.layout import LAParams, LTTextBox, LTTextLine # 将Word文档转换为PDF文件 def convert_to_pdf(doc_path): os.system(f'unoconv -f pdf {doc_path}') pdf_path = os.path.splitext(doc_path)[0] + '.pdf' return pdf_path # 提取PDF文本内容 def extract_pdf_text(pdf_path): rsrcmgr = PDFResourceManager() retstr = BytesIO() laparams = LAParams() device = TextConverter(rsrcmgr, retstr, laparams=laparams) with open(pdf_path, 'rb') as fp: interpreter = PDFPageInterpreter(rsrcmgr, device) for page in PDFPage.get_pages(fp): interpreter.process_page(page) layout = device.get_result() for lt_obj in layout: if isinstance(lt_obj, LTTextBox) or isinstance(lt_obj, LTTextLine): text = lt_obj.get_text() yield text device.close() retstr.close() # 合并PDF文件 def merge_pdfs(pdf_paths, output_path): merger = PdfFileMerger() for pdf_path in pdf_paths: with open(pdf_path, 'rb') as fp: merger.append(PdfFileReader(fp)) with open(output_path, 'wb') as fp: merger.write(fp) # 拆分PDF文件 def split_pdf(pdf_path, output_dir): with open(pdf_path, 'rb') as fp: reader = PdfFileReader(fp) for i in range(reader.getNumPages()): writer = PdfFileWriter() writer.addPage(reader.getPage(i)) output_path = os.path.join(output_dir, f'page_{i+1}.pdf') with open(output_path, 'wb') as fp: writer.write(fp) # 旋转PDF页面 def rotate_pdf_pages(pdf_path, angle): with open(pdf_path, 'rb') as fp: reader = PdfFileReader(fp) writer = PdfFileWriter() for i in range(reader.getNumPages()): page = reader.getPage(i) page.rotateClockwise(angle) writer.addPage(page) with open(pdf_path, 'wb') as fp: writer.write(fp) # 添加水印到PDF页面 def add_watermark_to_pdf(pdf_path, watermark_text): with open(pdf_path, 'rb') as fp: reader = PdfFileReader(fp) writer = PdfFileWriter() for i in range(reader.getNumPages()): page = reader.getPage(i) canvas_obj = canvas.Canvas(BytesIO()) canvas_obj.setFont('Helvetica', 16) canvas_obj.setFillAlpha(0.3) canvas_obj.drawString(150, 200, watermark_text) canvas_obj.save() watermark = PdfFileReader(BytesIO(canvas_obj.getpdfdata())) page.mergePage(watermark.getPage(0)) writer.addPage(page) with open(pdf_path, 'wb') as fp: writer.write(fp) # 测试代码 if __name__ == '__main__': # 将Word文档转换为PDF文件 doc_path = '/path/to/word/document.docx' pdf_path = convert_to_pdf(doc_path) # 提取PDF文本内容 for text in extract_pdf_text(pdf_path): print(text) # 合并PDF文件 pdf_paths = ['/path/to/pdf/file1.pdf', '/path/to/pdf/file2.pdf'] output_path = '/path/to/output/merged.pdf' merge_pdfs(pdf_paths, output_path) # 拆分PDF文件 pdf_path = '/path/to/pdf/file.pdf' output_dir = '/path/to/output' split_pdf(pdf_path, output_dir) # 旋转PDF页面 pdf_path = '/path/to/pdf/file.pdf' angle = 90 rotate_pdf_pages(pdf_path, angle) # 添加水印到PDF页面 pdf_path = '/path/to/pdf/file.pdf' watermark_text = 'Confidential' add_watermark_to_pdf(pdf_path, watermark_text) ``` 希望这样有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值