from __future__ import print_function
from os.path import join, dirname, abspath
import xlrd
import sys
sys.stdout = open('C:/Users/Shane/Desktop/textfile.txt','w')
print ('')
# Open the workbook
file_location = "C:/Users/Shane/Desktop/MAIN_DATA.xls"
xl_workbook = xlrd.open_workbook(file_location)
# List sheet names, and pull a sheet by name
#
sheet_names = xl_workbook.sheet_names()
#print('Sheet Names', sheet_names)
xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])
# Or grab the first sheet by index
# (sheets are zero-indexed)
#
xl_sheet = xl_workbook.sheet_by_index(0)
#print ('Sheet name: %s' % xl_sheet.name)
# Pull the first row by index
# (rows/columns are also zero-indexed)
#
row = xl_sheet.row(1) # 1st row
# Print 1st row values and types
#
from xlrd.sheet import ctype_text
print('(Column #) type:value')
for idx, cell_obj in enumerate(row):
cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')
print('(%s) %s %s' % (idx, cell_type_str, cell_obj.value))

所以这是我的代码,我试图使用python和pycharm从excel电子表格写入文本文件。创建了文本文件,因此我需要对其进行编辑,并将excel文件中的文本放在文本文件的中间。此代码只是将结果打印到空白文本文件,如何将此代码写入所有现成文件的中间?
答案 0 :(得分:0)
在python中执行此操作:您应首先阅读您的文件,您可以使用panda:
import pandas as pd
TheFile = pd.read_excel(path + filename)
然后写入新文件:http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
发送一些代码,我们将能够更具体地帮助您!