用python通过打印机打印csv

2024-05-18 04:13:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python输出csv文件。我已经阅读了下面的代码,它可以很好地处理.txt文件,但我无法通过它打印csv

import os
import tempfile

filename = tempfile.mktemp(".txt")

open (filename , "w").write ("Printing file")

os.startfile(filename, "print")

实际上,我想打印一个已经创建的csv文件,不需要编写和创建新文件,然后打印出来

编辑:从打印我的意思是通过打印机硬拷贝打印


Tags: 文件csv代码importtxtosopenfilename
2条回答

我说的是用python代码将csv文件打印为硬拷贝

def printing():
        #reading from csv writing in txt
        with open("CSV_files//newfile.txt", "w") as my_output_file:
            cs = pd.read_csv("CSV_files\\attendance.csv",header=None,index_col=None)
            with open("CSV_files//attendance.csv", "r") as my_input_file:
                [ my_output_file.write(" | ".join(row)+'\n') for row in csv.reader(my_input_file)]
        my_output_file.close()

        #reading from file and storing into reader and converting into string as .write() takes string
        strnew = ""
        with open('CSV_files//newfile.txt',"r") as f:
            reader = f.read()
            strnew = reader
            
        #for checking
        with open('CSV_files//print.txt',"w") as f:
            f.write(strnew)
        
        #printing
        filename = tempfile.mktemp("attendance.txt")#creating a temp file

        open (filename , "w").write(strnew)

        os.startfile(filename, "print")
        messagebox.showinfo("Print","Printing Request sent successfully!")

有关更多信息: github project link

如果要打印csv的内容,可以尝试以下方法:

import csv

file_path = 'a.csv'


with open(file_path) as file:
    content = csv.reader(file)
    for row in content:
        print(row)

相关问题 更多 >