读取目录/子目录下面的文档

 '''

python

import os
import re
import pandas as pd
#from openpyxl import Workbook
#wb = Workbook()
#ws = wb.active
one={}
all1=[]
columns = ['sysname', 'stp bridge-address']


txt_files = []
for root, dirs, files in os.walk(os.getcwd()):
    for file in files:
        if file.endswith(".txt"):
            if file.startswith("NFV"):
                txt_files.append(os.path.join(root, file))
            elif file.startswith("GD"):
                txt_files.append(os.path.join(root, file))
for file_name in txt_files:
    with open(file_name, 'r') as file:
        one={}
        for line in file.readlines():
            if line.startswith("sysname"):
                one["sysname"]=line.replace("sysname", '').replace('\n', '').strip()
            if line.startswith("stp bridge-address"):
                one["mac"]=line.replace('stp bridge-address', '').replace('\n', '').strip()
                break
        if one.get("mac") is not None: 
            
            all1.append(one)

df = pd.DataFrame(all1)
df.to_excel('result.xlsx', index=False)

'''

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
iTextSharp 是一个用于创建和处理 PDF 文件的开源库,它提供了许多强大的功能,包括读取 PDF 文件中的书签并将其转换为目录。以下是使用 iTextSharp 读取 PDF 文件中的书签并将其转换为目录的示例代码: ```c# using System; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string inputFilePath = @"input.pdf"; string outputFilePath = @"output.pdf"; using (PdfReader pdfReader = new PdfReader(inputFilePath)) { // Get the bookmarks PdfDictionary bookmarks = pdfReader.Catalog.GetAsDict(PdfName.OUTLINES); if (bookmarks != null) { // Create the document using (Document document = new Document()) { // Create the writer using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(outputFilePath, FileMode.Create))) { // Open the document document.Open(); // Create the table of contents Paragraph tableOfContents = new Paragraph("Table of Contents"); tableOfContents.Alignment = Element.ALIGN_CENTER; document.Add(tableOfContents); // Loop through the bookmarks ProcessBookmarks(bookmarks, document); // Close the document document.Close(); } } } } Console.WriteLine("Done!"); } private static void ProcessBookmarks(PdfDictionary bookmarks, Document document) { // Loop through the bookmarks foreach (PdfName key in bookmarks.Keys) { PdfDictionary bookmark = bookmarks.GetAsDict(key); // Get the title and page number string title = bookmark.GetAsString(PdfName.T).ToString(); int pageNumber = bookmark.GetAsNumber(PdfName.P).IntValue; // Create the chapter Chapter chapter = new Chapter(new Paragraph(title), pageNumber); // Add the chapter to the document document.Add(chapter); // Process any child bookmarks PdfDictionary childBookmarks = bookmark.GetAsDict(PdfName.FIRST); if (childBookmarks != null) { ProcessBookmarks(childBookmarks, document); } } } } } ``` 在上面的代码中,我们首先使用 `PdfReader` 类读取输入 PDF 文件,并使用 `Catalog.GetAsDict()` 方法获取书签。然后,我们创建一个新的 `Document` 对象,并使用 `PdfWriter` 类将其写入输出 PDF 文件。 接下来,我们使用 `ProcessBookmarks()` 方法遍历书签并将其转换为目录。对于每个书签,我们获取其标题和页码,并创建一个新的章节。然后,我们将章节添加到文档中,并递归处理任何子书签。 最后,我们关闭文档并完成转换。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值