Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Python编写Excel_Python_Excel_Python 3.x_File_Io - Fatal编程技术网

如何用Python编写Excel

如何用Python编写Excel,python,excel,python-3.x,file,io,Python,Excel,Python 3.x,File,Io,我试图编写一个小的“交互式”电子表格程序,以CSV、TSV和Excel格式导出。前两次我都成功了,但最后一次我遇到了一些困难。代码如下: import csv import matplotlib.pyplot as plt import xlwt A1 = int(input("Please input the nueercical value for A1. ")) A2 = int(input("Please do the same for A2. ")) prefixnumber = 1

我试图编写一个小的“交互式”电子表格程序,以CSV、TSV和Excel格式导出。前两次我都成功了,但最后一次我遇到了一些困难。代码如下:

import csv
import matplotlib.pyplot as plt
import xlwt

A1 = int(input("Please input the nueercical value for A1. "))
A2 = int(input("Please do the same for A2. "))
prefixnumber = 1, 2, 3, 4
meta = input("please sum this: Press y for yes and n for no.")
wb = xlwt.Workbook()
sh = wb.add_sheet("sheet")

if meta == "y":
        field = ['A1', 'A2', 'Meta']
        sum = A1 + A2
        sumint = int(sum)
        print(sum)
        rows = [A1, A2, sumint]
        with open('yeetboi.csv', 'w') as export:
                csvwrite = csv.writer(export, sum)
                csvwrite.writerow(field)
                csvwrite.writerow(rows)
                csvwrite.writerow(prefixnumber)
        with open('yeetboi.tsv', 'w') as exporttsv:
                tsvwrite = csv.writer(exporttsv, delimiter='\t')
                tsvwrite.writerow(field)
                tsvwrite.writerow(rows)
                tsvwrite.writerow(prefixnumber)
        sh.write(0,0,field)

else:
        pass
        print("nope")

是的,CSV和TSV文件非常容易使用,特别是与Excel相比,在Excel中,您有各种对象、格式等。请尝试调整下面的简单脚本以写入Excel文件

import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('C:/your_path/test.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Write some simple text.
worksheet.write('A1', 'Hello')

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)


workbook.close()


***********************************************************************************


from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

# Data can be assigned directly to cells
ws['A1'] = 42

# Rows can also be appended
ws.append([1, 2, 3])

# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()

# Save the file
wb.save("C:\your_path\\sample.xlsx")

很抱歉打扰您,但是如何在没有手动编码的情况下将一个sires数字写入第一列?我只找到了insert_cols,但它只提供空格。谢谢