【chatGPT】用chatGPT写代码(一)----在doc文档里面提取指定字符之间的内容,并保存到excel表。

在doc文档里面提取指定字符之间的内容,并保存到excel表。

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

#导入对应库
import os
from docx import Document
import xlwt

def extract_text_between_fields(docx_path, field1, field2, output_path):
doc = Document(docx_path)
extracted_text = ""
is_between_fields = False

for paragraph in doc.paragraphs:
#if field1 in paragraph.text and field2 in paragraph.text:
if field1 in paragraph.text:
extracted_text += paragraph.text + "\n"
is_between_fields = True
'''elif field2 in paragraph.text:
extracted_text += paragraph.text + "\n"
is_between_fields = False
elif is_between_fields:
extracted_text += paragraph.text + "\n"
'''
# 创建 Excel 工作簿和工作表
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Extracted Text')

# 将提取的文本保存到 Excel 表格中的单元格
worksheet.write(0, 0, 'Extracted Text')
worksheet.write(1, 0, extracted_text)

# 保存 Excel 表格
workbook.save(output_path)


# 使用示例
def main():
docx_path = "D:\WorkspaceTZX\实习期工作\AUTOSAR_SWS_OS.docx" # 替换为实际的 Word 文档路径
field1 = "[SWS_Os_" # 替换为字段1的标识
field2 = "⌋" # 替换为字段2的标识
output_path = "D:\WorkspaceTZX\实习期工作\AUTOSAR_SWS_OS.xls" # 替换为输出的 Excel 表格路径

extract_text_between_fields(docx_path, field1, field2, output_path)
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
main()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import os



from docx import Document
import xlwt

def extract_text_between_fields(docx_path, field1, field2, output_path):
    doc = Document(docx_path)
    extracted_text = ""
    is_between_fields = False

    for paragraph in doc.paragraphs:
        #if field1 in paragraph.text and field2 in paragraph.text:
        if field1 in paragraph.text:
            extracted_text += paragraph.text + "\n"
            is_between_fields = True
        '''elif field2 in paragraph.text:
            extracted_text += paragraph.text + "\n"
            is_between_fields = False
        elif is_between_fields:
            extracted_text += paragraph.text + "\n"
        '''
    # 创建 Excel 工作簿和工作表
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('Extracted Text')

    # 将提取的文本保存到 Excel 表格中的单元格
    worksheet.write(0, 0, 'Extracted Text')
    worksheet.write(1, 0, extracted_text)

    # 保存 Excel 表格
    workbook.save(output_path)


# 使用示例
def main():
    docx_path = "D:\WorkspaceTZX\实习期工作\AUTOSAR_SWS_OS.docx"  # 替换为实际的 Word 文档路径
    field1 = "[SWS_Os_"  # 替换为字段1的标识
    field2 = "⌋"  # 替换为字段2的标识
    output_path = "D:\WorkspaceTZX\实习期工作\AUTOSAR_SWS_OS.xls"  # 替换为输出的 Excel 表格路径

    extract_text_between_fields(docx_path, field1, field2, output_path)
def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    main()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大道生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值