簡體   English   中英

如何使用python openpyxl將文本文件的特定部分復制到excel工作表中的特定單元格?

[英]How to copy a specific portion of text file to a specific cell in excel worksheet with python openpyxl?

import openpyxl

## open the specific output file 

with open('/Users/bekir/Desktop/Python_project/Output/r391.txt') as
wb:
    lines = wb.read().splitlines()

## find tht from output file

for line in lines[8400:8480]:
    if line.startswith('       top-water-inlet temp       ='):
        THT = line.split('=',1)[-1].strip()[0:6]

for line in lines[1:30]:
    if line.startswith('  Geometry file :'):
        run_number = line.split(':',1)[-1].strip()[0:4]

## write THT into a specific cell of excel worksheet      

file_path  = '/Users/bekir/Desktop/deneme.xlsx'
xfile = openpyxl.load_workbook(file_path)
ws = xfile['Sheet3']

# have to start range from 1 since excel cell offset starts at 1
for i in range(1,100):
    cell = 'C' + str(i)

    if ws[cell].value == run_number:
        ws['J' + str(i)] = THT
        break

xfile.save(file_path)

嗨,

我可以找到特定的文本文件來源,但是無法使用openpyxl(python2.7)復制到excel工作表的特定單元格中。 程序必須匹配工作表中的run_number (已經寫在工作表中),並將THT值寫入坐標(“ J”列和run number s行)。 我無法編寫代碼的第二部分。 請你幫助我好嗎?

file_path  = '/Users/bekir/Documents/deneme.xlsx'
xfile = openpyxl.load_workbook(file_path)
ws = xfile['sayfa3']

# have to start range from 1 since excel cell offset starts at 1
for i in range(1,100):
    cell = 'D' + str(i)

    if ws[cell].value == run_number:
        ws['D' + str(i)] = THT
        break

xfile.save(file_path)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM